Link to home
Start Free TrialLog in
Avatar of Bob Schneider
Bob SchneiderFlag for United States of America

asked on

Converting Between Time Formats in VB Script

How can I convert/change a value like 4:00 PM into "16:00:00.0" and 4:00 AM into "4:00:00.0" using vbscript/classic ASP.  Note that it is going into a sql server db varchar field.


I could do it character-by-character but just wondering if there is a formatting tool available.


Thank you!

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>it is going into a sql server db varchar field

Don't do that.  Store dates and times in their proper data type column.  There have been hundreds if not thousands of questions on just this site about issues storing dates and times as strings.  If can ONLY cause you problems.  Mainly, dirty data when something slips through that you cannot convert back.  YES, it WILL happen.


Here's a previous question that goes the other way.  Should be able to just reverse it:
https://www.experts-exchange.com/questions/23785926/VBS-Convert-Time-hh-mm-ss-1-23-58-PM-to-HH-mm-ss-13-23-58.html

It relies on FormatDateTime:
https://www.w3schools.com/asp/func_formatdatetime.asp

You could also just split the string into the pieces and if PM, add 12 to hours.
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Check if it's valid
ISDATE(expression)

Format
d=CDate("May 18, 2015")
d=CDate(#5/18/15#)
d=CDate("5:18:40 AM")  
response.write(d)

Also, Totally agree with the above comments regarding date fields rather than varchar  

A.