Résumé des opérations communes sur les types de date en JavaScript
/ ** 3. * Date.isvalidate: Date Vérification de la légalité 5. * (2) Date.Isvalime: Heure La légalité Vérification 6. * (3) Date.IsvalidateTime: Date and Heure Legality Verification 7. * (4) Date.prototype.isapyear: Date de la légalité. Date.StringTodate: Converti une chaîne au Date Type 10. * (7) Date.Daysbetween: Calculez la différence entre les jours entre les deux dates 11. * (8) Date.prototype.dateadd: Date Calcul, prend en charge les numéros positifs et négatifs 12. * (9) Date.prototype.datedif Date.prototype.toarray: divisez la date en un tableau: Selon le numéro de tableau, c'est respectivement: année, jour, heure, minute et seconde respectivement. * (11) Date.prototype.datePart: obtenir des informations sur les données de date 15. * / 16. 17. 18./** 19. * Date Vérification de la légalité: Déterminez si DATASTR répond au format de date spécifié par FormatStr20. * Exemple: 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. (1) yyyy-mm-dd (format par défaut) ou yyyy-mm-dd (2) yyyy / mm / dd ou yyyy / mm / dd (3) mm-dd-yyy ou mm-dd-yyyy (4) mm / dd / yyyyy ou mm / dd / yyyy 25. 27. {28. If (! Datestr) {29. Return false; 30.} 31. if (! Formatstr) {32. FormatStr = "Yyyy-mm-dd"; // Format par défaut: 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]))))) / - (((0 [13578]) | (1 [02])) / - ((((0 [13578]) | (1 [02])) / - ((((( ((0-2] [0-9]) | (3 [01]))) | (((0 [469]) | (11)) / - (([0-2] [0-9]) | (30))) | ( 02 / - ([0-2] [0-2]) | (30))) | (02 / - ([0-2] [0-2]) | (3 [01])) | ((0 [469]) | (1 1)) / - ((((0-2] [0-9]) | (30))) | (02 / - ([0-2] [0-2]) | (30)) | (02 / - ([0-2] [0-2]) | (30)) | (02 / - ([0-2] [0-2]) | (30)) - ([0-2] [0-2]) | (30)) | (02 / - ([[0-2] [0-2]) | (30)) | (02 / - ([0-2] [0-2]) | (30)) | (02 / - ([0-2] [0-2]) | (30)) | (02 / - [[0-2] [0-2]) | (30)) ] [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. RETOUR R1.TEST (Datestr); 40.} Else if (formatstr == "yyyy / mm / dd" || R2 = / ^ (((((((((((((((((((((((((((((((((((((((((((((([0246 8] [048])))))) // (((0 [13578]) | (1 [02])) // (((0-2] [0-9]) | (3 [01]))) | ( ((0 [469]) | (11)) // (((([0-2] [0-9]) | (30))) | (02 // ([0-2] [0-2]) | (30))) | (02 // ([0-2] [0-2]) | (3 [01])) | (((0 [469]) | (11)) // ((((0-2] [0-9]) | (3 0))) | (02 // ([0-2] [0-2]) | (30)) | (02 // ([[0-2] [0-2]) | (30)) | (02 // ([0-2] [0-2]) | (30)) | (02 // ([0-2] [0-2]) | (30)) | (02 // ([0-2] [0-2]) | (30)) | (02 // ([0-2] [0-2]) // ([0-2] [0-2]) | (30)) | (02 // ([[0-2] [0-2]) | (30)) | (02 // ([0-2] [0-2]) | (30)) | (02 // ([0-2] [0-2]) | (30)) | (02 // [0-2] [0-2]) | (30)) | (02 // [0-2] [0-2]) | (30)) | (02 // [0-2]) ] [0-9])))) | (/ d {2} (([02468] [1235679]) | ([13579] [01345789])) // (((0 [13578]) | (1 [02])) // (0-2] [[13578]) | (1 [02])) // (0-2] [[13578]) | (1 [02])) // (0-2] [[13578]) | (1 [02])) // (0-2] [[13578]) | (1 [02])) // (0-2] [[13578]) | (1 [02])) // (0-2] [[13578]) | (1 [02])) // (0-2] [[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-yyy" || R3 = / ^ (((((((((0 [13578]) | (1 [02]))) / - (([0-2] [0-9]) | (3 [01]))) | ((0 [469]) | (11)) / - ((((((0-2] [0-9]) | (30))) -9]))) / - ((((([02468] [048]) | ([13579] [26])) (00)) | (/ d {2} (([02468] [48]) | ([13579] [26])) (00)) | (/ d {2} (([02468] ] [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. RETOUR R3.TEST (Datestr); 46.} Else if (formatstr == "mm / dd / yyyy" || formatstr == "mm / dd / yyyy") {47. r4 = / ^ ((((((((0 [13578]) | (1 [02])) // (([0-2] [0-9]) | (3 [01]))) | ((0 [469]) | (11)) // ((([0-2] [0-9]) | (30))) | (02 // ([[0-2] [0-9]) | (30))) | (02 // ([[0-2] [0-9] ))) // ((((([02468] [048]) | ([13579] [26])) (00)) | (/ d {2} (([02468] [48]) | ([13579] [26])) (00)) | (/ d {2} ([02468] [48]) | [13599 ] [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. Retour R4.Test (Datestr); 49. Alerte (Date.IsvaliTime ('23: 59: 59 ',' HH: MM: SS ')); // Vrai 62. * (2) Alert (Date.isvalime ('24 -68-89', 'HH: MM: SS')); // FALSE 63. * Formats obligatoires, Date STRING 64. (1. "HH: MM: SS"; // Format par défaut: HH: MM: SS 73.} 74. If (Timestr.Length! = Formatstr.Length) {75. RETOUR FAUX; 76.} Else {77. R1 = / ^ ([0-1] [0-9]) | (2 [0-3])) /: ([0-5] [0-9]) /: ([0-5] [0-9]) $ /; 79. R2 = / ^ ([0-1] [0-9]) | (2 [0-3]) / - ([0-5] [0-9]) / - ([0-5] [0-9]) $ /; 82. R3 = / ^ ([0-1] [0-9]) | (2 [0-3]) // [0-5] [0-9]) // [0-5] [0-9]) $ /; 85. FALSE; DateTimeReg = / ^ (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( (((((((((((([02468] [048])))))) | (((02469]) | (11)) / - ((((((((((((((((. (((((((((((((((([02468][48]))|((((((((((((((((((((((((((((((((((( ((((((((((((((((((((((((((((((((((((((1 ((1 ((1 ((1 ((1 ((1 1 ((1 ((1 ((1 ((1 (((1 (1 ((1 (1 ((1 (1 (1 (1 ((1 (1 ((1 (1 (1 (1 (1 ((1 (1 (1 ((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1(( 1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1((1( (1 (1 (1 (1 (1 (1 ((1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 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])))))))))))))) /: ([0-5] [0-9]) /: ([0-5] [0-9]))? $ / 102. RETOUR DateTimeReg.Test (DateTimestr); 103.} 104. 105. 106./** 107. * Judge Leap Years: La règle générale est: une année de saut pendant quatre ans, pas un pendant cent ans, et une autre année de 40 ans. (this.getyear ()% 4 == 0 && ((this.getyear ()% 100! = 0) || (this.getyear ()% 400 == 0))); D'accord: 119. * (1) yyyy / yyy / yy / yy représente l'année 120. * (2) mm / m mois 121. * (3) W / W Week 122. * (4) dd / dd / d / d date 123. * (5) HH / HH / H / H Time 124. * (6) mm / m minute 125. 126. * (8) III millisecondes 127. * / 128.date.prototype.format = fonction (formatstr) 129. {130. Var str = formatstrt; [Day ',' One ',' Two ',' Three ',' Four ',' Five ',' Six ']; 135. 136. Str = Str.replace (/yyyyedAyyyy/,this.getlyar ()); 100) .toString (): '0' + (this.getyear ()% 100)); 138. 139. Str = str.replace (/ mm str = str.replace (/ m / g, (parseInt (this.getmonth ()) + 1)); this.getDate ()); 145. Str = str.replace (/dd|d/g,this.getDate ()); str = str.replace (/h|h/g,this.Gethours ()); str = str.replace (/sss|ss/,this.getSeconds ()> 9? this.getSeconds (). toString (): '0' + this.getSeconds ()); this.getMilliseConds () <10? '00' + this.getMilliseconds () :( This.getMilliseconds () <100? '0' + this.getMilliseconds (): this.getMilliseconds ())); Requis, chaîne de date, s'il ne peut pas être analysé dans le type de date, renvoyez NULL 164. * Format: 165. * (1) yyyy / mm / dd: ie et ff General 166. * (2) mm / dd / yyyy: ie et ff sont communs 167. * (3) mm-dd-yyy: IE seulement 168. * (4) analysé à 8 heures 169. * / 170.Date.Stringtodate = fonction (datestr) 171. {172. if (! Datestr) {173. Alert ("La chaîne ne peut pas être analysée comme une date"); 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-yyy") {181. RETOUR DE NOUVEAU (DATE.PARSE (Datestr)); 182.} Else {183. Alert ("Stream ne peut pas être analysé comme date if (date.isvalidate (datestr, "yyyy-mm-dd") {188. RETOUR DE NOUVEAUX (DATE.PARSE (Datestr)); 189.} Else {190. Calculez la différence dans le nombre de jours entre deux dates: 202. * DateOne: requise, doit être une instance du type de données 203. * Datetwo: requise, doit être une instance du type de données 204. * / 205.Date.Daysbetween = Function (DateOne, Datetwo) 206. {207. If ((date de la date de la date) = 7. 209.} Else {210. Return math.abs (math.floor ((dateOne.getTime () - Datetwo.getTime ()) / 1000/60/60/24)); Numéro, et les nombres positifs sont l'ajout de périodes, et les nombres négatifs sont soustraits 218. * Champ: facultatif, identifiant quel champ est ajouté ou soustrait, les champs sont affichés dans la convention suivante. * (5) H / H Hour 225. * (6) M MINUTE 226. * (7) S / S Second 227. * (8) Q / Q Saison 228. * / 229.Date.prototype.dateadd = fonction (num, champ) 230. {231. IF (! if (! field) {235. field = "d"; 236.} 237. Switch (Field) {238. Case 'y': 239. Case 'y': return date ((this.getlyar () + num), this.getMonth (), this.getDate (), this.gethours); 241. Case 'Q': Renvoie une nouvelle date (this.getlyear (), (this.getMonth () + num * 3), this.getDate (), this.Gethours (), this.getMinutes (), this.getSeconds ()); Break; 242. this.Gethours (), this.getMinutes (), this.getSeconds ()); Break; 243. Case 'W': Rendre la nouvelle date (Date.Parse (This) ((86400000 * 7) * NUM); Break; 248. CASE 'H': RETOUR DE NOUVELLE (DATE.PARSE (This) + (3600000 * NUM); Break; 249. Ceci; 255.} 256. 257. 258./** 259. (2) M mois 265. * (3) W / W Semaine 266. * (4) D / D Day 267. * (5) H / H Time 268. * (6) M Minute 269. * (7) S / S Second 270. * / 271.Date.prototype.Datediff = Fonction (Dtend, Field) 272. {273. Var Dtstart = This; Date) == false) {275. Retour 0; 276.} Else {277. If (! Field) {278. (dtend.getMonth () + 1) + ((dtend.getlyar () - dtstart.getlyar () * 12) - (dtstart.getMonth () + 1); Break; 284. Case 'W': return (dtend.getmonth () + 1); Break; (dtend.getmonth () + 1) + ((dtend.getlyar () - dtStart.getlyar () * 12) - (dtstart.getMonth () + 1); Break; 284. Case 'W': RETOUR PARSEINT ((Dtend - Dtstart) / (86400000 * 7); PARSEINT ((DTEND - DTSTART) / 86400000); Break; 288. Case 'H': 289. Case 'H': Retour ParseInt ((DTEND - DTSTART) / 3600000); Break; RETOUR PARSEINT ((DTEND - DTSTART) / 60000); Break; 291. Case 'S': 292. Case 'S': 292. CASE 'M': RETOUR PARSEINT ((DTEND - DTSTART) / 60000); Break; 291. Cas 'S': 292. Case 'S': 292. Case '' S ': RETOURS PARSEINT ((DTEND - DTSTART) / 1000); Break; 302. * / 303 MyArray [4] = this.getMinutes (); 311. Y / y Année 320. * (2) M MOIS 321. * (3) W / W Week 322. * (4) D / D Day 323. * (5) H / H Hour 324. * (6) M Minute 325. * (7) S / S Second 326. * / 327. Date.prototype.DatePart = FONCTION) 328. {329. = "D"; 331.} 332. 'W': 338. Case 'W': Return Week [celle-là ()); Break; 339. this.getSeconds (); Break; 345. Default: Retour.getDate (); 346.} 347.Le résumé ci-dessus des opérations communes sur les types de date dans JavaScript est tout le contenu que je partage avec vous. J'espère que vous pourrez vous faire référence et j'espère que vous pourrez soutenir Wulin.com plus.