JS Time Format __js

Source: Internet
Author: User
Tags getdate

The first of these methods

var now = new Date ();
var nowstr = Now.format ("Yyyy-mm-dd hh:mm:ss");
Using Method 2:
var testdate = new Date ();
var teststr = Testdate.format ("yyyy mm month DD Day hh hour mm minute ss seconds");
alert (TESTSTR);
Example:
Alert (new Date (). Format ("YYYY year mm month DD Day"));
Alert (new Date (). Format ("mm/dd/yyyy"));
Alert (new Date (). Format ("YyyyMMdd"));
Alert (new Date (). Format ("Yyyy-mm-dd hh:mm:ss"));

The second method:

Time processing
var __formatterdatetime = function (date) {
var datetime = Date.getfullyear ()
+ "/"//"Year"
+ ((date.getmonth () + 1) > 10? (Date.getmonth () + 1): "0"
+ (Date.getmonth () + 1))
+ "/"//"Month"
+ (Date.getdate () < 10? "0" + date.getdate (): Date
. GetDate ())
+ " "
+ (Date.gethours () < 10? "0" + date.gethours (): Date
. GetHours ())
+ ":"
+ (Date.getminutes () < 10? "0" + date.getminutes (): Date
. getminutes ())
+ ":"
+ (Date.getseconds () < 10? "0" + date.getseconds (): Date
. getseconds ());
return datetime;
};

The third type:

Date.prototype.format = function (FMT) {
var year = This.getfullyear ();
var mont = this.getmonth () + 1;
var days = This.getdate ();
var hour = this.gethours ();
var minu = This.getminutes ();
var seco = This.getseconds ();
var mill = This.getmilliseconds ();

var m = null;
m = Fmt.match (/y/g); m = m = = null? []: M; Year = Year.formatnumber ("%" + m.length + "D");
m = Fmt.match (/m/g); m = m = = null? []: M; Mont = Mont.formatnumber ("%" + m.length + "D");
m = Fmt.match (/d/g); m = m = = null? []: M; Days = Days.formatnumber ("%" + m.length + "D");
m = Fmt.match (/h/g); m = m = = null? []: M; hour = Hour.formatnumber ("%" + m.length + "D");
m = Fmt.match (/m/g); m = m = = null? []: M; Minu = Minu.formatnumber ("%" + m.length + "D");
m = Fmt.match (/s/g); m = m = = null? []: M; Seco = Seco.formatnumber ("%" + m.length + "D");
m = Fmt.match (/s/g); m = m = = null? []: M; Mill = Mill.formatnumber ("%" + m.length + "D");
var str = FMT;
str = str.replace (/y+/g, year);
str = str.replace (/m+/g, Mont);
str = str.replace (/d+/g, days);
str = str.replace (/h+/g, hour);
str = str.replace (/m+/g, minu);
str = str.replace (/s+/g, Seco);
str = str.replace (/s+/g, Mill);

return str;
};

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.