Simple talk about JavaScript date type _javascript tips

Source: Internet
Author: User
Tags getdate

1 Create a new Date object that automatically obtains the current date and time if no arguments are taken

var d = new Date ()

2 If you need to specify a specific date, you can either pass Date.parse () or date (). UTC (), returning the timestamp as a parameter to new Date ()

Date.parse () Usage:

var time = date.parse (' 2015/05/20 ');
var newdate = new Date,//wed May 2015 00:00:00 gmt+0800 (China Standard Time)
//Convert to GMT
newdate.toutcstring ();//tu E, May 2015 16:00:00 GMT

You can also specify a date directly from new date (' 2015/05/20 '), and the new date () constructor automatically calls the Date.parse () static method.

DATE.UTC ()

The parameters of DATE.UTC () are year, month (from 0 to 11), Day (1-31), Time (0-23), Minute (0-59), second (0-59), milliseconds (0-999), minimum parameter 2, which should include years, and other not filled defaults to 0.

If the time to be created is May 20, 2015 of the Chinese standard Time, the code should be represented as

var mydate = new Date (DATE.UTC (2015,5,19,16,0,0))//sat June 2015 00:00:00 gmt+0800
/GMT
Mydate.toutcstring ()//Fri, June 2015 16:00:00 GMT

Other:

var d = new Date ();
Year
d.getfullyear () 
//month
d.getmonth ()//
day
d.getdate ()

Here's a concrete example to see

