1./** 2. * 判断是不是一个正确的时间 yyyy-MM-dd 3. * @param {String} str 4. * @return {Date} 5. */ 6.Date.isSimpleDate = function(str){ 7. var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; 8. var r = str.match(reg); 9. if(r==null)return null; 10. var d= new Date(r[1], r[3]-1,r[4]); 11. var newStr=d.getFullYear() + "-"; 12. newStr+=(((d.getMonth()+1)<10&&r[3].length>1)?('0'+(d.getMonth()+1)):(d.getMonth()+1)) + "-" ; 13. newStr+=(((d.getDate())<10 && r[4].length>1)?('0'+d.getDate()):d.getDate()); 14. if(newStr==str){ 15. return d; 16. }else{ 17. return null; 18. } 19.} 20./** 21. * 判断是不是一个正确的时间类型 yyyy-MM-dd hh 22. * @param {Object} str 23. */ 24.Date.isDateHH = function(str){ 25. var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2})$/; 26. var r = str.match(reg); 27. if(r==null)return null; 28. var d= new Date(r[1], r[3]-1,r[4],r[5]); 29. var newStr=d.getFullYear() + "-"; 30. newStr+=(((d.getMonth()+1)<10&&r[3].length>1)?('0'+(d.getMonth()+1)):(d.getMonth()+1)) + "-" ; 31. newStr+=(((d.getDate())<10 && r[4].length>1)?('0'+d.getDate()):d.getDate()) + " "; 32. newStr+=((d.getHours()<10&&r[5].length>1)?('0'+d.getHours()):d.getHours()); 33. if(newStr==str){ 34. return d; 35. }else{ 36. return null; 37. } 38.} 39./** 40. * 判断是不是一个正确的时间类型 yyyy-MM-dd hh:mm 41. * @param {String} str 42. */ 43.Date.isDateHHMM = function(str){ 44. var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2})$/; 45. var r = str.match(reg); 46. if(r==null)return null; 47. var d= new Date(r[1], r[3]-1,r[4],r[5],r[6]); 48. var newStr=d.getFullYear() + "-"; 49. newStr+=(((d.getMonth()+1)<10&&r[3].length>1)?('0'+(d.getMonth()+1)):(d.getMonth()+1)) + "-" ; 50. newStr+=(((d.getDate())<10 && r[4].length>1)?('0'+d.getDate()):d.getDate()) + " "; 51. newStr += ((d.getHours()<10&&r[5].length>1)?('0'+d.getHours()):d.getHours())+":"; 52. newStr += ((d.getMinutes()<10&&r[6].length>1)?('0'+d.getMinutes()):d.getMinutes()); 53. if(newStr==str){ 54. return d; 55. }else{ 56. return null; 57. } 58.} 59./** 60. * 判断是不是一个正确的时间类型 yyyy-MM-dd hh:mm:ss 61. * @param {String} str 62. */ 63.Date.isDateHHMMSS = function(str){ 64. var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 65. var r = str.match(reg); 66. if(r==null)return null; 67. var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); 68. var newStr=d.getFullYear() + "-"; 69. newStr+=(((d.getMonth()+1)<10&&r[3].length>1)?('0'+(d.getMonth()+1)):(d.getMonth()+1)) + "-" ; 70. newStr+=(((d.getDate())<10 && r[4].length>1)?('0'+d.getDate()):d.getDate()) + " "; 71. newStr += (((d.getHours()<10&&r[5].length>1)?('0'+d.getHours()):d.getHours()))+":"; 72. newStr += (((d.getMinutes()<10&&r[6].length>1)?('0'+d.getMinutes()):d.getMinutes())) + ":"; 73. newStr += ((d.getSeconds()<10&&r[7].length>1)?('0'+d.getSeconds()):d.getSeconds()); 74. if(newStr==str){ 75. return d; 76. }else{ 77. return null; 78. } 79.} 80./*** 81. * 判断是日期1是不是在日期2后面 82. * @param {Date/String} d1 83. * @param {Date/String} d2 84. * @return {Boolean} true 小于日期2 85. */ 86.Date.before = function(d1,d2){ 87. if((typeof d1 == 'object' && d1.constructor == Date) && (typeof d2 == 'object' && d2.constructor == Date)){ 88. if(d1<=d2){ 89. return true; 90. }else{ 91. return false; 92. } 93. }else if((typeof d1 == 'string' && d1.constructor == String) && (typeof d2 == 'string' && d2.constructor == String)){ 94. 95. var date1 = Date.isSimpleDate(d1)!=null?Date.isSimpleDate(d1):Date.isDateHH(d1); 96. date1 = date1!=null?date1:Date.isDateHHMM(d1); 97. date1 = date1!=null?date1:Date.isDateHHMMSS(d1); 98. var date2 = Date.isSimpleDate(d2)!=null?Date.isSimpleDate(d2):Date.isDateHH(d2); 99. date2 = date2!=null?date2:Date.isDateHHMM(d2) 100. date2 = date2!=null?date2:Date.isDateHHMMSS(d2); 101. if(date1==null||date2==null){ 102. alert("日期格式不正确!"); 103. return false; 104. } 105. if(date1<=date2){ 106. return true; 107. }else{ 108. return false; 109. } 110. }else if((typeof d1 == 'string' && d1.constructor == String) && (typeof d2 == 'object' && d2.constructor == Date)){ 111. 112. var date1 = Date.isSimpleDate(d1)!=null?Date.isSimpleDate(d1):Date.isDateHH(d1); 113. date1 = date1!=null?date1:Date.isDateHHMM(d1); 114. date1 = date1!=null?date1:Date.isDateHHMMSS(d1); 115. if(date1==null||d2==null){ 116. alert("日期格式不正确!"); 117. return false; 118. } 119. if(date1<=d2){ 120. return true; 121. }else{ 122. return false; 123. } 124. }else if((typeof d1 == 'object' && d1.constructor == Date) && (typeof d2 == 'string' && d2.constructor == String)){ 125. 126. var date2 = Date.isSimpleDate(d2)!=null?Date.isSimpleDate(d2):Date.isDateHH(d2); 127. date2 = date2!=null?date2:Date.isDateHHMM(d2) 128. date2 = date2!=null?date2:Date.isDateHHMMSS(d2); 129. if(d1==null||date2==null){ 130. alert("日期格式不正确!"); 131. return false; 132. } 133. if(d1<=date2){ 134. return true; 135. }else{ 136. return false; 137. } 138. }else{ 139. alert("无法判断输入数据类型!"); 140. return false; 141. } 142.}