ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using "dd/mm/yyyy h:mm" on Date Variables (https://www.excelbanter.com/excel-programming/354129-using-dd-mm-yyyy-h-mm-date-variables.html)

ExcelMonkey

Using "dd/mm/yyyy h:mm" on Date Variables
 
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

Tom Ogilvy

Using "dd/mm/yyyy h:mm" on Date Variables
 
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



--
Regards,
Tom Ogilvy

"ExcelMonkey" wrote in message
...
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





All times are GMT +1. The time now is 05:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com