Js method for obtaining time functions and extended functions built-in Date functions
Var mydate = new Date ();
Mydate. getYear (); // obtain the current year (2 digits)
Mydate. getFullYear (); // obtain the complete year (4 bits, 1970 -????)
Mydate. getMonth (); // obtain the current month (0-11, 0 represents January)
Mydate. getDate (); // obtain the current day (1-31)
Mydate. getDay (); // obtain X of the current week (0-6, 0 indicates Sunday)
Mydate. getTime (); // obtain the current time (milliseconds starting from 1970.1.1)
Mydate. getHours (); // obtain the current hour (0-23)
Mydate. getMinutes (); // obtain the current number of minutes (0-59)
Mydate. getSeconds (); // obtain the current number of seconds (0-59)
Mydate. getMilliseconds (); // obtain the current number of milliseconds (0-999)
Mydate. toLocaleDateString (); // obtain the current date
Var mytime = mydate. toLocaleTimeString (); // obtain the current time
Mydate. toLocaleString (); // obtain the date and time
Extended Functions
Date. prototype. isLeapYear
Date. prototype. Format
Date. prototype. DateAdd Date calculation
Date. prototype. DateDiff compare Date difference
Date. prototype. toString Date to string
Date. prototype. toArray Date is split into Arrays
Date. prototype. DatePart obtains part of the Date information.
Date. prototype. MaxDayOfDate indicates the maximum number of days in the month where the Date is located.
Date. prototype. WeekNumOfYear determine the week of the year where the Date is located
StringToDate string to date type
IsValidDate verification date validity
CheckDateTime full Date and Time check
DaysBetween date day difference
// ----------------------------------------------------- // Determine a leap year // --------------------------------------------------- Date. prototype. isLeapYear = function () {return (0 = this. getYear () % 4 & (this. get year () % 100! = 0) | (this. getYear () % 400 = 0 )));} // ------------------------------------------------- // format YYYY/yyyy/YY indicates the year // MM/M month // W/w weeks // dd/d/D date // hh/HH/h/H time // mm/m minutes // ss/SS/s/S seconds // --------------------------------------------------------- Date. prototype. format = function (formatStr) {var str = formatStr; var Week = ['day', 'yi', '2', '3', '4', '5 ', '6']; str = str. replace (/yyyy | YYYY/, this. getFullYear (); s Tr = str. replace (/yy | YY/, (this. getYear () % 100)> 9? (This. getYear () % 100 ). toString (): '0' + (this. getYear () % 100); str = str. replace (/MM/, this. getMonth ()> 9? This. getMonth (). toString (): '0' + this. getMonth (); str = str. replace (/M/g, this. getMonth (); 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 (); return str;} // + --------------------------------------------------- // | returns the date format of the two days difference: YYYY-MM-dd // + interval function daysBetween (DateOne, dateTwo) {var OneMonth = DateOne. substring (5, DateOne. lastIndexOf ('-'); var OneDay = DateOne. substring (DateOne. length, DateOne. LastIndexOf ('-') + 1); var OneYear = DateOne. substring (0, DateOne. indexOf ('-'); var TwoMonth = DateTwo. substring (5, DateTwo. lastIndexOf ('-'); var TwoDay = DateTwo. substring (DateTwo. length, DateTwo. lastIndexOf ('-') + 1); var TwoYear = DateTwo. substring (0, DateTwo. indexOf ('-'); var cha = (Date. parse (OneMonth + '/' + OneDay + '/' + OneYear)-Date. parse (TwoMonth + '/' + TwoDay + '/' + TwoYear)/86400000); return Math. abs (Cha);} // + --------------------------------------------------- // | Date calculation // + ------------------------------------------------------- Date. prototype. dateAdd = function (strInterval, Number) {var dtTmp = this; switch (strInterval) {case's ': return new Date (Date. parse (dtTmp) + (1000 * Number); case 'N': return new Date (Date. parse (dtTmp) + (60000 * Number); case 'H': return new Date (Date. parse (dtTmp) + (3600000 * Number); case 'D': return new Date (Date. parse (dtTmp) + (86400000 * Number); case 'W': return new Date (Date. parse (dtTmp) + (86400000*7) * Number); case 'q': return new Date (dtTmp. getFullYear (), (dtTmp. getMonth () + Number * 3, dtTmp. getDate (), dtTmp. getHours (), dtTmp. getMinutes (), dtTmp. getSeconds (); case 'M': return new Date (dtTmp. getFullYear (), (dtTmp. getMonth () + Number, dtTmp. getDate (), dt Tmp. getHours (), dtTmp. getMinutes (), dtTmp. getSeconds (); case 'y': return new Date (dtTmp. getFullYear () + Number), dtTmp. getMonth (), dtTmp. getDate (), dtTmp. getHours (), dtTmp. getMinutes (), dtTmp. getSeconds () ;}/// + --------------------------------------------------- // | compare the dtEnd format with the Date format or the valid Date format string // + invalid Date. prototype. dateDiff = function (strInterval, DtEnd) {var dtStart = this; if (typeof dtEnd = 'string') // if the string is converted to the date type {dtEnd = StringToDate (dtEnd);} switch (strInterval) {case's ': return parseInt (dtEnd-dtStart)/1000); case 'N': return parseInt (dtEnd-dtStart)/60000); case 'H ': return parseInt (dtEnd-dtStart)/3600000); case 'D': return parseInt (dtEnd-dtStart)/86400000); case 'W ': return parseInt (dtEnd-dtStart)/(86400000*7 )); Case 'M': return (dtEnd. getMonth () + 1) + (dtEnd. getFullYear ()-dtStart. getFullYear () * 12)-(dtStart. getMonth () + 1); case 'y': return dtEnd. getFullYear ()-dtStart. getFullYear () ;}/// + ----------------------------------------------------- // | Date output string, reload the system's toString method // + 1_date. prototype. toString = function (showWeek) {var myDate = this; var str = myDate. toLocale DateString (); if (showWeek) {var Week = ['day', 'yi', '2', '3', '4', '5 ', '6']; str + = 'Week' + Week [myDate. getDay ()];} return str;} // + --------------------------------------------------- // | date validity verification // | format: YYYY-MM-DD or YYYY/MM/DD // + ----------------------------------------------------- function IsValidDate (DateStr) {var sDate = DateStr. replace (/(^ \ s + | \ s + $)/g, ''); // remove spaces on both sides; if (sDate ='') return true; // if the format is YYYY-(/) MM-(/) D D or YYYY-(/) M-(/) DD Or YYYY-(/) M-(/) D or YYYY-(/) MM -(/) D is replaced with ''// database, the valid date can be: YYYY-MM/DD (2003-3/21), the database will be automatically converted to the YYYY-MM-DD format var s = sDate. replace (/[\ d] {4, 4} [\-/] {1} [\ d] {1} [\-/] {1} [\ d] {1, 2} /g, ''); if (s ='') // The description format meets the YYYY-MM-DD or YYYY-M-DD or YYYY-M-D or YYYY-MM-D {var t = new Date (sDate. replace (/\-/g, '/'); var ar = sDate. split (/[-/:]/); if (ar [0]! = T. getYear () | ar [1]! = T. getMonth () + 1 | ar [2]! = T. getDate () {// alert ('incorrect date format! Format: YYYY-MM-DD or YYYY/MM/DD. Pay attention to the leap year. '); Return false ;}} else {// alert (' incorrect date format! Format: YYYY-MM-DD or YYYY/MM/DD. Pay attention to the leap year. '); Return false;} return true;} // + --------------------------------------------------- // | Date and Time check // | format: YYYY-MM-DD HH: MM: SS // + ----------------------------------------------------- function CheckDateTime (str) {var reg =/^ (\ d +)-(\ d {1, 2})-(\ d {1 }) (\ d {1, 2}) :( \ d {1, 2}) :( \ d {1, 2}) $/; var r = str. match (reg); if (r = null) return false; r [2] = r [2]-1; var d = new Date (r [1], r [2], r [3], r [4], r [5], r [6]); if (d. getFul LYear ()! = R [1]) return false; if (d. getMonth ()! = R [2]) return false; if (d. getDate ()! = R [3]) return false; if (d. getHours ()! = R [4]) return false; if (d. getMinutes ()! = R [5]) return false; if (d. getSeconds ()! = R [6]) return false; return true;} // + ----------------------------------------------------- // | splits the Date into an array // + seasonal Date. prototype. toArray = function () {var myDate = this; var myArray = Array (); myArray [0] = myDate. getFullYear (); myArray [1] = myDate. getMonth (); myArray [2] = myDate. getDate (); myArray [3] = myDate. getHours (); myArray [4] = myDate. getMinutes (); myArray [5] = MyDate. getSeconds (); return myArray ;} // + ------------------------------------------------- // | get date data // | the interval parameter indicates the data type. // | y, m, D, w, ww, week, h, n minutes, s, s/+ --------------------------------------------------- Date. prototype. datePart = function (interval) {var myDate = this; var partStr = ''; var Week = ['day', 'day', 'two', '3 ', '4', '5', '6']; switch (interval) {case 'y': partStr = myDate. getFullYear (); break; case'm ': PartStr = myDate. getMonth () + 1; break; case 'D': partStr = myDate. getDate (); break; case 'W': partStr = Week [myDate. getDay ()]; break; case 'ww ': partStr = myDate. weekNumOfYear (); break; case 'H': partStr = myDate. getHours (); break; case 'N': partStr = myDate. getMinutes (); break; case's ': partStr = myDate. getSeconds (); break;} return partStr;} // + ------------------------------------------------------- // | obtain the current date Maximum number of days in a month // + --------------------------------------------------- Date. prototype. maxDayOfDate = function () {var myDate = this; var ary = myDate. toArray (); var date1 = (new Date (ary [0], ary [1] + 1,1); var date2 = date1.dateAdd (1, 'M', 1 ); var result = dateDiff (date1.Format ('yyyy-MM-dd'), date2.Format ('yyyy-MM-dd'); return result ;} // + --------------------------------------------------- // | obtain the week of the year where the current date is located. // + ------ --------------------------------------------- Date. prototype. weekNumOfYear = function () {var myDate = this; var ary = myDate. toArray (); var year = ary [0]; var month = ary [1] + 1; var day = ary [2]; document. write ('<script language = VBScript \> \ n'); document. write ('mydate = DateValue (''+ month + '-' + day + '-' + year +'') \ n'); document. write ('result = DatePart ('ww ', myDate) \ n'); document. write ('\ n'); return result ;}/ // + Convert // | convert string to date type // | format: MM/dd/yyyy mm-dd-YYYY/MM/dd YYYY-MM-dd // + convert function StringToDate (DateStr) {var converted = Date. parse (DateStr); var myDate = new Date (converted); if (isNaN (myDate) {// var delimCahar = DateStr. indexOf ('/')! =-1? '/': '-'; Var arys = DateStr. split ('-'); myDate = new Date (arys [0], -- arys [1], arys [2]);} return myDate ;}