Let's talk about the javascript Date type and javascriptdate.

Source: Internet
Author: User

Let's talk about the javascript Date type and javascriptdate.

1. Create a new date object. If no parameter is included, the object automatically obtains the current date and time.

var d = new Date()

2 If you need to specify a specific Date, you can use Date. parse () or Date (). UTC () to return the timestamp as a parameter for new Date ().

Date. parse () usage:

Var time = Date. parse ('2014/1/20'); var newDate = new Date (time); // Wed May 20 2015/05 00:00:00 GMT + 2015 (China Standard time) // convert to Greenwich Mean Time newDate. toUTCString (); // Tue, 19 May 2015 16:00:00 GMT

You can also specify a Date directly by using new Date ('2014/1/20'). The new Date () constructor automatically calls the Date. parse () static method.

Date. UTC ()

Date. the UTC () parameters are respectively year, month (from 0 to 11), Day (1-31), hour (0-23), minute (0-59 ), second (0-59), Millisecond (0-999), minimum parameter 2, that is, it should contain the year and month, other default value is 0.

If the time to be created is January 1, May 20, 2015 of the Chinese Standard Time, the code should be represented

Var myDate = new Date (Date. UTC (2015, 0800,) // Sat Jun 20 00:00:00 GMT + // Greenwich Mean Time myDate. toUTCString () // Fri, 19 Jun 2015 16:00:00 GMT

Others:

Var d = new Date (); // year d. getFullYear () // month d. getMonth () // day d. getDate ()

Let's take a look at the specific example below

/*** Date and Time script library method list: * (1) Date. isValiDate: Date validity verification * (2) Date. isValiTime: time validity verification * (3) Date. isValiDateTime: Date and time validity verification * (4) Date. prototype. isLeapYear: determines whether a leap year * (5) Date. prototype. format: format the Date * (6) Date. stringToDate: Convert string to Date type * (7) Date. daysBetween: calculates the number of days difference between two dates * (8) Date. prototype. dateAdd: Date calculation, supporting positive and negative numbers * (9) Date. prototype. dateDiff: Compare Date difference: compare two fields with the same period and return the difference * (10) Date. prototype. toArray: divide the date into an array: By the array sequence number: year, month, day, hour, minute, second * (11) Date. prototype. datePart: Obtain Date data information * // *** verify Date validity: Determine whether dataStr meets the Date format specified by formatStr * example: * (1) alert (Date. isValiDate ('1970-02-29 ', 'yyyy-MM-dd'); // true * (2) alert (Date. isValiDate ('aaaa-58-29 ', 'yyyy-MM-dd'); // false * dateStr: required, date string * formatStr: optional, format string, optional formats include: (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 (dateSt R, 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 [1357 ]) 8]) | (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]) | ([1, 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 ("the date format is incorrect! "); Return false ;}} return false;}/*** time validity verification: checks whether timeStr meets the time format specified by formatStr * 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 formats: (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 ("the time format is incorrect! "); Return false ;}} return false;}/*** Date and time validity verification * 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);}/*** determines a leap year. The general rule is: Four years, one hour, one year without renewal, and four hundred years without renewal. */Date. prototype. isLeapYear = function () {return (this. getYear () % 4 = 0 & (this. getYear () % 100! = 0) | (this. getYear () % 400 = 0);}/*** format Date: * formatStr: optional, Format String, default format: yyyy-MM-dd hh: mm: ss * Conventions: * (1) YYYY/yyyy/YY/yy indicates the year * (2) MM/M month * (3) W/w weeks * (4) dd/DD/d/D date * (5) hh/HH/h/H time * (6) mm/m min * (7) ss/SS/s/S seconds * (8) iii millisecond */Date. prototype. format = function (formatStr) {var str = formatStr; if (! FormatStr) {str = "yyyy-MM-dd hh: mm: ss"; // default format} var Week = ['day', 'yi', '2 ', '3', '4', '5', '6']; str = str. replace (/yyyy | YYYY/, this. getFullYear (); str = str. replace (/yy | YY/, (this. getYear () % 100)> 9? (This. getYear () % 100 ). 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? '00' + this. getMilliseconds () :( this. getMilliseconds () <100? '0' + this. getMilliseconds (): this. getMilliseconds (); return str;}/*** convert string to date type: * dateStr: required, date string. If it cannot be parsed to date type, return 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 parsed at eight o'clock */Date. stringToDate = function (dateStr) {if (! DateStr) {alert ("string cannot be parsed as 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 parsed as Date"); return null ;}} else {// non-IE if (Date. isValiDate (dateStr, "yyyy-MM-dd") {return new Date (Date. parse (dateStr);} else {alert ("string cannot be parsed as date"); return null ;}}} return null ;} /*** calculate the number of days difference between two dates: * dateOne: required. It must be a Data-type instance * dateTwo: required. It must be a Data-type instance */Date. daysBetween = function (dateOn E, 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: a negative number is supported. You can add or subtract the number. The calculated date * num is returned. required, it must be a number, a positive value is a period addition, and a negative value is a date minus * field: (optional) It indicates the field on which the data is added or subtracted. For the fields, see the following conventions. If this parameter is not set, the default format is d *, which is in the following format: * (1) Y/y * (2) M month * (3) W/w week * (4) d/d * (5) H/h * (6) m minutes * (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': return new Date (this. getFullYear () + num), this. getMonth (), this. getDate (), this. getHours (), this. getMinutes (), this. getSeconds (); break; case 'q': return new Date (this. getFullYear (), (this. getMonth () + num * 3), this. getDate (), this. getHours (), this. getMinutes (), this. getSeconds (); break; case 'M': return new Date (this. getFullYear (), this. GetMonth () + num, this. getDate (), this. getHours (), this. getMinutes (), this. getSeconds (); break; case 'W': return new Date (Date. parse (this) + (86400000*7) * num); break; case 'D': return new Date (Date. parse (this) + (86400000 * num); break; case 'H': return new Date (Date. parse (this) + (3600000 * num); break; case 'M': return new Date (Date. parse (this) + (60000 * num); brea K; case's ': return new Date (Date. parse (this) + (1000 * num); break; default: return this;}/*** compare date difference: compare two fields with the same period, returns the difference value * dtEnd: required. It must be a Data-type instance * field: (optional) indicates the field on which the instance is compared. For the fields, see the following conventions. If this parameter is not set, the default format is d *, which is in the following format: * (1) Y/y * (2) M month * (3) W/w week * (4) d/d * (5) H/h * (6) m minutes * (7) S/s seconds */Date. prototype. dateDiff = function (dtEnd, field) {var dtStart = this; if (dtEnd instanceof Date) = false) {return 0;} else {if (! Field) {field = "d";} switch (field) {case 'y': return dtEnd. getFullYear ()-dtStart. getFullYear (); break; case 'M': return (dtEnd. getMonth () + 1) + (dtEnd. getFullYear ()-dtStart. getFullYear () * 12)-(dtStart. getMonth () + 1); break; case 'W': return parseInt (dtEnd-dtStart)/(86400000*7); break; case 'D': return parseInt (dtEnd-dtStart)/86400000); break; case 'H': case 'H': Return parseInt (dtEnd-dtStart)/3600000); break; case 'M': return parseInt (dtEnd-dtStart)/60000); break; case's ': case's ': return parseInt (dtEnd-dtStart)/1000); break; default: return 0;} return 0 ;}/ *** splits the date into an array: by array sequence number: year, month, day, hour, minute, second */Date. prototype. toArray = function () {var myArray = new Array (); myArray [0] = this. getFullYear (); myArray [1] = this. getMonth (); myArray [2] = this. getDa Te (); myArray [3] = this. getHours (); myArray [4] = this. getMinutes (); myArray [5] = this. getSeconds (); return myArray;}/*** get date data information: * field: (optional) indicates the field on which the data is compared. For the fields, see the following conventions. If this parameter is not set, the default value is d * (1) Y/y * (2) M month * (3) W/w weeks * (4) d/d * (5) H/h * (6) m minutes * (7) S/s seconds */Date. prototype. datePart = function (field) {if (! Field) {field = "d";} var Week = ['day', 'yi', '2', '3', '4', '5 ', '6']; switch (field) {case 'y': return this. getFullYear (); break; case 'M': return (this. getMonth () + 1); break; case 'W': return Week [this. getDay ()]; break; case 'D': return this. getDate (); break; case 'H': return this. getHours (); break; case 'M': return this. getMinutes (); break; case's ': return this. getSeconds (); break; 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.