Solved

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

Posted on 2007-03-30
6
1,299 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 50

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 50

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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide cell in a table 2 27
Index on a Table 6 25
Insert Button on a table 16 37
Button on Table, name table1 not working 4 20
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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