js获取当前时间并转变格式

比眉伴天荒 2022-06-12 01:06 397阅读 0赞
  1. /** * @description 把当前时间转成 (年.月.日 时:分:秒)日期格式的 * @params 无 * @return 返回当前时间的日期格式,例如:2017.07.11 15:14:44 */
  2. function getCurrentTime(){
  3. var date = new Date();
  4. var month = date.getMonth() + 1;
  5. var strDate = date.getDate();
  6. if (month >= 1 && month <= 9) {
  7. month = "0" + month;
  8. }
  9. if (strDate >= 0 && strDate <= 9) {
  10. strDate = "0" + strDate;
  11. }
  12. var hours = date.getHours();
  13. if (hours >=0 && hours <= 9) {
  14. if (hours == 0) {
  15. hours = "00";
  16. } else{
  17. hours = "0" + hours;
  18. }
  19. }
  20. var minutes = date.getMinutes();
  21. if (minutes >=0 && minutes <= 9) {
  22. if (minutes == 0) {
  23. minutes = "00";
  24. } else{
  25. minutes = "0" + minutes;
  26. }
  27. }
  28. var seconds = date.getSeconds();
  29. if (seconds >=0 && seconds <= 9) {
  30. if (seconds == 0) {
  31. seconds = "00";
  32. } else{
  33. seconds = "0" + seconds;
  34. }
  35. }
  36. var currentdate = date.getFullYear()+"."+ month+"."+strDate+" "+hours+":"+minutes+":"+ ":"+seconds;
  37. console.log(currentdate);
  38. //2017.07.11 15:14:44
  39. return currentdate ;
  40. }
  41. /** * @description 把传入的(年 月 日 时 分 秒 2017 7 5 13 8 5)转成无格式的日期(20170705130805) * @params year,month,strDate,hours,minutes,seconds 年 月 日 时 分 秒 例如:2017 7 5 13 8 5 * @return 返回传入参数的无格式日期 例如:20170705130805 */
  42. function getCurrentTime(year,month,strDate,hours,minutes,seconds) {
  43. if(month >= 1 && month <= 9) {
  44. month = "0" + month;
  45. }
  46. if(strDate > 0 && strDate <= 9) {
  47. strDate = "0" + strDate;
  48. }
  49. if(hours >= 0 && hours <= 9) {
  50. if (hours == 0) {
  51. hours = "00";
  52. } else{
  53. hours = "0" + hours;
  54. }
  55. }
  56. if(minutes >= 0 && minutes <= 9) {
  57. if (minutes == 0) {
  58. minutes = "00";
  59. } else{
  60. minutes = "0" + minutes;
  61. }
  62. }
  63. if(seconds >= 0 && seconds <= 9) {
  64. if (seconds == 0) {
  65. seconds = "00";
  66. } else{
  67. seconds = "0" + seconds;
  68. }
  69. }
  70. var currentdate = year + month + strDate + hours + minutes + seconds;
  71. console.log(currentdate);
  72. return currentdate;
  73. }
  74. /** * @description 方法入口 要求超时时间格式为20170705130805 * @params tiemOut 单位秒 例如:60 (60秒后超时) * @return */
  75. function sendRequest(){
  76. var curTime = new Date();
  77. var curTimeB = getCurrentTime(curTime.getFullYear(),curTime.getMonth()+1,curTime.getDate(),curTime.getHours(),curTime.getMinutes(),curTime.getSeconds());
  78. console.log("当前时间="+curTimeB);//打印这里为了对比转变后的超时时间
  79. var oldTime = curTime .getTime(); //获取当前时间的毫秒值
  80. //假如在1分钟后超时,转成毫秒值
  81. var timeOut = oldTime + 60 * 1000;//假如超时时间为当前时间之后的60秒
  82. console.log(timeOut);
  83. //获取超时时间的时间戳
  84. var timeOutDate = new Date(timeOut);
  85. console.log(timeOutDate);
  86. //获取超时时间的年月日时分秒
  87. var year = timeOutDate.getFullYear();
  88. var month = timeOutDate.getMonth()+1;
  89. var strDate = timeOutDate.getDate();
  90. var hours = timeOutDate.getHours();
  91. var minutes = timeOutDate.getMinutes();
  92. var seconds = timeOutDate.getSeconds();
  93. //转变成需要的日期格式。
  94. var time = getCurrentTime(year,month,strDate,hours,minutes,seconds);
  95. console.log("超时时间="+time);//20170705130805
  96. }

发表评论

表情:
评论列表 (有 0 条评论,397人围观)

还没有评论,来说两句吧...

相关阅读