Solved

Convert Timestamp String to Regular Date & Time Format in ASP Classic

Posted on 2007-03-30
6
1,260 Views
Last Modified: 2008-01-09
In ASP 3 with VB Script, how do I convert a timestamp string like
     2007-03-29  17:30:21    

to standard date format like
     03-29-2007  05:30 PM  ?

For example, in PHP I could do:

     $string = "2007-03-29  17:30:21";
     $updated = date('m-d-Y h:i A', strtotime($string) );

What is the ASP equivalent?  Thanks.
0
Comment
Question by:Randall-B
  • 3
  • 2
6 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 18827458
try

<%
      str = "2007-03-29  17:30:21"
      
      strDate = cDate(str)
      newstr = right("0" & month(strDate),2) & "-" & right("0" & day(strDate),2) & "-" & right("00" & year(strDate),4) & " " & right("0" & hour(strDate),2) & ":" & right("0" & minute(strDate),2) & " "
      if hour(strDate) > 12 then
            newstr = newstr & "PM"
      else
            newstr = newstr & "AM"
      end if
      
      response.write str & " to " & newstr
%>
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18828953
Or if you like living dangerously you can depend on the computer settings as in:

MsgBox FormatDateTime(CDate("2007-03-29  17:30:21"), vbGeneralDate)
0
 

Author Comment

by:Randall-B
ID: 18830871
ryancys,
   Thanks; that works great except it is not converting "17" to "05" in "17:30:21".  I would like it to output:   03-29-2007  05:30 PM  (with the "05"). Thanks.

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Randall-B
ID: 18830876
acperkins,
   Thanks for the alternate suggestion. As you noted, depending on the computer settings might be risky, as each user could different settings, so I'll go with the method ryancys suggested. Thanks again.
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 250 total points
ID: 18830955
oops, sorry, try this instead:


<%
      str = "2007-03-29  17:30:21"
     
      strDate = cDate(str)
     
   if hour(strDate) = 12 then  

 newstr = right("0" & month(strDate),2) & "-" & right("0" & day(strDate),2) & "-" & right("00" & year(strDate),4) & " " & right("0" & hour(strDate),2) & ":" & right("0" & minute(strDate),2) & " PM"

    elseif hour(strDate) > 12 then

 newstr = right("0" & month(strDate),2) & "-" & right("0" & day(strDate),2) & "-" & right("00" & year(strDate),4) & " " & right("0" & hour(strDate)-12,2) & ":" & right("0" & minute(strDate),2) & " PM"

      else

 newstr = right("0" & month(strDate),2) & "-" & right("0" & day(strDate),2) & "-" & right("00" & year(strDate),4) & " " & right("0" & hour(strDate),2) & ":" & right("0" & minute(strDate),2) & " AM"

      end if
     
      response.write str & " to " & newstr
%>
0
 

Author Comment

by:Randall-B
ID: 18830980
ryancys,
   That's it. Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now