/*** 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 (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 [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 indicates the year * (2) mm/M month * (3) w/w weeks * (4) DD/D/d date * (5) hh/h time * (6) mm/M min * (7) SS/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 (dateone, d Atetwo) {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. getdate (); 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 ();}