In JavaScript development, various types of Date objects are often required to be verified or formatted,
In JavaScript development, various types of Date objects are often required to be verified or formatted. However, js does not provide so many fine functions, so we have to expand it with prototype, the following is a commonly used Date operation method implemented by myself. You can save it as date. js, and then call it in the program.
/*** 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 [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 ("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 ("incorrect time format! "); 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 () 0! = 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 (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 ();}
Address of this article: http://www.nowamagic.net/librarys/veda/detail/561,welcome.