Common operations Summary of date types in JavaScript _ basics

Source: Internet
Author: User
Tags getdate

Common Operations Summary of date types in JavaScript

/** 3. * Date Time Script Library method list: 4. * (1) Date.isvalidate: Date legality verification 5. * (2) Date.isvalitime: Time Validity verification 6. * (3) Date.isvalidatetime: Validation of date and time legality 7. * (4) Date.prototype.isLeapYear: Judge whether leap Year 8. * (5) Date.prototype.format: Date format 9. * (6) Date.stringtodate: string converted to date type 10. * (7) Date.daysbetween: Calculates a two-day difference of 11 days. * (8) DATE.PROTOTYPE.DATEADD: Date calculation, support positive and negative number 12. * (9) Date.prototype.dateDiff: compare date Difference: Compare two periods of the same field, return the difference value 13. * (a) Date.prototype.toArray: the date is divided into groups: according to the array ordinal number: month-day time of 14 seconds. * (one) Date.prototype.datePart: Get Date data information 15. 
* * 16. 17.18./** 19. * Date Legality verification: Determines whether datastr conforms to the FORMATSTR specified date format 20. * Example: 21. * (1) alert (date.isvalidate (' 2008-02-29 ', ' yyyy-mm-dd '));//true 22. * (2) alert (date.isvalidate (' aaaa-58-29 ', ' yyyy-mm-dd '));//false 23. * DATESTR: Required, date string 24. * FORMATSTR: Optional, format string, optional format: (1) yyyy-mm-dd (default) 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 25. */26.date.isvalidate = function (Datestr, FORMATSTR) 27.    {. if (!datestr) {29. 
return false;  30.} 31. if (!formatsTR) {32.  Formatstr = "YYYY-MM-DD";//Default format: Yyyy-mm-dd 33.  } 34.    if (datestr.length!=formatstr.length) {35. 
return false;    }else{37. if (formatstr== "Yyyy-mm-dd" | |      formatstr== "Yyyy-mm-dd") {38. 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]))) $/; 
R1.test return (DATESTR); }else if (formatstr== "Yyyy/mm/dd" | |      formatstr== "Yyyy/mm/dd") {41. 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") {44. 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])) 
$/; 
R3.test return (DATESTR); }else if (formatstr== "mm/dd/yyyy" | |      formatstr== "Mm/dd/yyyy") {47. 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{50. Alert ("date format is not correct!") 
"); 
A. return false;  52.} 53.  } 54. 
return false; 
55.} 56. 57.58./** 59. * Time Validity validation: Determines whether timestr conforms to the FORMATSTR specified time format 60. * Example: 61. * (1) alert (date.isvalitime (' 23:59:59 ', ' hh:mm:ss '));//true 62. * (2) alert (date.isvalitime (' 24-68-89 ', ' hh:mm:ss '));//false 63. * TIMESTR: Required, date string 64. * FORMATSTR: Optional, format string, optional format: (1) hh:mm:ss (default) (2) Hh-mm-ss (3) hh/mm/ss 65. */66.date.isvalitime = function (TIMESTR, FORMATSTR) 67.    {69. if (!TIMESTR). 
return false;  70.} 71.    if (!FORMATSTR) {72.  Formatstr = "HH:MM:SS";//Default format: Hh:mm:ss 73.  } 74.    if (timestr.length!=formatstr.length) {75. 
return false;    }else{77.      if (formatstr== "Hh:mm:ss") {78. var r1=/^ ([0-1][0-9]) | ( 
2[0-3]) \:([0-5][0-9]) \:([0-5][0-9]) $/; 
R1.test return (TIMESTR);      }else if (formatstr== "Hh-mm-ss") {81. var r2=/^ ([0-1][0-9]) | ( 2[0-3]) \-([0-5][0-9]) \-([0-5][0-9]) $/;
A. Return r2.test (TIMESTR);      }else if (formatstr== "Hh/mm/ss") {84. var r3=/^ ([0-1][0-9]) | ( 
2[0-3]) \/([0-5][0-9]) \/([0-5][0-9]) $/; 
Return R3.test (TIMESTR);      }else{87. Alert ("Time format is not correct!") 
"); 
return false;  89.} 90.  } 91. 
return false; 
92.} 93. 94.95./** 96. * Date and time legality validation 97. * Format: YYYY-MM-DD HH:MM:SS 98. */99.date.isvalidatetime = function (datetimestr) 100. {datetimereg=/^ var ((([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])? $/102. 
Return Datetimereg.test (DATETIMESTR); 
103.} 104. 105.106./** 107. 
* Judgment Leap Year: General law: Four years a leap, hundred years not a leap, 400 year again leap. 108. */109.date.protOtype.isleapyear = function () 110. {A. return (This.getyear ()%4==0&& (This.getyear ()%100!=0) | | 
(This.getyear ()%400==0)); 
112.} 113. 114.115./** 116. * Date format: 117. * FORMATSTR: Optional, format string, default format: Yyyy-mm-dd HH:MM:SS 118. * agreed to the following format: 119. * (1) Yyyy/yyyy/yy/yy represents the year 120. * (2) mm/m month 121. * (3) w/w Week 122. * (4) dd/dd/d/d date 123. * (5) hh/hh/h/h time 124. * (6) mm/m minutes 125. * (7) ss/ss/s/s seconds 126. * (8) III millisecond 127. */128.date.prototype.format = function (FORMATSTR) 129. 
{130. var str = FORMATSTR;    131. if (!FORMATSTR) {132.  str = "Yyyy-mm-dd hh:mm:ss";//Default format 133.  } 134. 
var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];  135.136. Str=str.replace (/yyyy| 
Yyyy/,this.getfullyear ()); 137. Str=str.replace (/yy| yy/, (this.getyear ()%) >9? 
(This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100));  138.139. Str=str.replace (/mm/,this.getmonth () >=9? ( 
parseint (This.getmonth ()) +1). ToString (): ' 0 ' + (parseint (This.getmonth ()) +1)); 140. Str=str.replace (/m/g, parseInt (This.getmonth ()) +1));  141.142. Str=str.replace (/w| 
W/g,week[this.getday ()]);  143.144. Str=str.replace (/dd| 
Dd/,this.getdate () >9?this.getdate (). toString (): ' 0 ' + this.getdate ()); 145. Str=str.replace (/d| 
D/g,this.getdate ());  146.147. Str=str.replace (/hh| 
Hh/,this.gethours () >9?this.gethours (). toString (): ' 0 ' + this.gethours ()); 148. Str=str.replace (/h| 
H/g,this.gethours ()); 
149. Str=str.replace (/mm/,this.getminutes () >9?this.getminutes (). toString (): ' 0 ' + this.getminutes ()); 
Str=str.replace (/m/g,this.getminutes ());  151.152. Str=str.replace (/ss| 
Ss/,this.getseconds () >9?this.getseconds (). toString (): ' 0 ' + this.getseconds ()); 153. Str=str.replace (/s| 
S/g,this.getseconds ());  154.155. Str=str.replace (/iii/g,this.getmilliseconds () <10? ' ' +this.getmilliseconds ():(this.getmilliseconds () <100? ' 
0 ' +this.getmilliseconds (): This.getmilliseconds ());  156.157. 
return str; 
158.} 159. 160.161./** 162. * string is converted to date type: 163. * DATESTR: Required, date string, such asThe result cannot be resolved to a date type and returns null 164. * Format: 165. * (1) Yyyy/mm/dd:ie and FF General 166. * (2) Mm/dd/yyyy:ie and FF General 167. * (3) mm-dd-yyyy: IE 168 only. * (4) YYYY-MM-DD: Non ie, and the clock is resolved at 8 o'clock full 169. */170.date.stringtodate = function (datestr) 171.    {172. if (!DATESTR) {173. 
Alert ("string cannot resolve to date"); 
174. return null;    175.}else{176. if (Date.isvalidate (Datestr, "Yyyy/mm/dd") | |      Date.isvalidate (Datestr, "mm/dd/yyyy")) {177. 
return new Date (Date.parse (DATESTR));      178.}else{179.        if ((!-[1,])) {//ie 180.          if (Date.isvalidate (Datestr, "mm-dd-yyyy")) {181. 
return new Date (Date.parse (DATESTR));          }else{183. 
Alert ("string cannot resolve to date"); 
184. return null;      185.} 186.        }else{//is not IE 187.          if (Date.isvalidate (Datestr, "Yyyy-mm-dd")) {188. 
return new Date (Date.parse (DATESTR));          189.}else{190. 
Alert ("string cannot resolve to date"); 
191. return null;      192.} 193.    } 194.  } 195.  } 196. 
return null; 
197.} 198. 199.200./** 201. * Calculate the two-day difference in days: 202. * Dateone: Required, must be instance 203 of the data type. * Datetwo: Required, must be instance 204 of the data type. */205.date.daysbetween = function (dateone,datetwo) 206. {207. If (Dateone instanceof Date) ==false| |    (Datetwo instanceof Date) ==false) {208. 
return 0;    209.}else{210. 
Return Math.Abs (Math.floor (Dateone.gettime ()-datetwo.gettime ())/1000/60/60/24); 
211.} 212.} 
213.214. 215./** 216. * Date Calculation: Support negative numbers, you can add and subtract, return the calculated date 217. * Num: Required, must be a number, and positive numbers are time plus, minus 218 is the date. * Field: Optional, the identification is in which field to add or subtract, the field see the following convention. Without this parameter, the default is D 219. * agreed to the following format: 220. * (1) y/y year 221. * (2) M-month 222. * (3) w/w Week 223. * (4) d/d Day 224. * (5) h/h 225. * (6) m minute 226. * (7) s/s seconds 227. * (8) q/q Quarter 228. */229.date.prototype.dateadd = function (num, field) 230. {231. if (!num) | | isNaN (num) | |    parseint (num) ==0) {232. 
return this;  233.} 234.    if (!field) {235. 
field = "D";  236.} 237.    Switch (field) {238.    Case ' Y ': 239. Case ' Y ': Return new Date ((This.getfullyear () +num), This.getmonth (), this.getdate (), thIs.gethours (), This.getminutes (), this.getseconds ());    Case ' Q ': 241. Case ' Q ': return new Date (This.getfullyear (), (This.getmonth () +num*3), This.getdate (), this.gethours (), 
This.getminutes (), this.getseconds ()); 242. Case ' M ': return new Date (This.getfullyear (), This.getmonth () +num, This.getdate (), this.gethours (), This.getminute 
S (), this.getseconds ());    243. Case ' W ': 244. 
Case ' W ': Return to New Date (Date.parse (This) + ((86400000 * 7) * num));    245. Case ' D ': 246. 
Case ' d ': Return to New Date (Date.parse (this) + (86400000 * num));    247. Case ' H ': 248. 
Case ' H ': Return to New Date (Date.parse (this) + (3600000 * num)); 
249. Case ' m ': Return to New Date (Date.parse (this) + (60000 * num));    Case ' S ': 251. 
Case ' s ': Return to New Date (Date.parse (this) + (1000 * num)); 
252. Default:return this;  253.} 254. 
return this; 
255.} 256. 257.258./** 259. * Compare Date Difference: Compares two periods of the same field, returns the difference value 260. * Dtend: Required, must be the data typeInstance 261. * Field: Optional, the identification is in which field to compare, the field see the following convention. Without this parameter, the default is D 262. * agreed to the following format: 263. * (1) y/y year 264. * (2) M-month 265. * (3) w/w week 266. * (4) d/d Day 267. * (5) h/h 268. * (6) m minute 269. * (7) s/s seconds 270. */271.date.prototype.datediff = function (dtend, field) 272. 
{273. var dtstart = this;    274. if ((dtend instanceof Date) ==false) {275. 
return 0;    276.}else{277.      if (!field) {278. 
field = "D";    279.} 280.      Switch (field) {281.      Case ' Y ': 282. 
Case ' Y ': return dtend.getfullyear ()-dtstart.getfullyear (); 283. Case ' M ': Return (Dtend.getmonth () +1) + ((Dtend.getfullyear ()-dtstart.getfullyear ()) *12)-(Dtstart.getmonth () +1) 
; break;      284. Case ' W ': 285. 
Case ' W ': Return parseint ((Dtend-dtstart)/(86400000 * 7));      286. Case ' D ': 287. 
Case ' d ': Return parseint ((dtend-dtstart)/86400000);      Case ' H ': 289. 
Case ' H ': Return parseint ((dtend-dtstart)/3600000); 290. Case ' m ': Return parseint (DTEND-DTStart)/60000);      291. Case ' S ': 292. 
Case ' s ': Return parseint ((Dtend-dtstart)/1000); 
293. Default:return 0;    294.} 295. 
return 0; 
296.} 297.} 
298.299. 300./** 301. * The date is divided into groups: according to the array ordinal number: month-day time of 302 seconds. */303.date.prototype.toarray = function () 304. 
{305. var myarray = new Array (); 
306. myarray[0] = This.getfullyear (); 
307. Myarray[1] = This.getmonth (); 
308. myarray[2] = This.getdate (); 
309. Myarray[3] = this.gethours (); 
310. myarray[4] = This.getminutes (); 
311. myarray[5] = This.getseconds (); 
312. return myarray; 
313.} 314. 315.316./** 317. * Get Date Data information: 318. * Field: Optional, the identification is in which field to compare, the field see the following convention. Without this parameter, the default is D 319. * (1) y/y year 320. * (2) M-month 321. * (3) w/w Week 322. * (4) D/D Day 323. * (5) h/h 324. * (6) m minute 325. * (7) S/S seconds 326. */327.date.prototype.datepart = function (field) 328.    {329. if (!field) {330. 
field = "D";  331.} 332. 
var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];    333. switch (field) {334. Case ' Y ': 335. 
Case ' Y ': return this.getfullyear (); 
336. Case ' M ': Return (This.getmonth () +1);    337. Case ' W ': 338. 
Case ' W ': Return Week[this.getday ()];break;    339. Case ' D ': 340. 
Case ' d ': return This.getdate ();    341. Case ' H ': 342. 
Case ' H ': return This.gethours (); 
343. Case ' m ': return This.getminutes (); 
344. Case ' s ': return This.getseconds (); 
345. Default:return This.getdate ();  346.} 347. 
return This.getdate ();  348.}

The most common operation of date types in this JavaScript is the small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.