Solved

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

Posted on 2007-03-30
6
1,287 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

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.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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