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:

It relies on FormatDateTime:

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

Link to home
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

d=CDate("May 18, 2015")
d=CDate("5:18:40 AM")  

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