JS gets the current datetime format of YYYY-MM-DD HH:MM:SS

Source: Internet
Author: User

------------------------------------------------------------------------------------JS Gets the current datetime format as YYYY-MM-DD hh: Mm:ss

Date.prototype.format = function (format) {var args = {"m+": This.getmonth () + 1, "d+": This.getdate (), "H +": This.geth Ours (), "m+": This.getminutes (), "s+": This.getseconds (), "q+": Math.floor ((This.getmonth () + 3)/3),//quarter

"S": This.getmilliseconds ()}; if (/(y+)/.test (format)), format = Format.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length)); for (var i in args) {var n = args[i]; if (New RegExp ("(" + i + ")"). Test (format), format = Format.replace (regexp.$1,regexp.$1 . length = = 1? N: ("xx" + N). substr (("" + N). length);} return format;};

Calling methods

Alert (new Date (). Format ("Yyyy-mm-dd hh:mm:ss:s"));

------------------------------------------------------------------------------------
function Getnowformatdate () {
var date = new Date ();
var seperator1 = "-";
var Seperator2 = ":";
var month = Date.getmonth () + 1;
var strdate = Date.getdate ();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strdate >= 0 && strdate <= 9) {
Strdate = "0" + strdate;
}
var currentdate = date.getyear () + seperator1 + month + Seperator1 + strdate
+ "" + date.gethours () + Seperator2 + date.getminutes ()
+ Seperator2 + date.getseconds ();
return currentdate;
}

------------------------------------------------------------------------------------

function Curdatetime () {var d = new Date (), var year = D.getyear (), var month = D.getmonth () +1; var Date = D.getdate (); var Day = D.getday (); var hours = d.gethours (); var minutes = d.getminutes (); var seconds = d.getseconds (); var ms = D.getmilliseconds (); var curdatetime= year;if (month>9) curdatetime = Curdatetime + "-" +month;elsecurdatetime = curdatetime + "-0" +month;if ( date>9) Curdatetime = Curdatetime + "-" +date;elsecurdatetime = curdatetime + "-0" +date;if (hours>9) CurDateTime = Curdatetime + "" +hours;elsecurdatetime = Curdatetime + "0" +hours;if (minutes>9) curdatetime = CurDateTime + ":" +minutes ; elsecurdatetime = curdatetime + ": 0" +minutes;if (seconds>9) curdatetime = Curdatetime + ":" +seconds;elsecurdatetime = Curdatetime + ": 0" +seconds;return curdatetime; }alert (Curdatetime ());
--------------------------------------------------------

JS gets the current datetime format of YYYY-MM-DD HH:MM:SS

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.