Several time format processing commonly used in JS-"notes finishing"

Source: Internet
Author: User

Here to organize the usual time format processing method

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

1 //Time Formatting functions2Date.prototype.format =function(format) {3     varo = {4"m+": This. GetMonth () + 1,//Month5"D+": This. GetDate (),// Day6"H +": This. GetHours (),//Hour7"m+": This. getminutes (),//minute8"S+": This. getseconds (),//Second9"q+": Math.floor (( This. GetMonth () + 3)/3),//QuarterTen"S": This. Getmilliseconds ()//Millisecond One     } A     if(/(y+)/. Test (format)) { -Format = Format.replace (Regexp.$1, ( This. getFullYear () + ""). substr (4-regexp.$1. length)); -     } the      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; +}

1 //var time = new Date () can be converted to 2016-08-03 18:30:00 format//Eliminate differences between browsers2 functionDatehandle (objdate) {3Objdate =NewDate ();//creates a Date object that represents the current time4     varYear = Objdate.getfullyear ();//Four-digit year5     varmonth = Objdate.getmonth () + 1;//GetMonth () The month returned is 0, plus 1 .6     varDate =objdate.getdate ();7     varHours =objdate.gethours ();8     varminutes =objdate.getminutes ();9     varseconds =objdate.getseconds ();Ten     varDate = year + "-" + month + "-" + Date + "" + Hours + ":" + minutes + ":" +seconds; One     returndate; A}
1 //+---------------------------------------------------  2 //| String to date type3 //| format mm/dd/yyyy mm-dd-yyyy YYYY/MM/DD yyyy-mm-dd4 //+---------------------------------------------------  5 functionstringtodate (datestr) {6 7     varconverted =Date.parse (DATESTR);8     varMyDate =NewDate (converted);9     if(IsNaN (mydate)) {Ten         varArys = Datestr.split ('-')); OneMyDate =NewDate (Arys[0],--arys[1], arys[2]); A     } -     returnmydate; -}
1 //+---------------------------------------------------  2 //| Comparison date Difference dtend format is a date type or a valid date format string3 //+---------------------------------------------------  4Date.prototype.DateDiff =function(Strinterval, dtend) {5     varDtstart = This;6     if(typeofDtend = = ' String ')//if the string is converted to a date type7     {8Dtend =stringtodate (dtend);9     }Ten     Switch(strinterval) { One          Case' s ':returnparseint ((Dtend-dtstart)/1000); A          Case' N ':returnparseint ((dtend-dtstart)/60000); -          Case' H ':returnparseint ((dtend-dtstart)/3600000); -          Case' d ':returnparseint ((dtend-dtstart)/86400000); the          Case' W ':returnparseint ((Dtend-dtstart)/(86400000 * 7)); -          Case' m ':return(Dtend.getmonth () + 1) + ((Dtend.getfullyear ()-dtstart.getfullyear ()) * (Dtstart.getmonth () + 1); -          Case' Y ':returnDtend.getfullyear ()-dtstart.getfullyear (); -     } +}
1 //time conversion//plus digital color/style
Time Unit seconds
2 functionSecondtodateandstyle (time) {3Time =parseint (time);4 if(NULL! = Time && "! =Time ) {5 if(Time > && time < 60 * 60) {6Time = "<span style=\" color: #FF6600, \ ">" + parseint (time/60.0) + "</span>" + "min" + "<span style=\" color:# Ff6600;\ ">" + parseint ((parsefloat (time/60.0)-parseint (time/60.0)) + "</span>" + "Seconds";7}Else if(Time >= * && Time < 60 * 60 * 24) {8Time = "<span style=\" color: #FF6600, \ ">" + parseint (time/3600.0) + "</span>" + "when" + "<span style=\" color : #FF6600; \ ">" + parseint ((parsefloat (time/3600.0)-parseint (time/3600.0)) + "</span>" + "min" + "<spa n style=\ "color: #FF6600; \" > "+ parseint ((Parsefloat ((parsefloat (time/3600.0)-parseint (time/3600.0))-Parse Int ((parsefloat (time/3600.0)-parseint (time/3600.0)) *) + "</span>" + "Seconds";9}Else {TenTime = "<span style=\" color: #FF6600; \ ">" + parseint (time) + "</span>" + "Seconds"; One } A}Else { -Time = "<span style=\" color: #FF6600; \ ">0</span> Seconds"; - } the returnTime ; -}

Continuous finishing add-in

Cond...

Several time format processing commonly used in JS-"notes finishing"

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.