Converting %date% %time% to VB Time/Date
Author |
Message |
Rick Raisle #1 / 8
|
Converting %date% %time% to VB Time/Date
I'm outputting time to a batch file using a line like: echo. %date% %time% >> "C:\Logfile.txt" On my system, that results in a text line in the log file like the following: Thu 07/24/2008 15:16:36.85 I realize it will vary depending on Regions/International settings, but ignoring that for the moment, I would like to have a function to quickly turn this into a decimal Date in my VB app. I can use DateValue on the "07/24/2008" with something like: cDec(DateValue("07/24/2008")) to give 39653 and can use TimeValue on a PORTION of the time to give: CDec(TimeValue("15:16:36")) = 0.636527777777778 But it sure is clumsy to extract only the MM/DD/YYYY portion to do the date, and especially the HH:MM:SS portion to do the time, then grab the decimal seconds off the end. Is there some function I'm missing that would, preferably, take the whole date and time string and convert it to decimal days? Or lacking that, can at least convert the entire time string correctly. I can't believe that TimeValue fails with "15:16:36.85"! Yes, I know I can make a function to extract what I need; I just didn't want to reinvent the wheel, in case I'm not familiar with a time/date function I should be. After that is answered, I guess the next step is will whatever the answer is work with other Region/International settings? -- Regards, Rick Raisley heavymetal-A-T-bellsouth-D-O-T-net
|
Tue, 11 Jan 2011 03:56:43 GMT |
|
|
Jeff Johnso #2 / 8
|
Converting %date% %time% to VB Time/Date
Quote: > But it sure is clumsy to extract only the MM/DD/YYYY portion to do the > date, and especially the HH:MM:SS portion to do the time, then grab the > decimal seconds off the end. > Is there some function I'm missing that would, preferably, take the whole > date and time string and convert it to decimal days?
CDec(CDate("...")) Quote: > I can't believe that TimeValue fails with "15:16:36.85"!
CDate() can't handle that, though. Quote: > After that is answered, I guess the next step is will whatever the answer > is work with other Region/International settings?
For some dates, yes. For others, no. Welcome to Hell. CDate() WILL convert DD/MM/YYYY, but you can't be guaranteed to get the proper results. For example, 8/12/08 will convert to August 12th, not December 8th. (Unless this is governed by regional settings, and I'm too lazy to switch them and try it.)
|
Tue, 11 Jan 2011 04:13:34 GMT |
|
|
Rick Raisle #3 / 8
|
Converting %date% %time% to VB Time/Date
Quote:
>> But it sure is clumsy to extract only the MM/DD/YYYY portion to do the >> date, and especially the HH:MM:SS portion to do the time, then grab the >> decimal seconds off the end. >> Is there some function I'm missing that would, preferably, take the whole >> date and time string and convert it to decimal days? > CDec(CDate("..."))
I haven't gotten that to work with any format of date AND time, just with date. Is there a format I'm missing that works? Quote: >> I can't believe that TimeValue fails with "15:16:36.85"! > CDate() can't handle that, though.
Yeah, and I need fractional second resolution. Durn! Quote: >> After that is answered, I guess the next step is will whatever the answer >> is work with other Region/International settings? > For some dates, yes. For others, no. Welcome to Hell.
I seem to find myself there a lot. I'd enjoy it more, if there were more women. ;-) Quote: > CDate() WILL convert DD/MM/YYYY, but you can't be guaranteed to get the > proper results. For example, 8/12/08 will convert to August 12th, not > December 8th. (Unless this is governed by regional settings, and I'm too > lazy to switch them and try it.)
I'm pretty sure CDate is regional-aware (all the other "C" functions are). But I don't know that DOS's %date% output will automatically match Windows regional format. ;-) -- Regards, Rick Raisley heavymetal-A-T-bellsouth-D-O-T-net
|
Tue, 11 Jan 2011 04:24:08 GMT |
|
|
Matt Williamso #4 / 8
|
Converting %date% %time% to VB Time/Date
Quote:
>>> But it sure is clumsy to extract only the MM/DD/YYYY portion to do the >>> date, and especially the HH:MM:SS portion to do the time, then grab the >>> decimal seconds off the end. >>> Is there some function I'm missing that would, preferably, take the >>> whole date and time string and convert it to decimal days? >> CDec(CDate("...")) > I haven't gotten that to work with any format of date AND time, just with > date. Is there a format I'm missing that works? >>> I can't believe that TimeValue fails with "15:16:36.85"! >> CDate() can't handle that, though. > Yeah, and I need fractional second resolution. Durn! >>> After that is answered, I guess the next step is will whatever the >>> answer is work with other Region/International settings? >> For some dates, yes. For others, no. Welcome to Hell. > I seem to find myself there a lot. I'd enjoy it more, if there were more > women. ;-) >> CDate() WILL convert DD/MM/YYYY, but you can't be guaranteed to get the >> proper results. For example, 8/12/08 will convert to August 12th, not >> December 8th. (Unless this is governed by regional settings, and I'm too >> lazy to switch them and try it.) > I'm pretty sure CDate is regional-aware (all the other "C" functions are). > But I don't know that DOS's %date% output will automatically match Windows > regional format. ;-)
The special %date% environment variable does change when you change the regional format. Open a cmd prompt and do an echo %date%. Now goto control panel | regional options and change to Finnish and go back to your cmd prompt and echo the date again.
|
Tue, 11 Jan 2011 04:53:52 GMT |
|
|
Jeff Johnso #5 / 8
|
Converting %date% %time% to VB Time/Date
Quote: >>> Is there some function I'm missing that would, preferably, take the >>> whole date and time string and convert it to decimal days? >> CDec(CDate("...")) > I haven't gotten that to work with any format of date AND time, just with > date. Is there a format I'm missing that works?
I took exactly what you wrote and tried this in the Immediate window: ? CDec(CDate("07/24/2008 15:16:36")) I got 39653.6365277778. It even works if there are multiple spaces between the date and time.
|
Tue, 11 Jan 2011 05:09:17 GMT |
|
|
Rick Raisle #6 / 8
|
Converting %date% %time% to VB Time/Date
Quote:
>>>> Is there some function I'm missing that would, preferably, take the >>>> whole date and time string and convert it to decimal days? >>> CDec(CDate("...")) >> I haven't gotten that to work with any format of date AND time, just with >> date. Is there a format I'm missing that works? > I took exactly what you wrote and tried this in the Immediate window: > ? CDec(CDate("07/24/2008 15:16:36")) > I got 39653.6365277778. It even works if there are multiple spaces between > the date and time.
Yes, that works. The string I get from DOS, though, has the day of the week and decimal seconds, so this CDec(CDate("Fri 07/25/2008 8:07:58.35")) doesn't work. Let's see, a 1-liner might be something like: decDate = CDec(CDate(Mid$(strDate, 5, Len(strDate) - 7))) + Right$(strDate, 3) / 3600 / 24 where strDate = "Fri 07/25/2008 8:07:58.35", from the DOS prompt. -- Regards, Rick Raisley heavymetal-A-T-bellsouth-D-O-T-net
|
Tue, 11 Jan 2011 20:25:50 GMT |
|
|
Jeff Johnso #7 / 8
|
Converting %date% %time% to VB Time/Date
Quote: >> ? CDec(CDate("07/24/2008 15:16:36")) >> I got 39653.6365277778. It even works if there are multiple spaces >> between the date and time. > Yes, that works. The string I get from DOS, though, has the day of the > week and decimal seconds, so this > CDec(CDate("Fri 07/25/2008 8:07:58.35")) > doesn't work.
Oh, sorry. I thought that once we determined that the fractional seconds didn't work that it was a foregone conclusion that you'd have to do SOME string surgery. I guess I didn't make it clear, though.
|
Tue, 11 Jan 2011 21:35:06 GMT |
|
|
Tony Procto #8 / 8
|
Converting %date% %time% to VB Time/Date
It does on my system Matt. It flips between US & UK as I change my regional settings (this is W2003 but I believe XP to be the same) Tony Proctor
Quote:
>>>> But it sure is clumsy to extract only the MM/DD/YYYY portion to do the >>>> date, and especially the HH:MM:SS portion to do the time, then grab the >>>> decimal seconds off the end. >>>> Is there some function I'm missing that would, preferably, take the >>>> whole date and time string and convert it to decimal days? >>> CDec(CDate("...")) >> I haven't gotten that to work with any format of date AND time, just with >> date. Is there a format I'm missing that works? >>>> I can't believe that TimeValue fails with "15:16:36.85"! >>> CDate() can't handle that, though. >> Yeah, and I need fractional second resolution. Durn! >>>> After that is answered, I guess the next step is will whatever the >>>> answer is work with other Region/International settings? >>> For some dates, yes. For others, no. Welcome to Hell. >> I seem to find myself there a lot. I'd enjoy it more, if there were more >> women. ;-) >>> CDate() WILL convert DD/MM/YYYY, but you can't be guaranteed to get the >>> proper results. For example, 8/12/08 will convert to August 12th, not >>> December 8th. (Unless this is governed by regional settings, and I'm too >>> lazy to switch them and try it.) >> I'm pretty sure CDate is regional-aware (all the other "C" functions >> are). But I don't know that DOS's %date% output will automatically match >> Windows regional format. ;-) > The special %date% environment variable does change when you change the > regional format. Open a cmd prompt and do an echo %date%. Now goto control > panel | regional options and change to Finnish and go back to your cmd > prompt and echo the date again.
|
Tue, 11 Jan 2011 23:51:12 GMT |
|
|
|