Summary of common operations on the Date type in javascript _ basic knowledge

Source: Internet
Author: User
The following is a summary of common operations on the Date type in javascript. I think it is quite good. Now I will share it with you and give you a reference. Let's take a look at it with xiaobian. Summary of common operations on Date type in javascript

/** 3. * Date and Time script library method list: 4. * (1) Date. isValiDate: date validity verification 5. * (2) Date. isValiTime: time validity verification 6. * (3) Date. isValiDateTime: Date and time validity verification 7. * (4) Date. prototype. isLeapYear: determines whether a leap year is used. * (5) Date. prototype. format: Date format 9. * (6) Date. stringToDate: String converted to date type 10. * (7) Date. daysBetween: calculates the number of days difference between two dates by 11. * (8) Date. prototype. dateAdd: date calculation, supporting positive and negative 12. * (9) Date. prototype. dateDiff: Compare date difference: compare two fields with the same period, return the difference value 13. * (10) Date. prototype. t OArray: divides a date into an array. The sequence numbers are respectively: year, month, day, hour, minute, and second. * (11) Date. prototype. datePart: Get date data information 15. */16. 17. 18. /** 19. * Date validity verification: determines whether dataStr meets the date format specified by formatStr 20. * example: 21. * (1) alert (Date. isValiDate ('1970-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 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 25. */26. date. isValiDate = function (dateStr, formatStr) 27. {28. 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; 36 .} 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]) $/; 39. return r1.test (dateStr); 40 .} 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]) | ([13 579] [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]) $/; 42. return r2.test (dateStr); 43 .} 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]) $/; 45. return r3.test (dateStr); 46 .} 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]) $/; 48. return r4.test (dateStr); 49 .} else {50. alert ("the date format is incorrect! "); 51. return false; 52 .} 53 .} 54. return false; 55 .} 56. 57. 58. /** 59. * Time validity verification: checks whether timeStr meets the time format specified by formatStr 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 formats: (1) hh: mm: ss (default Format) (2) hh-mm-ss (3) hh/mm/ss 65. */66. date. isValiTime = function (timeStr, formatSt R) 67. {68. if (! TimeStr) {69. 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; 76 .} 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]) $/; 79. return r1.test (timeStr); 80 .} else if (formatStr = "hh-mm-ss") {81. var r2 =/^ ([0-1] [0-9]) | (2 [0-3]) \-([0-5] [0-9]) \-([0-5] [0-9]) $/; 82. return r2.test (timeStr); 83 .} else if (formatStr = "hh/mm/ss") {84. var r3 =/^ ([0-1] [0-9]) | (2 [0-3]) \/([0-5] [0-9]) \/( [0-5] [0-9]) $/; 85. return r3.test (timeStr); 86.} else {87. alert ("the time format is incorrect! "); 88. return false; 89 .} 90 .} 91. return false; 92 .} 93. 94. 95. /** 96. * Date and time validity verification 97. * Format: yyyy-MM-dd hh: mm: ss 98. */99. date. isValiDateTime = function (dateTimeStr) 100. {101. 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} ([0246) 8] [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])? USD/102. return dateTimeReg. test (dateTimeStr); 103 .} 104. 105. 106. /** 107. * leap year: the general rule is four years, one hour for a hundred years, and one hour for a hundred years. 108. */109. date. prototype. isLeapYear = function () 110. {111. return (this. getYear () % 4 = 0 & (this. get year () % 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. * The format is as follows: 119. * (1) YYYY/yyyy/YY indicates the year 120. * (2) MM/M month 121. * (3) W/w 122 a week. * (4) dd/DD/d/D date 123. * (5) hh/HH/h/H time 124. * (6) mm/m min 125. * (7) ss/SS/s/S second 126. * (8) iii 127 Ms. */128. date. prototype. format = function (formatStr) 129. {130. varstr = formatStr; 131. if (! FormatStr) {132. str = "yyyy-MM-dd hh: mm: ss"; // The default format is 133 .} 134. var Week = ['day', 'yi', '2', '3', '4', '5', '6']; 135. 136. str = str. replace (/yyyy | YYYY/, this. getFullYear (); 137. str = str. replace (/yy | YY/, (this. getYear () % 100)> 9? (This. getYear () % 100 ). 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 (); 150. 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? '00' + this. getMilliseconds () :( this. getMilliseconds () <100? '0' + this. getMilliseconds (): this. getMilliseconds (); 156. 157. return str; 158 .} 159. 160. 161. /** 162. * convert string to date type: 163. * dateStr: (required) date string. If it cannot be parsed into a date type, null 164 is returned. * Format: 165. * (1) yyyy/MM/dd: General 166 for IE and FF. * (2) MM/dd/yyyy: General 167 for IE and FF. * (3) MM-dd-yyyy: IE only 168. * (4) yyyy-MM-dd: Non-IE, and the clock is resolved at eight o'clock 169. */170. date. stringToDate = function (dateStr) 171. {172. if (! DateStr) {173. alert ("the string cannot be parsed as a 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,]) {// Internet Explorer 180. if (Date. isValiDate (dateStr, "MM-dd-yyyy") {181. return new Date (Date. parse (dateStr); 182 .} else {183. alert ("the string cannot be parsed as a date"); 184. return null; 185 .} 186 .} else {// non-IE 187. if (Date. isValiDate (dateStr, "yyyy-MM-dd") {188. return new Date (Date. parse (dateStr); 189 .} else {190. alert ("the string cannot be parsed as a date"); 191. return null; 192 .} 193 .} 194 .} 195 .} 196. return null; 197 .} 198. 199. 200. /** 201. * The number of days difference between two dates: 202. * dateOne: required. It must be Data-type instance 203. * dateTwo: required. It must be Data-type instance 204. */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: a negative number can be added or subtracted. The calculated date 21 is returned. 7. * num: required. It must be a number, and a positive value is a period plus. A negative value is a date minus 218. * field: Optional. It indicates the field on which the tag is added or subtracted. For the fields, see the following conventions. If this parameter is not set, the default value is d 219. * The format is as follows: 220. * (1) Y/y year 221. * (2) 222 M months. * (3) W/w week 223. * (4) D/d: 224. * (5) 225 H/h. * (6) m: 226. * (7) S/s 227 seconds. * (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 (); break; 240. case 'q': 241. case 'q': return new Date (this. getFullYear (), (this. getMonth () + num * 3), this. getDate (), this. getHours (), this. getMinutes (), this. getSeconds (); break; 242. case 'M ': Return new Date (this. getFullYear (), this. getMonth () + num, this. getDate (), this. getHours (), this. getMinutes (), this. getSeconds (); break; 243. case 'W': 244. case 'W': return new Date (Date. parse (this) + (86400000*7) * num); break; 245. case 'D': 246. case 'D': return new Date (Date. parse (this) + (86400000 * num); break; 247. case 'H': 248. case 'H': return new Date (Date. parse (this) + (3600000 * num )); Break; 249. case 'M': return new Date (Date. parse (this) + (60000 * num); break; 250. case's: 251. case's ': return new Date (Date. parse (this) + (1000 * num); break; 252. default: return this; 253 .} 254. return this; 255 .} 256. 257. 258. /** 259. * compare date difference: compare two fields with the same period, and return the difference of 260. * dtEnd: required. It must be Data-type instance 261. * field: (optional) field on which the logo is compared. For the fields, see the following conventions. If this parameter is not set, the default value is d 262. * The format is as follows: 263. * (1) Y/y year 264. * (2) 265 M months. * (3) W/w week 266. * (4) D/d: 267. * (5) 268 H/h. * (6) m: 269. * (7) S/s 270 seconds. */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. getfullyears (); break; 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); break; 286. case 'D': 287. case 'D': return parseInt (dtEn D-dtStart)/86400000); break; 288. case 'H': 289. case 'H': return parseInt (dtEnd-dtStart)/3600000); break; 290. case 'M': return parseInt (dtEnd-dtStart)/60000); break; 291. case's: 292. case's ': return parseInt (dtEnd-dtStart)/1000); break; 293. default: return 0; 294 .} 295. return 0; 296 .} 297 .} 298. 299. 300. /** 301. * divide the date into an array. The sequence numbers of the arrays are: year, month, day, hour, minute, second, and 302. */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. * obtain date data: 318. * field: (optional) field on which the logo is compared. For the fields, see the following conventions. If this parameter is not set, the default value is d 319. * (1) Y/y year 320. * (2) 321 M months. * (3) W/w week 322. * (4) D/d: 323. * (5) 324 H/h. * (6) m: 325. * (7) S/s 326 seconds. */327. date. prototype. datePart = function (field) 328. {329. if (! Field) {330. field = "d"; 331 .} 332. var Week = ['day', 'yi', '2', '3', '4', '5', '6']; 333. switch (field) {334. case 'y': 335. case 'y': return this. getfullyears (); break; 336. case 'M': return (this. getMonth () + 1); break; 337. case 'W': 338. case 'W': return Week [this. getDay ()]; break; 339. case 'D': 340. case 'D': return this. getDate (); break; 341. case 'H': 342. case 'H': return this. getHours (); break; 343. case 'M': return this. getMinutes (); break; 344. case's ': return this. getSeconds (); break; 345. default: return this. getDate (); 346 .} 347. return this. getDate (); 348 .}

The common operations of the Date type in the above javascript section are all the content shared by the editor. I hope to give you a reference and support for the script.

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.