Solved

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

Posted on 2007-03-30
6
1,362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 52

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 52

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

632 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