JS powerful date format, timestamp support 10-bit or 13-bit timestamp, or time string, while supporting the processing of Android iOS, not just date formatting there are other ways, such as how many days to get a month, get a date in the first week of the year, the first day of the year , the day of the week, and other methods
Source:
/*** JS date format, timestamp support 10-bit or 13-bit timestamp, or time String * @param {string} format passed in the string, y-m-d h:i:s each letter represents the meaning see code * @param {int String}timestamp the time to format the default is that the current time can be a string in the form of a date, which can be a 10-bit or 13-bit timestamp * @return {string} formatted time string */function dateformat (format, Timestamp) {if (timestamp== "" | | timestamp==null | | format== "") return ""//if it is passed in a string form of a date, change back to timestamp if (typeof (timestamp) = = "string") {//compatible with Iosvar timestamp = Timestamp.replace ("T", "") if (/(Iphone|ipad|ipod|ios)/i.test (navigator.useragent)) {timestamp = Timestamp.replace (/\-/g, "/");} if (Timestamp.indexof ("-")!==false | | timestamp.indexof ("/")!==false) timestamp = new Date (timestamp). GetTime ()}// If a 10-bit timestamp is passed into the 13-bit if (timestamp.tostring (). length = = Timestamp * 1000//If this step is still not a 13-bit timestamp, Description data is problematic timestamp = parseint (timestamp) if (timestamp.tostring (). Length! =) return "" Var A, jsdate= new Date (timestamp); var pad = function (n, c) {if ((n = n + ""). Length < C) {return new Array (++c-n.length). Join ("0") + N;} else {return n;}}; var txt_weekdays = ["SundaY "," Monday "," Tuesday "," Wednesday "," Thursday "," Friday "," Saturday "];var Txt_ordin = {1:" St ", 2:" nd ", 3:" rd ", +:" St ", "ND", at: "Rd", to: "St"};var txt_months = ["", "January", "February", "March", "April", "may", "June", "July", "August", "September", "October", "November", "December"]; var f = {//dayd:function () {return pad (F.J (), 2)},//2 bit days, 02d:function () {return F.L (). substr (0,3)},//the first three bits of the week word j:function () {return jsdate.getdate ()},//days 2l:function () {return TXT_WEEKDAYS[F.W ()]},//Week English n:function () {return F.W () + 1},// The number of day of the week Sunday for 1s:function () {return TXT_ORDIN[F.J ()]? TXT_ORDIN[F.J ()]: ' th '},w:function () {return jsdate.getday ()},/ The number of day of the week Sunday for 0z:function () {return (Jsdate-new Date (jsdate.getfullyear () + "/1/1"))/864e5 >> 0},//The current time is the day of the year Monthf:function () {return TXT_MONTHS[F.N ()]},//month English m:function () {return pad (F.N (), 2)},//2 bit month 02m:function () {return F.F (). substr (0,3)},//month English first three letters N:function () {return jsdate.getmonth () + 1},//Month 2t:function () {//Current month total number of days Var n;if ( (n = jsdate.getmonth () + 1) = = 2) {return + F.L ();} else{if (N & 1 && N < 8 | |! ( N & 1) && n > 7) {return,} Else{return 30;}}},//yearl:function () {var y = f.y (); return (! ( Y & 3) && (y% 1e2 | |! ( Y% 4e2))? 1:0},//year y:function () {return jsdate.getfullyear ()},y:function () {return (Jsdate.getfullyear () + ""). Slice (2)},// Timea:function () {return getHours (jsdate) > 11? "PM": "AM"},a:function () {return F.A (). toUpperCase ()},b:function () {//Peter Paul Koch:var off = (jsdate.gettimezoneoffs ET () + *60;var theseconds = (getHours (jsdate) * 3600) + (Jsdate.getminutes () * +) + jsdate.getseconds () + Off;var beat = Math.floor (theseconds/86.4), if (Beat >) beat-= 1000;if (Beat < 0) beat + = 1000;if ((String (Beat)). length = = 1) beat = "xx" +beat;if ((String (Beat)). length = = 2) beat = "0" +beat;return beat;},g:function () {return getHours (jsdate)% 12 | | 12},g:function () {return getHours (jsdate)},h:function () {return pad (F.G (), 2)},h: function () {return pad (getHours (jsdate), 2)},i:function () {return pad (jsdate.getminutes (), 2)},s:function () {return Pad (Jsdate.getseconds (), 2)},//u not supported yet//timezone//e not supported yet//i not supported Yeto:function () {var t = Pad (Math.Abs (Jsdate.gettimezoneoffset ()/60*100), 4), if (Jsdate.gettimezoneoffset () > 0) t = "-" + t; else T = "+" + T;return t;},p:function () {var O = F.O (); return (O.SUBSTR (0, 3) + ":" + o.substr (3, 2))},//t not supported Yet//z not supported yet//full date/timec:function () {return f.y () + "-" + f.m () + "-" + f.d () + "T" + f.h () + ":" + f.i ( ) + ":" + f.s () + F.P ()},//R not supported Yetu:function () {return Math.Round (Jsdate.gettime ()/1000)}};//compatible with iOS function ge Thours (dateobj) {//Because the time of UTC has been converted in the top, the T is gone, so here is the same way to return dateobj.gethours (); if (/(Iphone|ipad|ipod|ios)/ I.test (navigator.useragent)) {return dateobj.getutchours ();} Else{return dateobj.gethours ();}} var ret = "" Return Format.replace (/[\\]? ( [A-za-z]) /g, function (t, s) {if (t!=s) {//ESCapedret = s;} else if (F[s]) {//a date function Existsret = F[s] ();} else{//Nothing specialret = s;} return ret;});
Examples of Use:
Month and day time of the second var temp = DateFormat ("y-m-d h:i:s", New Date (). GetTime ()); Console.log (temp);//day var temp = DateFormat ("m-d", " 2018-02-24 "), Console.log (temp),///week month var temp = DateFormat (" F "," 2018-02-24 "), Console.log (temp);//week english var temp = DateFormat ("L", "2018-02-24"); Console.log (temp); The day of the week, Sunday for the first day var temp = DateFormat ("N", "2018-02-24"); Console.log (temp);//days of the week, Monday for the first day var temp = DateFormat ("W", "2018-02-24"); Console.log (temp);//day of the year var temp = DateFormat ("Z", "2018-02-24"); Console.log (temp);//Gets a date in the first week of the year Math.ceil (Day of the Year/7) var temp = Math.ceil ( DateFormat ("Z", "2018-03-10")/7) Console.log (temp);//Gets the number of days of the month var temp = DateFormat ("T", "2018-02-24"); Console.log ( temp);
Source: jsfun.cn
JS Powerful date Format function, not only can format the date, can also query the week, the day of the year and so on