Js date processing class

Source: Internet
Author: User

Sort the date Processing Methods written by yourself and collected on the Internet into DateUtil. js/*** date processing tool class */var DateUtil = function () {/*** determine a leap year * @ param date Date date object * @ return boolean true or false */this. isLeapYear = function (date) {return (0 = date. getYear () % 4 & (date. get year () % 100! = 0) | (date. getYear () % 400 = 0);}/*** convert a date object to a string in the specified format * @ param f date format, the format is defined as yyyy-MM-dd HH: mm: ss * @ param date Date date object. If the default format is, the current time is ** YYYY/yyyy/YY/yy, indicating the year * MM/M month * W/w week * dd/DD/d/D date * hh/HH/h /H time * mm/m minutes * ss/SS/s/S seconds * @ return string specifies the format of the time string */this. dateToStr = function (formatStr, date) {formatStr = arguments [0] | "yyyy-MM-dd HH: mm: ss "; date = arguments [1] | new Date (); var str = FormatStr; var Week = ['day', 'yi', '2', '3', '4', '5', '6']; str = str. replace (/yyyy | YYYY/, date. getFullYear (); str = str. replace (/yy | YY/, (date. getYear () % 100)> 9? (Date. getYear () % 100 ). toString (): '0' + (date. getYear () % 100); str = str. replace (/MM/, date. getMonth ()> 9? (Date. getMonth () + 1): '0' + (date. getMonth () + 1); str = str. replace (/M/g, date. getMonth (); str = str. replace (/w | W/g, Week [date. getDay ()]); str = str. replace (/dd | DD/, date. getDate ()> 9? Date. getDate (). toString (): '0' + date. getDate (); str = str. replace (/d | D/g, date. getDate (); str = str. replace (/hh | HH/, date. getHours ()> 9? Date. getHours (). toString (): '0' + date. getHours (); str = str. replace (/h | H/g, date. getHours (); str = str. replace (/mm/, date. getMinutes ()> 9? Date. getMinutes (). toString (): '0' + date. getMinutes (); str = str. replace (/m/g, date. getMinutes (); str = str. replace (/ss | SS/, date. getSeconds ()> 9? Date. getSeconds (). toString (): '0' + date. getSeconds (); str = str. replace (/s | S/g, date. getSeconds (); return str ;} /*** date calculation ** @ param strInterval string optional values y, m, D, w, week, ww, week, h, n minutes, s, s * @ param num int * @ param Date date Date object * @ return Date returns the Date object */this. dateAdd = function (strInterval, num, date) {date = arguments [2] | new Date (); switch (strInterval) {case's ': return new Date (date. getTime () + (10 00 * num); case 'N': return new Date (date. getTime () + (60000 * num); case 'H': return new Date (date. getTime () + (3600000 * num); case 'D': return new Date (date. getTime () + (86400000 * num); case 'W': return new Date (date. getTime () + (86400000*7) * num); case 'M': return new Date (date. getFullYear (), (date. getMonth () + num, date. getDate (), date. getHours (), date. getMinutes (), date. getSeconds (); Case 'y': return new Date (date. getFullYear () + num), date. getMonth (), date. getDate (), date. getHours (), date. getMinutes (), date. getSeconds ());}} /*** compare date difference dtEnd format is a date type or valid date format string * @ param strInterval string optional value y year m month D day w week ww week h hour n minutes s seconds * @ param dtStart Date value options: y year m month D day w week ww week h hour n minute s second * @ param dtEnd Date value options: y year m month D day w week ww week h hour n in seconds */this. dateDiff = function (strInterval, dtStart, 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 () * 1 2)-(dtStart. getMonth () + 1); case 'y': return dtEnd. getFullYear ()-dtStart. getFullYear () ;}}/*** string to date object * @ param Date date Format: yyyy-MM-dd HH: mm: ss, which must be in the order of year, month, day, hour, minute, and second, the intermediate separator is not limited */this. strToDate = function (dateStr) {var data = dateStr; var reCat =/(\ d {1, 4})/gm; var t = data. match (reCat); t [1] = t [1]-1; eval ('var d = new Date ('+ t. join (',') + '); return d;}/*** converts a string in the specified format to a date object yyyy-MM-dd HH: mm: Ss **/this. strFormatToDate = function (formatStr, dateStr) {var year = 0; var start =-1; var len = dateStr. length; if (start = formatStr. indexOf ('yyyy')>-1 & start <len) {year = dateStr. substr (start, 4);} var month = 0; if (start = formatStr. indexOf ('mm')>-1 & start <len) {month = parseInt (dateStr. substr (start, 2)-1;} var day = 0; if (start = formatStr. indexOf ('dd')>-1 & star T <len) {day = parseInt (dateStr. substr (start, 2);} var hour = 0; if (start = formatStr. indexOf ('hh ')>-1 | (start = formatStr. indexOf ('hh ')> 1) & start <len) {hour = parseInt (dateStr. substr (start, 2);} var minute = 0; if (start = formatStr. indexOf ('mm')>-1 & start <len) {minute = dateStr. substr (start, 2);} var second = 0; if (start = formatStr. indexOf ('ss')>-1 & start <len) {Second = dateStr. substr (start, 2);} return new Date (year, month, day, hour, minute, second);}/*** convert a Date object to milliseconds */this. dateToLong = function (date) {return date. getTime ();}/*** convert to date object in milliseconds * @ param dateVal number the number of milliseconds of the date */this. longToDate = function (dateVal) {return new Date (dateVal);}/*** determines whether the string is in the Date format * @ param str string * @ param formatStr string Date format, yyyy-MM-dd */this. isDate = f Unction (str, formatStr) {if (formatStr = null) {formatStr = "yyyyMMdd";} var yIndex = formatStr. indexOf ("yyyy"); if (yIndex =-1) {return false;} var year = str. substring (yIndex, yIndex + 4); var mIndex = formatStr. indexOf ("MM"); if (mIndex =-1) {return false;} var month = str. substring (mIndex, mIndex + 2); var dIndex = formatStr. indexOf ("dd"); if (dIndex =-1) {return false;} var day = str. substring (dI Ndex, dIndex + 2); if (! IsNumber (year) | year> "2100" | year <"1900") {return false;} if (! IsNumber (month) | month> "12" | month <"01") {return false;} if (day> getMaxDay (year, month) | day <"01") {return false;} return true;} this. getMaxDay = function (year, month) {if (month = 4 | month = 6 | month = 9 | month = 11) return "30 "; if (month = 2) if (year % 4 = 0 & year % 100! = 0 | year % 400 = 0) return "29"; else return "28"; return "31";}/*** whether the variable is a number */this. isNumber = function (str) {var regExp =/^ \ d + $/g; return regExp. test (str);}/*** splits the date into an array [year, month, day, hour, minute, second] */this. toArray = function (myDate) {myDate = arguments [0] | new Date (); 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 information * parameter interval indicates data type * y, M, D, w, week, ww, week, h, n minutes, s */this. datePart = function (interval, myDate) {myDate = arguments [1] | new Date (); var partStr = ''; var Week = ['day', '1 ', '2', '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 'M': partStr = myDate. getMinutes (); break; case's ': partStr = myDate. getSeconds (); break;} return partStr;}/*** get the maximum number of days of the month of the current date */this. maxDayOfDate = function (date) {date = Rguments [0] | new Date (); date. setDate (1); date. setMonth (date. getMonth () + 1); var time = date. getTime ()-24*60*60*1000; var newDate = new Date (time); return newDate. getDate () ;}return this ;}(); Test Result: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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.