Solved

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

Posted on 2007-03-30
6
1,275 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Incremental load example 2 52
How to import SQL 2000 database to SQL 2014 5 97
Error when saving to sql table a '/' 5 25
Question about ASP Including Files 6 39
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

10 Experts available now in Live!

Get 1:1 Help Now