- An extension to date that converts date to a string of the specified format
- The month (m), Day (d), hour (h), Minute (m), second (s), quarter (q) can be used with 1-2 placeholders,
- Year (y) can use 1-4 placeholders, milliseconds (S) with only 1 placeholders (1-3 digits)
- Date.prototype.Format = function (FMT) { //author:meizz
- var o = {
- "m+": this.getmonth () + 1, //month
- "d+": this.getdate (), //day
- "H +": this.gethours (), //hour
- "m+": this.getminutes (), //min
- "s+": this.getseconds (), //sec
- "q+": Math.floor ((this.getmonth () + 3)/3), //quarterly
- "S": this.getmilliseconds () //MS
- };
- if (/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));
- For (var k in O)
- if (new RegExp ("(" + K + ")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("xx" + o[k]). substr (("" + o[k]).));
- return FMT;
- }
- Call:
- var time1 = new Date (). Format ("Yyyy-mm-dd");
- var time2 = new Date (). Format ("Yyyy-mm-dd HH:mm:ss");
- var nowtime=New Date ();
- Nowtime.setmonth (Nowtime.getmonth ()-1);
- Alert (Nowtime.format ("Yyyy-mm-dd HH:mm:ss");)//Current time last month
jquery action on the current date (formatting the current date)