How to get the time difference of date by the granularity of JS

Source: Internet
Author: User

Get Date Type

var type = _xaxis[0].split (/-|:| \s/). length;

Gets the time difference of a date by granularity

    functionGetdatediffbytype (newdate,enddate,type) {newdate=NewDate (newdate) = = ' Invalid date '? Newdate+ ': 00 ': newdate; EndDate=NewDate (endDate) = = ' Invalid date '? Enddate+ ': 00 ': endDate; varDateDiff =NewDate (endDate)-NewDate (newdate); var_date =NewDate (DateDiff); varYear = _date.getfullyear ()-1970; varmonth =_date.getmonth (); Switch(type) { Case1: {                returnYear ;  Break; }             Case2: {                returnMonth+year*12;  Break; }             Case3: {                returnDatediff/1000/60/60/24;  Break; }             Case4: {                returnDATEDIFF/1000/60/60; Break; }             Case5: {                returnDatediff/1000/60;  Break; }             Case6: {                returndatediff/1000; Break; }        }    }   

Attached: date is formatted by time granularity

function Datefmtbytype (_date,_type) {_date = new Date (_date) = = ' Invalid date '? _date+ ': xx ': _date;//"2017-02-12 12" is in Valid Datevar date = new Date (_date), var fmt = ["yyyy", "yyyy-mm", "Yyyy-mm-dd", "Yyyy-mm-dd hh", "Yyyy-mm-dd hh:mm", " Yyyy-mm-dd hh:mm:ss "];return Date.dateformat (Fmt[_type-1]);}

Attached: Date format code ("Yyyy-mm-dd hh:mm:ss")

Date.prototype.dateFormat = function (FMT)   {//author:meizz     var o = {       "m+": This.getmonth () +1,                 //month       "d+": this.getdate (),//                    day       "H +": this.gethours (),                   //hour       "m+": This.getminutes (),       //min "S+": This.getseconds (),                 //sec       "q+": Math.floor ((This.getmonth () +3)/3),//quarterly       "s"  : This.getmilliseconds ()             //msec     };     if (/(y+)/.test (FMT))       Fmt=fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));     For (var k in O)       if (New RegExp ("(" + K + ")"). Test (FMT))     FMT = Fmt.replace (regexp.$1, (regexp.$1.length==1)? (O[k]): (("XX" + o[k]). substr (("" + O[k]).));     return FMT;   

  

How to get the time difference of date by the granularity of JS

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.