timeUtil.js 1.1 KB

123456789101112131415161718192021222324252627
  1. Date.prototype.format = function (format) {
  2. var o = {
  3. "M+": this.getMonth() + 1, // month
  4. "d+": this.getDate(), // day
  5. "h+": this.getHours(), // hour
  6. "m+": this.getMinutes(), // minute
  7. "s+": this.getSeconds(), // second
  8. "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
  9. "S": this.getMilliseconds()
  10. // millisecond
  11. }
  12. if (/(y+)/.test(format))
  13. format = format.replace(RegExp.$1, (this.getFullYear() + "")
  14. .substr(4 - RegExp.$1.length));
  15. for (var k in o)
  16. if (new RegExp("(" + k + ")").test(format))
  17. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  18. return format;
  19. }
  20. function checkDateTime(str) {
  21. var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
  22. var r = str.match(reg);
  23. if (r == null) return false;
  24. var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
  25. return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
  26. }