Javascript dateutil. js

Source: Internet
Author: User

 // Confirm <br/> // determine a leap year <br/> // ----------------------------------------------------- <br/> date. prototype. isleapyear = function () <br/>{< br/> return (0 = This. getyear () % 4 & (this. get year () % 100! = 0) | (this. getyear () % 400 = 0 ))); <br/>}< br/> // --------------------------------------------------- <br/> // format <br/> // yyyy/YYYY/yy indicates the year <br/> // mm/M month <br/> // w/w week <br/> // DD/D/d date <br/> // hh/ h/h time <br/> // mm/M minutes <br/> // SS/S seconds <br/> // ------------------------------------------------------- <br/> date. prototype. format = function (formatstr) <br/>{< br/> var STR = formatstr; <br/> VaR week = ['day', 'yi', '2', '3', '4', '5', '6']; </P> <p> STR = Str. replace (/YYYY | yyyy/, this. getfullyear (); <br/> STR = Str. replace (/yy | YY/, (this. getyear () % 100)> 9? (This. getyear () % 100 ). tostring (): '0' + (this. getyear () % 100); </P> <p> STR = Str. replace (/MM/, this. getmonth ()> 9? This. getmonth (). tostring (): '0' + this. getmonth (); <br/> STR = Str. replace (/M/G, this. getmonth (); </P> <p> STR = Str. replace (/w | w/g, week [this. getday ()]); </P> <p> STR = Str. replace (/DD | DD/, this. getdate ()> 9? This. getdate (). tostring (): '0' + this. getdate (); <br/> STR = Str. replace (/d | D/g, this. getdate (); </P> <p> STR = Str. replace (/HH | hh/, this. gethours ()> 9? This. gethours (). tostring (): '0' + this. gethours (); <br/> STR = Str. replace (/h | h/g, this. gethours (); <br/> STR = Str. replace (/MM/, this. getminutes ()> 9? This. getminutes (). tostring (): '0' + this. getminutes (); <br/> STR = Str. replace (/M/G, this. getminutes (); </P> <p> STR = Str. replace (/SS | SS/, this. getseconds ()> 9? This. getseconds (). tostring (): '0' + this. getseconds (); <br/> STR = Str. replace (/S | S/g, this. getseconds (); </P> <p> return STR; <br/>}</P> <p> // + --------------------------------------------------- <br/> // | returns the format of the day difference between two time periods: yyyy-mm-DD <br/> // + --------------------------------------------------- <br/> function daysbetween (dateone, datetwo) <br/>{< br/> var onemonth = dateone. substring (5, dateone. lastindexof ('-'); <Br/> var oneday = dateone. substring (dateone. length, dateone. lastindexof ('-') + 1); <br/> var oneyear = dateone. substring (0, dateone. indexof ('-'); </P> <p> var twomonth = datetwo. substring (5, datetwo. lastindexof ('-'); <br/> var twoday = datetwo. substring (datetwo. length, datetwo. lastindexof ('-') + 1); <br/> var twoyear = datetwo. substring (0, datetwo. indexof ('-'); </P> <p> var CHA = (date. parse (o Nemonth + '/' + oneday + '/' + oneyear)-date. parse (twomonth + '/' + twoday + '/' + twoyear)/86400000); <br/> return math. ABS (CHA ); <br/>}</P> <p> // + date <br/> // | date calculation <br/> // + ----------------------------------------------------- <br/> date. prototype. dateadd = function (strinterval, number) {<br/> var dttmp = This; <br/> switch (strinterval) {<br/> case 's': Return ne W date (date. parse (dttmp) + (1000 * number); <br/> case 'N': return new date (date. parse (dttmp) + (60000 * number); <br/> case 'H': return new date (date. parse (dttmp) + (3600000 * number); <br/> case 'D': return new date (date. parse (dttmp) + (86400000 * number); <br/> case 'W': return new date (date. parse (dttmp) + (86400000*7) * number); <br/> case 'q': return new date (dttmp. getfullyear (), (DTT MP. getmonth () + number * 3, dttmp. getdate (), dttmp. gethours (), dttmp. getminutes (), dttmp. getseconds (); <br/> case 'M': return new date (dttmp. getfullyear (), (dttmp. getmonth () + number, dttmp. getdate (), dttmp. gethours (), dttmp. getminutes (), dttmp. getseconds (); <br/> case 'y': return new date (dttmp. getfullyear () + number), dttmp. getmonth (), dttmp. getdate (), dttmp. gethours (), dttmp. getminutes (), DT TMP. getseconds ()); <br/>}</P> <p> // + --------------------------------------------------- <br/> // | compare the dtend format with the date type or valid date format string <br/> // + ----------------------------------------------------- <br/> date. prototype. datediff = function (strinterval, dtend) {<br/> var dtstart = This; <br/> If (typeof dtend = 'string ') // If the string is converted to the date type <br/>{< br/> dtend = stringtodate (dtend); <br/>}< br/> switch (Strinterval) {<br/> case 's': Return parseint (dtend-dtstart)/1000); <br/> case 'N ': return parseint (dtend-dtstart)/60000); <br/> case 'H': Return parseint (dtend-dtstart)/3600000 ); <br/> case 'D': Return parseint (dtend-dtstart)/86400000); <br/> case 'W': Return parseint (dtend-dtstart) /(86400000*7); <br/> case 'M': Return (dtend. getmonth () + 1) + (dtend. getfullyear ()-dtstar T. getfullyear () * 12)-(dtstart. getmonth () + 1); <br/> case 'y': Return dtend. getfullyear ()-dtstart. getfullyear (); <br/>}</P> <p> // + ----------------------------------------------- <br/> // | date output string, the system's tostring method is reloaded <br/> // + ------------------------------------------------- <br/> date. prototype. tostring = function (showweek) <br/>{< br/> var mydate = This; <br/> var STR = mydate. tolocaleda Testring (); <br/> If (showweek) <br/> {<br/> var week = ['day', 'yi', '2 ', '3', '4', '5', '6']; <br/> STR + = 'Week' + week [mydate. getday ()]; <br/>}< br/> return STR; <br/>}</P> <p> // + --------------------------------------------------- <br/> // | date validity verification <br/> // | format: YYYY-MM-DD or yyyy/mm/DD <br/> // + --------------------------------------------------- <br/> function isvaliddate (datestr) <br/> {<br/> var sdate = dat Estr. replace (/(^/S + |/S + $)/g, ''); // remove spaces on both sides; <br/> If (sdate = '') return true; <br/> // if the format is yyyy-(/) Mm-(/) dd Or yyyy-(/) m -(/) DD, yyyy-(/) m-(/) D, or yyyy-(/) Mm-(/) D is replaced with ''<br/> // database, valid date can be: YYYY-MM/DD (2003-3/21), the database is automatically converted to YYYY-MM-DD format <br/> var S = sdate. replace (/[/d] {4, 4} [/-/] {1} [/d] {1} [/-/] {1} [/d] {1, 2} /g, ''); <br/> If (S = '') // The description format meets the YYYY-MM-DD or YYYY-M-DD or YYYY-M-D or YYYY-MM-D <br/> {<br/> var T = N EW date (sdate. replace (//-/g, '/'); <br/> var AR = sdate. split (/[-/:]/); <br/> If (AR [0]! = T. getyear () | ar [1]! = T. getmonth () + 1 | ar [2]! = T. getdate () <br/>{< br/> // alert ('incorrect Date Format! Format: YYYY-MM-DD or yyyy/mm/DD. Pay attention to the leap year. '); <Br/> return false; <br/>}< br/> else <br/> {<br/> // alert ('incorrect Date Format! Format: YYYY-MM-DD or yyyy/mm/DD. Pay attention to the leap year. '); <Br/> return false; <br/>}< br/> return true; <br/>}</P> <p> // + --------------------------------------------------- <br/> // | Date and Time check <br/> // | format: YYYY-MM-DD hh: MM: SS <br/> // + --------------------------------------------------- <br/> function checkdatetime (STR) <br/>{< br/> var Reg =/^ (/d +) -(/d {}) :(/d {}) $ /; <br/> var r = Str. match (REG); <br/> If (r = NUL L) return false; <br/> r [2] = R [2]-1; <br/> var d = new date (R [1], R [2], R [3], R [4], R [5], R [6]); <br/> If (D. getfullyear ()! = R [1]) return false; <br/> If (D. getmonth ()! = R [2]) return false; <br/> If (D. getdate ()! = R [3]) return false; <br/> If (D. gethours ()! = R [4]) return false; <br/> If (D. getminutes ()! = R [5]) return false; <br/> If (D. getseconds ()! = R [6]) return false; <br/> return true; <br/>}</P> <p> // + date <br/> // | Splits a date into an array. <br/> // + ----------------------------------------------------- <br/> date. prototype. toarray = function () <br/>{< br/> var mydate = This; <br/> var myarray = array (); <br/> myarray [0] = mydate. getfullyear (); <br/> myarray [1] = mydate. getmonth (); <br/> myarray [2] = mydate. getdate (); <Br/> myarray [3] = mydate. gethours (); <br/> myarray [4] = mydate. getminutes (); <br/> myarray [5] = mydate. getseconds (); <br/> return myarray; <br/>}</P> <p> // + --------------------------------------------------- <br/> // | obtain date data information <br/> // | interval indicates data type <br/> // | y, M, D, W, week WW, week h, n minutes, S <br/> // + ----------------------------------------------- <br/> date. prototype. datepart = function (interv Al) <br/>{< br/> var mydate = This; <br/> var partstr = ''; <br/> var week = ['day ', '1', '2', '3', '4', '5', '6']; <br/> switch (interval) <br/>{< br/> case 'y': partstr = mydate. getfullyear (); break; <br/> case 'M': partstr = mydate. getmonth () + 1; break; <br/> case 'D': partstr = mydate. getdate (); break; <br/> case 'W': partstr = week [mydate. getday ()]; break; <br/> case 'ww ': partstr = mydate. weeknumofyear (); Break; <br/> case 'H': partstr = mydate. gethours (); break; <br/> case 'N': partstr = mydate. getminutes (); break; <br/> case's ': partstr = mydate. getseconds (); break; <br/>}< br/> return partstr; <br/>}</P> <p> // + --------------------------------------------------- <br/> // | obtains the maximum number of days of the current month. <br/> // + hour <br/> date. prototype. maxdayofdate = function () <br/> {<B R/> var mydate = This; <br/> var ary = mydate. toarray (); <br/> var date1 = (new date (ary [0], Ary [1] + 1,1); <br/> var date2 = date1.dateadd (1, 'M', 1); <br/> var result = datediff (date1.format ('yyyy-mm-dd'), date2.format ('yyyy-mm-dd ')); <br/> return result; <br/>}</P> <p> // + ------------------------------------------------- <br/> // | the week in which the current date is obtained is the week in the year. <br/> // + ------------------------------------------- -------- <Br/> date. prototype. weeknumofyear = function () <br/>{< br/> var mydate = This; <br/> var ary = mydate. toarray (); <br/> var year = ary [0]; <br/> var month = ary [1] + 1; <br/> var day = ary [2]; <br/> document. write ('<script language = VBScript/>/N'); <br/> document. write ('mydate = datevalue ("" + month + "-" + day + "-" + year + "")/N'); <br/> document. write ('result = datepart ("ww", mydate)/N'); <br/> d Ocument. write ('/N'); <br/> return result; <br/>}</P> <p> // + --------------------------------------------------- <br/> // | convert string to date type <br/> // | format: mm/DD/YYYY mm-dd-yyyy/mm/DD yyyy-mm-DD <br/> // + --------------------------------------------------- <br/> function stringtodate (datestr) <br/>{</P> <p> var converted = date. parse (datestr); <br/> var mydate = new date (converted); <br/> If (isnan (mydate) <B R/>{< br/> // var delimcahar = datestr. indexof ('/')! =-1? '/': '-'; <Br/> var arys = datestr. split ('-'); <br/> mydate = new date (arys [0], -- arys [1], arys [2]); <br/>}< br/> return mydate; <br/>}< br/> document. write (new date ()). datepart ('H '));

 

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.