/** * Date Time Script Library method list: * (1) Date.isvalidate: Date Legality verification * (2) Date.isvalitime: Time Validity Verification * (3) Date.isvalidatetime: Validation of date and time legality * (4) Date.prototype.isLeapYear: Judge whether Leap year * (5) Date.prototype.format: Date format * (6) Date.stringtodate: string to date Type * (7) date. 
 Daysbetween: Count Two days Difference * (8) DATE.PROTOTYPE.DATEADD: Date calculation, support positive negative * (9) Date.prototype.dateDiff: compare date difference: Compare two times the same field, return the difference value * (a) Date.prototype.toArray: the date is divided into groups: according to the array ordinal number: time and minutes * (one) Date.prototype.datePart: Get Date data information//** * Date legality verification: Judge Da TASTR conformance to date format specified by FORMATSTR * Example: * (1) alert (date.isvalidate (' 2008-02-29 ', ' yyyy-mm-dd '));//true * (2) alert (Date.isvali Date (' aaaa-58-29 ', ' yyyy-mm-dd '));//false * Datestr: Required, date String * formatstr: Optional, format string, optional format: (1) YYYY-MM-DD (default format) or YYYY-MM-DD (2) Yyyy/mm/dd or YYYY/MM/DD (3) mm-dd-yyyy or MM-DD-YYYY (4) mm/dd/yyyy or mm/dd/yyyy/date.isvalidate = function ( 
  Datestr, Formatstr) {if (!DATESTR) {return false; } if (!formatstr) {formatstr = "yyyy-mm-dd";//Default format: Yyyy-mm-dd} if (datestr.length!=forMatstr.length) {return false; }else{if (formatstr== "Yyyy-mm-dd" | | formatstr== "Yyyy-mm-dd") {var r1=/^ ((([02468][048]) | ( [13579] [26])) (00)) | (\d{2} ([02468][48]) | ( [13579] [26]))) \-(((0[13578)) | (1[02]) \-([0-2][0-9]) | (3[01])) | (((0[469]) | (11)) \-([0-2][0-9]) | (30)) | (02\-([0-2][0-9])) | (\d{2} ([02468][1235679]) | ( [13579] [01345789])) \-(((0[13578)) | (1[02]) \-([0-2][0-9]) | (3[01])) | (((0[469]) | (11)) \-([0-2][0-9]) | (30)) | (02\-([0-1][0-9]) | ( 
      2[0-8]))) $/; 
    Return R1.test (DATESTR); }else if (formatstr== "Yyyy/mm/dd" | | formatstr== "Yyyy/mm/dd") {var r2=/^ ((([02468][048]) | ( [13579] [26])) (00)) | (\d{2} ([02468][48]) | ( [13579] [26]))) \/(((0[13578)) | (1[02]) \/([0-2][0-9]) | (3[01])) | (((0[469]) | (11)) \/([0-2][0-9]) | (30)) | (02\/([0-2][0-9])) | (\d{2} ([02468][1235679]) | ( [13579] [01345789])) \/(((0[13578)) | (1[02]) \/([0-2][0-9]) | (3[01])) | (((0[469]) | (11)) \/([0-2][0-9]) | (30)) | (02\/([0-1][0-9]) | ( 
      2[0-8]))) $/; 
    Return R2.test (DATESTR); }else if (formatstr== "Mm-dd-yyyy" | | formatstr== "mm-dd-yyyy") {var r3=/^ ((((0[13578)) | 1[02]) \-([0-2][0-9]) | ( 3[01])) | (((0[469]) | (11)) \-([0-2][0-9]) | (30)) | (02\-([0-2][0-9])) \-(([02468][048]) | ([13579][26]) (00)) | (\d{2} ([02468][48]) | ( [13579] [26])) | ((((0[13578]) | (1[02]) \-([0-2][0-9]) | (3[01])) | (((0[469]) | (11)) \-([0-2][0-9]) | (30)) | (02\-([0-1][0-9]) | ( 2[0-8])) \-\d{2} ([02468][1235679]) | ( [13579] [01345789])) 
      $/; 
    Return R3.test (DATESTR); }else if (formatstr== "mm/dd/yyyy" | | formatstr== "mm/dd/yyyy") {var r4=/^ ((((0[13578)) | 1[02]) \/([0-2][0-9]) | ( 3[01])) | (((0[469]) | (11)) \/([0-2][0-9]) | (30)) | (02\/([0-2][0-9])) \/(([02468][048]) | ([13579][26]) (00)) | (\d{2} ([02468][48]) | ( [13579] [26])) | ((((0[13578]) | (1[02]) \/([0-2][0-9]) | (3[01])) | (((0[469]) | (11)) \/([0-2][0-9]) | (30)) | (02\/([0-1][0-9]) | ( 2[0-8])) \/\d{2} ([02468][1235679]) | ( [13579] [01345789])) 
      $/; 
    Return R4.test (DATESTR); }else{alert ("date format is not correct!") 
      "); 
    return false; } RETUrn false; /** * Time Validity verification: Determine if TIMESTR conforms to formatstr specified time format * Example: * (1) alert (date.isvalitime (' 23:59:59 ', ' hh:mm:ss '));//true * (2) Alert (date.isvalitime (' 24-68-89 ', ' hh:mm:ss '));//false * Timestr: Required, date String * formatstr: Optional, format string, optional format: (1) hh:mm:ss (default format) 
  (2) Hh-mm-ss (3) hh/mm/ss * * date.isvalitime = function (Timestr, formatstr) {if (!TIMESTR) {return false;  } if (!formatstr) {formatstr = "hh:mm:ss";//Default format: Hh:mm:ss} if (timestr.length!=formatstr.length) {return 
  False }else{if (formatstr== "Hh:mm:ss") {var r1=/^ ([0-1][0-9]) | ( 
      2[0-3]) \:([0-5][0-9]) \:([0-5][0-9]) $/; 
    Return R1.test (TIMESTR); }else if (formatstr== "Hh-mm-ss") {var r2=/^ ([0-1][0-9]) | ( 
      2[0-3]) \-([0-5][0-9]) \-([0-5][0-9]) $/; 
    Return R2.test (TIMESTR); }else if (formatstr== "Hh/mm/ss") {var r3=/^ ([0-1][0-9]) | ( 
      2[0-3]) \/([0-5][0-9]) \/([0-5][0-9]) $/; 
    Return R3.test (TIMESTR); }else{alert ("Time format is not correct!") 
      "); 
    return false; } 
  return false; /** * Date and time legality validation * format: YYYY-MM-DD hh:mm:ss/date.isvalidatetime = function (datetimestr) {var datetimereg= /^ ((([02468][048]) | ([13579][26]) (00)) | (\d{2} ([02468][48]) | ( [13579] [26]))) \-(((0[13578)) | (1[02]) \-([0-2][0-9]) | (3[01])) | (((0[469]) | (11)) \-([0-2][0-9]) | (30)) | (02\-([0-2][0-9])) | (\d{2} ([02468][1235679]) | ( [13579] [01345789])) \-(((0[13578)) | (1[02]) \-([0-2][0-9]) | (3[01])) | (((0[469]) | (11)) \-([0-2][0-9]) | (30)) | (02\-([0-1][0-9]) | ( 2[0-8]))) (\s{1} ([0-1][0-9]) | ( 
2[0-3]) \:([0-5][0-9]) \:([0-5][0-9])? $/return Datetimereg.test (DATETIMESTR); 
 /** * Judgment Leap Year: General law: Four years a leap, hundred years not a leap, 400 year again leap. */Date.prototype.isLeapYear = function () {return (This.getyear ()%4==0&& (This.getyear ()%100!=0) | | 
(This.getyear ()%400==0)); 
 /** * Date Format: * formatstr: Optional, format string, default format: Yyyy-mm-dd HH:MM:SS * The following format is stipulated: * (1) yyyy/yyyy/yy/yy indicates year * (2) mm/m Month 
* (3) w/w Week * (4) dd/dd/d/d date * (5) hh/hh/h/h time * (6) mm/m minutes * (7) ss/ss/s/s seconds * (8) III milliseconds */Date.prototype.format = function (formatstr) {var str = FORMATSTR; 
   
  if (!formatstr) {str = ' yyyy-mm-dd hh:mm:ss ';/default format} var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ']; Str=str.replace (/yyyy| 
  Yyyy/,this.getfullyear ()); Str=str.replace (/yy| yy/, (this.getyear ()%) >9? 
   
  (This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100)); Str=str.replace (/mm/,this.getmonth () >=9? ( 
  parseint (This.getmonth ()) +1). ToString (): ' 0 ' + (parseint (This.getmonth ()) +1)); 
   
  Str=str.replace (/m/g, (parseint (This.getmonth ()) +1)); Str=str.replace (/w| 
   
  W/g,week[this.getday ()]); Str=str.replace (/dd| 
  Dd/,this.getdate () >9?this.getdate (). toString (): ' 0 ' + this.getdate ()); Str=str.replace (/d| 
   
  D/g,this.getdate ()); Str=str.replace (/hh| 
  Hh/,this.gethours () >9?this.gethours (). toString (): ' 0 ' + this.gethours ()); Str=str.replace (/h| 
  H/g,this.gethours ()); Str=str.replace (/mm/,this.getminutes () >9?this.getminutes (). toString (): ' 0 ' + this.getminutes ()); 
   
  Str=str.replace (/m/g,this.getminutes ()); Str=str.replace (/ss| 
  Ss/,this.getseconds () >9?this.getseconds (). toString (): ' 0 ' + this.getseconds ()); Str=str.replace (/s| 
   
  S/g,this.getseconds ()); Str=str.replace (/iii/g,this.getmilliseconds () <10? ' ' +this.getmilliseconds ():(this.getmilliseconds () <100? ' 
   
  0 ' +this.getmilliseconds (): This.getmilliseconds ()); 
return str;  
 /** * String to date type: * DATESTR: Required, date string, if unable to resolve to date type, returns NULL * format: * (1) Yyyy/mm/dd:ie and FF General * (2) Mm/dd/yyyy:ie and FF General 
    * (3) mm-dd-yyyy: Only IE * (4) YYYY-MM-DD: Non ie, and the clock is resolved at 8 o'clock the whole */date.stringtodate = function (datestr) {if (!DATESTR) { 
    Alert ("string cannot resolve to date"); 
  return null; }else{if (date.isvalidate (Datestr, "Yyyy/mm/dd") | | 
    Date.isvalidate (Datestr, "mm/dd/yyyy")) {return new Date (Date.parse (DATESTR)); }else{if ((!-[1,]) {//ie if (date.isvalidate (Datestr, "mm-dd-yyyy")) {return new Date (Date.parse ( 
        DATESTR)); }else{alert ("String cannot be solvedAnalysis for date "); 
        return null; 
        }else{//Non IE if (date.isvalidate (Datestr, "Yyyy-mm-dd")) {return new Date (Date.parse); 
          }else{alert ("string cannot resolve to date"); 
        return null; 
}}} return null; /** * Calculates the number of days difference for two dates: * Dateone: Required, must be an instance of the data type * Datetwo: required, must be an instance of the data type * * Date.daysbetween = function (Dateone, Datetwo) {if (Dateone instanceof Date) ==false| | 
  (Datetwo instanceof Date) ==false) {return 0; 
  }else{return Math.Abs (Math.floor (Dateone.gettime ()-datetwo.gettime ())/1000/60/60/24)); }/** * Date calculation: Support negative numbers, you can add and subtract, return the calculated date * num: Required, must be a number, and positive is a period plus, negative is the date minus * field: optional, the identification is in which field to add or subtract, the field see the following convention.  
 Without this parameter, the default is D * agreement to the following format: * (1) y/y Year * (2) M Month * (3) w/w Week * (4) d/d Day * (5) h/h (6) M (7) s/s seconds * (8) q/q season */DATE.PROTOTYPE.DATEADD = function (num, field) {if (!num) | | isNaN (num) | | 
  parseint (num) ==0) {return this; 
 } if (!field) {field = ' d '; Switch (field) {case ' Y ': Case ' y ': Return new Date ((This.getfullyear () +num), This.getmonth (), This.getdate ( 
    ), This.gethours (), This.getminutes (), this.getseconds ()); Case ' Q ': Case ' Q ': return new Date (This.getfullyear (), (This.getmonth () +num*3), This.getdate (), this.gethours (), this 
    . getminutes (), this.getseconds ()); Case ' M ': return new Date (This.getfullyear (), This.getmonth () +num, This.getdate (), this.gethours (), This.getminutes (), 
    This.getseconds ()); 
    Case ' W ': Case ' W ': Return new Date (Date.parse (This) + ((86400000 * 7) * num)); 
    Case ' d ': Case ' d ': Return to New Date (Date.parse (this) + (86400000 * num)); 
    Case ' h ': Case ' h ': return new Date (Date.parse (this) + (3600000 * num)); 
    Case ' m ': Return to New Date (Date.parse (this) + (60000 * num)); 
    Case "s": Case ' s ': Return to New Date (Date.parse (this) + (1000 * num)); 
  Default:return this; 
return to this; }/** * Comparison date difference: ratioMore than two periods the same field, return the difference value * Dtend: Required, must be an instance of the data type * field: Optional, identifies which field to compare with, fields see the following conventions. Without this parameter, the default is D * conventions as follows: * (1) y/y Year * (2) M Month * (3) w/w Week * (4) d/d Day * (5) h/h * (6) M (7) s/s sec/DATE.P 
  Rototype.datediff = function (dtend, field) {var dtstart = this; 
  if ((dtend instanceof Date) ==false) {return 0; 
    }else{if (!field) {field = ' d '; 
      Switch (field) {case ' Y ': Case ' y ': return dtend.getfullyear ()-dtstart.getfullyear (); Case ' M ': Return (Dtend.getmonth () +1) + ((Dtend.getfullyear ()-dtstart.getfullyear ()) *12)-(Dtstart.getmonth () +1); 
      Break 
      Case ' W ': Case ' W ': Return parseint (Dtend-dtstart)/(86400000 * 7)); 
      Case ' d ': Case ' d ': return parseint (Dtend-dtstart)/86400000); 
      Case ' h ': Case ' h ': return parseint (Dtend-dtstart)/3600000); 
      Case ' m ': Return parseint ((Dtend-dtstart)/60000); Case "s": Case ' s ': Return parseint (Dtend- Dtstart)/1000); 
    Default:return 0; 
  return 0; 
  }/** * divides dates into groups: According to the array ordinal number: Month/day time seconds/Date.prototype.toArray = function () {var myarray = new Array (); 
  Myarray[0] = This.getfullyear (); 
  MYARRAY[1] = This.getmonth (); 
  MYARRAY[2] = This.getdate (); 
  MYARRAY[3] = this.gethours (); 
  MYARRAY[4] = This.getminutes (); 
  MYARRAY[5] = This.getseconds (); 
return myarray; /** * Get Date data information: * Field: Optional, the identification is in which field to compare, the field see the following convention. Without this parameter, the default is D * (1) y/y years * (2) M-month * (3) w/w Week * (4) d/d Day * (5) h/h * (6) M (7) s/s sec/Date.prototype.dat 
  Epart = function (field) {if (!field) {field = ' d '; 
  var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ']; 
    Switch (field) {case ' Y ': Case ' y ': return this.getfullyear (); 
    Case ' M ': Return (This.getmonth () +1); 
    Case ' W ': Case ' W ': Return Week[this.getday ()];break; 
    Case ' d ': Case ' d ': return This.getdate (); Case ' h ': Case ' h ': returnThis.gethours (); 
    Case ' m ': return This.getminutes (); 
    Case ' s ': return This.getseconds (); 
  Default:return this.getdate (); 
return This.getdate (); 
 }

Related Article

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.