1. Format the JS Date object in the specified format and add a prototype method
/** * Returns the string * format of the specified format eg: ' Yyyy-mm-dd hh:mm:ss ' **/Date.prototype.format=function(format) {varo = { "m+": This. GetMonth () + 1, "D+": This. GetDate (),"H +": This. GetHours (),"m+": This. getminutes (),"S+": This. getseconds (),"q+": Math.floor (( This. GetMonth () + 3)/3), "S": This. getmilliseconds ()}if(/(y+)/. Test (format)) {Format= Format.replace (Regexp.$1, ( This. getFullYear () + ""). substr (4-regexp.$1. length)); } for(varKincho) {if(NewRegExp ("(" + K + ")"). Test (format)) {format= Format.replace (regexp.$1, regexp.$1.length = = 1? O[k]: ("XX" + o[k]). substr ("" +O[k]) (length)); } } returnformat;} Use such as:NewDate (). Format (' Yyyy-mm-dd ');//Echo: ' 2015-12-01 '
2. Get Yesterday (forever relative to the day before today)
// get yesterday's date back yesterday function yesterday () { varnew Date (); var the_yesterday = Today.setdate (Today.getdate ()-1); return The_yesterday;}
3. Get the last one months of today
//get nearly one months back on the one month of todayfunctionLastmonth () {varToday =NewDate (); varmonth =Today.getmonth (); varThe_last_month; if(Month = = 0) {The_last_month= Today.setmonth (11); } Else { varThe_last_month = Today.setmonth (month-1); } the_last_month=NewDate (the_last_month). Format (' Yyyy-mm-dd '); returnThe_last_month;}
4. Get the last week of the specified format returns an array
//get the return array for nearly a week (note: Format here uses the above for the format prototype method)functionLastweek (format) {varToday =NewDate (); varThe_week = []; varFormat = Format | | ' Yyyy-mm-dd '; for(vari = 1; I < 8; i++) { vartemp = Today.setdate (Today.getdate ()-1); Temp=NewDate (temp). Format (format); The_week.unshift (temp); }; returnThe_week;}
5. Get nearly n days to return an array
//get nearly n days to return an arrayfunctionLastnday (days,format) {varToday =NewDate (); varThe_days = []; varFormat = Format | | ' Yyyy-mm-dd '; for(vari = 1; i < days+1; i++) { vartemp = Today.setdate (Today.getdate ()-1); Temp=NewDate (temp). Format (format); The_days.unshift (temp); }; returnthe_days;}//use such asLastnday (7);//returns the date of the last week array format parameter optional//[' 2011-11-01 ', ' 2011-11-02 ', ' 2011-11-03 ', ' 2011-11-04 ', ' 2011-11-05 ', ' 2011-11-06 ', ' 2011-11-07 ']
6. Convert milliseconds to length
//convert milliseconds to hours//returns 1. If less than or equal to 60 seconds, displays the number of seconds//2. If more than 1 minutes less than 1 hours, display minutes//3. If more than 1 hours, display x hours x minutesfunctionMillisecondtotime (MSD) {varTime = parseint (MSD)/1000; if(Time <= 60) { time= time + ' second '; returnTime ; } Else if(Time > && time < 60 * 60) { time= parseint (TIME/60) + "Minutes"; returnTime ; } Else { varhour = parseint (time/3600) + "Hour"; varminute = parseint (parseint (time% 3600)/60) + "minutes"; time= Hour +minute; returnTime ; }}
JS, format and convert dates, minutes, seconds, etc.