JavaScript Date Time Summary

Source: Internet
Author: User

Write this article and summarize the processing of the time format encountered by the front-end JavaScript.

1 C # timestamp processing

The C # time returned from the background is:/date (-62135596800000)/, this is the C # datetime.minvalue; To display in an HTML page, one method is to process the backend into the YYYY-MM-DD HH:mm:ss format, which is displayed directly on the front end. If the backend does not do the processing, you need to do the front-end processing, the following is the front-end processing of this situation.

The code is as follows:

// Description: The C # timestamp, formatted as:/date (-62135596800000), converted to JS time.  //  parameter: TimeSpan string for example: '/date ( -62135596800000) '//  Result: JS's Datevar function (TimeSpan) {    var timespan = Timespan.replace (' Date ', '). Replace (' (', ') '. Replace (') ', '). Replace (/\//g, ");     var New Date (parseint (TimeSpan));     return D;};

2 JS time format processing 2.1 conversion to: YYYY-MM-DD HH:MM:SS format

The code is as follows:

//Description: JS time date format parameter//parameters: formatted string such as: ' Yyyy-mm-dd HH:mm:ss '//results: such as 2016-06-01 10:09:00Date.prototype.Format =function(FMT) {//Author:meizz    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 ()}; varYear = This. getFullYear (); varYEARSTR = year + "; Yearstr= Yearstr.length >= 4? YEARSTR: ' 0000 '. substr (0, 4-yearstr.length) +Yearstr; if(/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, (Yearstr + ""). substr (4-regexp.$1. length));  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;}

2.2 such as: February 08, 1993 after conversion for 08feb93

The code is as follows:

// Description: The date of conversion JS is: // parameter: The date of JS // return: For example: February 08, 1993 after conversion for 08feb93 var function (d) {    var array = d.todatestring (). Split (');     var str = array[2]+array[1]+array[3].substr (2,2);     return str.touppercase ()}

3 Common JS's date function

42 Time Subtraction 4.1 Two date subtraction-seconds

The code is as follows:

//Description: Two time subtraction//parameter: JS date type, or string type, in the format: Yyyy-mm-dd HH:mm:ss//return: Number of seconds Date1-date2varSubstractdate =function(Date1, date2) {varType1 =typeofdate1; varType2 =typeofDate2; if(Type1 = = ' String ') {date1=NewDate (date1); }    if(type2 = = ' String ') {Date2=NewDate (DATE2); }    return(DATE1-DATE2)/1000;}

Test results,:

According to mathematical knowledge: 1 days = 24 hours 1 hours = 60 Minutes 1 minutes = 60 seconds to deduce, the difference in minutes, hours, days 4.2 Two date subtraction--month

Two months of the date difference, can not be simple to 1 month how many days to calculate, because some months have 30 days, some have 31 days. So this is the way to calculate. The calculation of the difference year can be referred to in the following way.

The code is as follows:

varGetdiffmonths =function(Date1, date2) {if(!date1instanceofDate) {Console.error (' Param date1 is not Date '); }    if(!date2instanceofDate) {Console.error (' Param date2 is not Date '); }        varMONTHS1 = Date1.getfullyear () * 12 +Date1.getmonth (); varMONTHS2 = Date2.getfullyear () * 12 +Date2.getmonth (); returnMONTHS1-MONTHS2;}

Test results,:

4 time added 4.1 Two dates added--Day

The code is as follows:

// Description: Add days // parameters: Days such as 40 days // results: For example date: 2016-16-13, plus 40 days, the result is: 2016-07-23 function {days} {    varnew Date (this);      + days );      return date;}
Add the month, year, and reference to the above code.

JavaScript Date Time Summary

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.