1. Date.prototype.format = function (format) {
  2. var args = {
  3. "M+": this.getMonth() + 1,
  4. "d+": this.getDate(),
  5. "h+": this.getHours(),
  6. "m+": this.getMinutes(),
  7. "s+": this.getSeconds(),
  8. "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
  9. "S": this.getMilliseconds()
  10. };
  11. if (/(y+)/.test(format))
  12. format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  13. for (var i in args) {
  14. var n = args[i];
  15. if (new RegExp("(" + i + ")").test(format))
  16. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
  17. }
  18. return format;
  19. };
  20. 调用方法
  21. alert(new Date().format("yyyy-MM-dd hh:mm:ss:S"));
  22. alert(new Date().format("yyyy-MM-dd hh:mm:ss"));

————————————————————————————

  1. function getNowFormatDate() {
  2. var date = new Date();
  3. var seperator1 = "-";
  4. var seperator2 = ":";
  5. var month = date.getMonth() + 1;
  6. var strDate = date.getDate();
  7. if (month >= 1 && month <= 9) {
  8. month = "0" + month;
  9. }
  10. if (strDate >= 0 && strDate <= 9) {
  11. strDate = "0" + strDate;
  12. }
  13. var currentdate = date.getYear() + seperator1 + month + seperator1 + strDate
  14. + " " + date.getHours() + seperator2 + date.getMinutes()
  15. + seperator2 + date.getSeconds();
  16. return currentdate;
  17. }

————————————————————————————

  1. function curDateTime(){
  2. var d = new Date();
  3. var year = d.getYear();
  4. var month = d.getMonth()+1;
  5. var date = d.getDate();
  6. var day = d.getDay();
  7. var hours = d.getHours();
  8. var minutes = d.getMinutes();
  9. var seconds = d.getSeconds();
  10. var ms = d.getMilliseconds();
  11. var curDateTime= year;
  12. if(month>9)
  13. curDateTime = curDateTime +"-"+month;
  14. else
  15. curDateTime = curDateTime +"-0"+month;
  16. if(date>9)
  17. curDateTime = curDateTime +"-"+date;
  18. else
  19. curDateTime = curDateTime +"-0"+date;
  20. if(hours>9)
  21. curDateTime = curDateTime +""+hours;
  22. else
  23. curDateTime = curDateTime +"0"+hours;
  24. if(minutes>9)
  25. curDateTime = curDateTime +":"+minutes;
  26. else
  27. curDateTime = curDateTime +":0"+minutes;
  28. if(seconds>9)
  29. curDateTime = curDateTime +":"+seconds;
  30. else
  31. curDateTime = curDateTime +":0"+seconds;
  32. return curDateTime;
  33. }
  34. alert(curDateTime());

 

版权声明:本文为3box原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/3box/p/5748007.html