Using "dd/mm/yyyy h:mm" on Date Variables

E

ExcelMonkey

I am calculating dates in a loop and using the following date format:

DateFormat = "dd/mm/yyyy h:mm"

When I print to the Immediate Window, the first hour (hour ending 0) shows
up without the h:mm extension. I expected to see: 01/01/2005 00:00:00.

Why is this?
Thanks

CurrentDate = Format(DateSerial(CurrentYear, CurrentMonth, CurrentDay),
DateFormat)
Debug.Print CurrentDate
CurrentDate = Format(CurrentDate + 1 / 24, DateFormat)
Debug.Print CurrentDate

Immediate Window:
01/01/2005
01/01/2005 01:00:00
 
T

Tom Ogilvy

Obviously CurrentDate is Dimmed as Date. Since it has no time value the
first time, it doesn't display.

I added a ", cdbl(CurrentDate)" to each Debug.Print line to illustrate

1/1/05 38353
1/1/05 1:00:00 AM 38353.0416666667

Using format as an intermediate in the assignment statement does't affect
what is stored in the Date Serial. In fact, you would get the same result
without it:

Sub F()
Dim currentDate As Date
DateFormat = "dd/mm/yyyy h:mm"
CurrentYear = 2005
CurrentMonth = 1
CurrentDay = 1
currentDate = DateSerial(CurrentYear, _
CurrentMonth, CurrentDay)
Debug.Print currentDate, CDbl(currentDate)
currentDate = currentDate + 1 / 24
Debug.Print currentDate, CDbl(currentDate)

End Sub

Again produces:

1/1/05 38353
1/1/05 1:00:00 AM 38353.0416666667
 

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