Time format conversion in ASP

Source: Internet
Author: User
Tags current time min

The now () function in ASP can get the current time of the system, the format of this time is like "2008-5-19 10:55:26". However, sometimes we are more accustomed to using the time format "May 19, 2008 10:55 26 seconds". Well, How can we get such a request?

Idea one: Replace the function with the replace (), with the following code:

<%
function Chgtime (str)
If str <> "then
str = replace (str, "-", "Year", 1, 1) ' Convert first '-' to ' year '
str = replace (str, "-", "Month", 1, 1) ' Convert second '-' to ' month '
str = replace (str, "", "Day") ' Convert space ' to ' Day '
str = replace (str, ":", "when", 1, 1) ' Translate colon ': ' to ' when '
str = replace (str, ":", "Min", 1, 1) ' Convert colon ': ' to ' divide '
str = str& "seconds" to add "seconds" at the end
End If
Chgtime=str
End Function
Response.Write Chgtime ("2008-5-19 10:55:26")
%>

Operation Result: May 19, 2008 10:55 26 seconds

Thinking Analysis: Replace from left to right, referring to the explanation behind the line of code.

Idea two: Using the FormatDateTime () function, the specific code is as follows:
<%
function chgtime1 (str)
Dim str1,str2
If str <> "then
STR1 = FormatDateTime (str,1) ' Get date part, get ' May 19, 2008 Monday '
str2 = FormatDateTime (str,3) ' Get get time part, get ' 10:55:26 '
End If
chgtime1=str1& "" &str2
End Function
Response.Write chgtime1 ("2008-5-19 10:55:26")
%>

Operation Result: May 19, 2008 Monday 10:55:26

Thinking Analysis: Use different parameters of the FormatDateTime () function to get the different parts of the time and then connect with the string connector.

Combined with the above two ideas, you can get shape such as "May 19, 2008 Monday 10:55 26 seconds" time format. The code is as follows:

<%
function Chgtime2 (str)
Dim str1,str2
If str <> "then
STR1 = FormatDateTime (str,1) ' Get date part
str2 = FormatDateTime (str,3) ' Get Get time part
STR2 = replace (str2, ":", "when", 1, 1) ' Convert colon ': ' to ' when '
STR2 = replace (str2, ":", "Min", 1, 1) ' Convert colon ': ' to ' divide '
STR2 = str2& "seconds" to add "seconds" at the end
End If
chgtime2=str1& "" &str2
End Function
Response.Write chgtime2 ("2008-5-19 10:55:26")
%>

Operation Result: May 19, 2008 Monday 10:55 26 seconds

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.