/** * Convert date to a specified format of string * months (M), Day (d), 12 hours (h), 24 hours (h), minutes (m), seconds (s), Weeks (E), quarter (q) can use 1-2 placeholders * year (y) can use 1-4 placeholders, milliseconds (s) only with A placeholder (a number that is 1-3 digits) * @param date Date * @param string FMT * @returns string*/functionFormatDate (date, FMT) {//Author:meizz varo = { "m+": Date.getmonth () + 1,//Month"D+": date.getdate (),//Day"H +": date.gethours ()% 12 = = 0? 12:date.gethours ()% 12,//hours"H +": date.gethours (),//hours"m+": date.getminutes (),//points"S+": Date.getseconds (),//seconds"q+": Math.floor ((Date.getmonth () + 3)/3),//Quarterly"S": Date.getmilliseconds ()//milliseconds }; varWeek = ["Day", "one", "two", "three", "four", "five", "six"]; if(/(y+)/. Test (FMT)) {FMT= Fmt.replace (Regexp.$1, (date.getfullyear () + ""). substr (4-regexp.$1. length)); } if(/(e+)/. Test (FMT)) {FMT= Fmt.replace (regexp.$1, (Regexp.$1.length > 1)? (Regexp.$1.length > 2?) "Week": "Week"): "") + week[date.getday () + ""]); } for(varKincho) {if(NewRegExp ("(" + K + ")"). Test (FMT)) {FMT= Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("XX" + o[k]). substr ("" +o[k])); } } returnFMT;}//ExampleConsole.log (FormatDate (NewDate (), "Yyyy-mm-dd hh:mm:ss. S "));//2017-07-11 13:07:12Console.log (FormatDate (NewDate (), "Yyyy-mm-dd HH:mm:ss"));//2017-07-11 13:07:12Console.log (FormatDate (NewDate (), "yyyy-m-d h:m:s.s"));//17-7-11 13:7:12Console.log (FormatDate (NewDate (), "yy-m-d h:m:s"));//17-7-11 13:7:12Console.log (FormatDate (NewDate (), "Yyyy-mm-dd E"));//2017-07-11 13:07:12Console.log (FormatDate (NewDate (), "Yyyy-mm-dd EE"));//2017-07-11 13:07:12Console.log (FormatDate (NewDate (), "Yyyy-mm-dd EEE"));//2017-07-11 13:07:12/** * Determine if the specified date is the week of the year * @param date date * @return integer*/functionGetweeknumber (date) {varTotaldays = 0; varyears =date.getyear ()if(Years < 1000) {years+ = 1900; } vardays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; //determine if a leap year is calculated for the number of days in February if(Math.Round (Date.getyear ()/4) = = Date.getyear ()/4) {days[1] = 29 } varCurmonth =Date.getmonth (); Totaldays=date.getdate (); for(vari = 0; i < Curmonth; i++) {totaldays+= +Days[i]; } //the first day of the year is the days of the week varYearfirstday =NewDate (years, 0, 1). GetDay () | | 7; varWeek =NULL; if(Yearfirstday = = 1) {Week= Math.ceil (Totaldays/yearfirstday); } Else{totaldays-= (7-yearfirstday + 1); Week= Math.ceil (TOTALDAYS/7) + 1; } returnWeek;}//ExampleConsole.log (Getweeknumber (NewDate ()));// inConsole.log (Getweeknumber (NewDate (' 2017-01-01 '));//1Console.log (Getweeknumber (NewDate (' 2017-12-31 '));// -Console.log (Getweeknumber (NewDate (' 2000-05-08 '));// -
Download: [Js_date_function_demo.zip]
Copyright NOTICE: This document is licensed under the attribution-Non-commercial use-sharing (CC BY-NC-SA 3.0 CN) International License Agreement, please specify the author and source. Article title: Code Collection Series--javascript--Date function This article link: http://www.cnblogs.com/sochishun/p/7150428.html This article Sochishun (e-mail: 14507247#qq.com | blog: http://www.cnblogs.com/sochishun/) Published: July 11, 2017 |
Code Collection series--javascript--Date function