Formatting Dates!!

A

Al

Hi Folks,

I am using Office97. I am trying to "extract" the year from:
MyDate = Format(Date, "dd, ddd mmm yyyy")

Both;
Format(MyDate, "yyyy")
Year(MyDate)
Fail to return the year.

Yet if MyDate = Format(Date, "dd mm yyyy") then both year and format will
work.

Can somebody please tell me what I am missing?

The reason for all this is that have a listbox that is populated with the
seven days from today in it, in format ddd, dd mm yyyy. I need to then
insert the year/weekday/month/day etc into various bookmarks in the
template.

Thanks

-Al
 
P

Peter Hewett

Hi Al

Take a slightly different approach. In your ListBox control store the date
twice. The first being your formatted date ("dd, ddd mmm yyyy") and the
second being your date as a serial number. When the user selects a date
(they only see the formatted date) you actually use the serial date (stored
in column 2 of the ListBox) to provide year/weekday/month/day etc.

So use something like this to populate your ListBox

Dim dateOther As Date
Dim lngIndex As Long
Dim dateNow As Date

' Get date like this just in case it changes (paranoia!)
dateNow = Date

' Date = -30 to +30 days from today
For lngIndex = -30 To 30
dateOther = DateAdd("d", lngIndex, dateNow)
With lstDates
.AddItem Format$(dateOther, "dd, ddd mmm yyyy")
.List(.ListCount - 1, 1) = CLng(dateOther)
End With
Next

Use this to get the date info out:

Dim strDay As String
Dim strMonth As String
Dim strYear As String
Dim strWeekday As String
Dim dateSelected As Date

dateSelected = lstDates.List(lstDates.ListIndex, 1)
strDay = Format$(dateSelected, "dd")
strMonth = Format$(dateSelected, "mmm")
strYear = Format$(dateSelected, "yyyy")
strWeekday = Format$(dateSelected, "dddd")

The above assumes you're using a ListBox control named "lstDates" You don't
need to change your lisboxes column count as you still only want to display
one column.

HTH + Cheers - Peter
 
A

Al

I like it - nice approach.

Thanks

-Al

Peter Hewett said:
Hi Al

Take a slightly different approach. In your ListBox control store the date
twice. The first being your formatted date ("dd, ddd mmm yyyy") and the
second being your date as a serial number. When the user selects a date
(they only see the formatted date) you actually use the serial date (stored
in column 2 of the ListBox) to provide year/weekday/month/day etc.

So use something like this to populate your ListBox

Dim dateOther As Date
Dim lngIndex As Long
Dim dateNow As Date

' Get date like this just in case it changes (paranoia!)
dateNow = Date

' Date = -30 to +30 days from today
For lngIndex = -30 To 30
dateOther = DateAdd("d", lngIndex, dateNow)
With lstDates
.AddItem Format$(dateOther, "dd, ddd mmm yyyy")
.List(.ListCount - 1, 1) = CLng(dateOther)
End With
Next

Use this to get the date info out:

Dim strDay As String
Dim strMonth As String
Dim strYear As String
Dim strWeekday As String
Dim dateSelected As Date

dateSelected = lstDates.List(lstDates.ListIndex, 1)
strDay = Format$(dateSelected, "dd")
strMonth = Format$(dateSelected, "mmm")
strYear = Format$(dateSelected, "yyyy")
strWeekday = Format$(dateSelected, "dddd")

The above assumes you're using a ListBox control named "lstDates" You don't
need to change your lisboxes column count as you still only want to display
one column.

HTH + Cheers - Peter
 
A

Al

Peter,

I get a type mismatch on Clng(dateOther)

How do i get the date back to the shorter format?

-Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top