JS style code to display date time in a specified format _javascript tips

Source: Internet
Author: User
<textarea id="runcode35851"><ptml> <body> <script>///<summary>///format display date time///</summary>///<param Name= "X" > The date time to display, such as new Date () </param>///<param name= "y" > The format to be displayed, such as Yyyy-mm-dd Hh:mm:ss</param > Function Date2str (x,y) {var z = {m:x.getmonth () +1,d:x.getdate (), h:x.gethours (), M:x.getminutes (), S:x.getseconds ( )}; y = Y.replace (/(m+|d+|h+|m+|s+)/g,function (v) {return (v.length>1?) 0 ":" ") +eval (' Z. ' +v.slice ( -1))). Slice (-2)}); Return Y.replace (/(y+)/g,function (v) {return x.getfullyear (). toString (). Slice (-v.length)}); Alert (Date2str (New Date (), "Yyyy-mm-dd hh:mm:ss")); Alert (Date2str (New Date (), "yyyy-m-d h:m:s")); </script> </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Time is the current time
Copy Code code as follows:

//1.2013-4-9 11:21:32
//2.2013 year April 9 11:21 32 sec
//3.2013 Year April 9 11 o'clock in the morning 2 1 minutes 32 seconds
//4.2013 year April 9 13:21 P.M. 32 seconds
/*
var thedate = new Date ();
Alert (thedate.getfullyear () + '-' + thedate.getmonth () + '-' + thedate.getdate () + ' + thedate.tolocaletimestring ()) ;
Alert (thedate.tolocaledatestring () + ' + thedate.gethours () + ' dot ' + thedate.getminutes () + ' min ' + thedate.getseconds ( ) + ' seconds ');
if (thedate.gethours () <) {
alert (thedate.tolocaledatestring () + ' a.m. ' + thedate.gethours () + ' dot ' + thedat E.getminutes () + ' min ' + thedate.getseconds () + ' seconds ');
}
Else {
Alert (thedate.tolocaledatestring () + ' PM ' + thedate.gethours () + ' dot ' + thedate.getminutes () + ' min ' + thedate.getseconds () + ' seconds ');
}
*/

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.