Link to home
Create AccountLog in
Avatar of hpops
hpops

asked on

VBScript date format incorrect

Hello, I have some code here that worked fine towards the end of last month but noticed that it stopped working correctly at the beginning of this month as it doesn't return the date field format as required for the rest of my script function. I need the date format to be mm/dd/yyyy, however this script seems to be returning only m/d/yyyy.

Can anyone help me get this to work in the mm/dd/yyyy format?



 
Dim strDateTime, strDate, strTime
strDateTime = DateAdd("n", 1, Now())
strDate = Left(strDateTime, InStr(strDateTime, " ")-1)
strTime = FormatDateTime(strDateTime, 4)

strDateTime = strDate & " " & strTime

WScript.Echo strDatetime

Open in new window

Avatar of Bill Prew
Bill Prew

Here's an easy way to work around that:

Dim strDateTime, strDate, strTime
datDateTime = DateAdd("n", 1, Now())
strDate = Right("0" & Month(datDateTime), 2) & "/" & Right("0" & Day(datDateTime), 2)& "/" & Year(datDateTime)
strTime = FormatDateTime(strDateTime, 4)

strDateTime = strDate & " " & strTime

WScript.Echo strDatetime

Open in new window

~bp
The basic idea there is to pad the month and day with a leading "0", and then take the rightmost 2 characters from that result.  In this way when the day or month are single digits, they will get a leading zero, but when they are 2 digits we just throw away the extra unneeded leading zero.  Hope that makes sense.

~bp
Avatar of hpops

ASKER

billprew,

This looks good for the date but the time shows all zeros. I also need that and failed to mention it earlier. Can you help me out with the time issue? It shows as 11/07/2011 00:00
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of hpops

ASKER

billprew,

This did the trick. Thank you very much.
Welcome.

~bp