截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Time.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var Times = {
  2. // 获取几天前日期
  3. getDay: function(day){
  4. var today = new Date();
  5. var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
  6. today.setTime(targetday_milliseconds);
  7. var tYear = today.getFullYear();
  8. var tMonth = today.getMonth();
  9. var tDate = today.getDate();
  10. tMonth = Times.doHandleMonth(tMonth + 1);
  11. tDate = Times.doHandleMonth(tDate);
  12. return tYear+"-"+tMonth+"-"+tDate;
  13. }
  14. // 处理月份
  15. ,doHandleMonth: function(month){
  16. var m = month;
  17. if(month.toString().length === 1){
  18. m = "0" + month;
  19. }
  20. return m;
  21. },
  22. /**
  23. * Notes: 日期时间格式 转 时间戳
  24. * date: 日期时间 如: 2020-12-1 13:23:11
  25. */
  26. tostrtime: function(date) {
  27. return Date.parse(new Date(date)) / 1000;
  28. },
  29. timestampToTime: function(timestamp) {
  30. var date = new Date(timestamp * 1000);
  31. var Y = date.getFullYear() + '-';
  32. var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  33. var D = date.getDate() + ' ';
  34. var h = date.getHours() < 10 ? '0'+date.getHours() + ':' : date.getHours() + ':';
  35. var m = date.getMinutes() < 10 ? '0'+date.getMinutes() + ':' : date.getMinutes() + ':';
  36. var s = date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds();
  37. return Y + M + D + h + m + s;
  38. },
  39. /**
  40. * Notes: 今天的开始时间和结束时间
  41. * isStamp: 是否转成时间戳
  42. */
  43. today: function(isStamp) {
  44. isStamp = isStamp === undefined ? true : isStamp;
  45. var start = Times.getDay(0) + " 00:00:00"
  46. var end = Times.getDay(0) + " 23:59:59"
  47. if (isStamp) {
  48. start = Times.tostrtime(start);
  49. end = Times.tostrtime(end);
  50. }
  51. return [start, end];
  52. },
  53. /**
  54. * Notes: 本月开始和结束
  55. * isStamp: 是否转成时间戳
  56. */
  57. month: function(isStamp) {
  58. isStamp = isStamp === undefined ? true : isStamp;
  59. var date=new Date();
  60. // 开始时间
  61. var Year = date.getFullYear();
  62. var Month = (date.getMonth() +1) < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1;
  63. var start = Year + '-' + Month + '-01 00:00:00'
  64. // 结束时间
  65. var currentMonth=date.getMonth();
  66. var nextMonth=++currentMonth;
  67. var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
  68. var oneDay=1000*60*60*24;
  69. var lastDate = new Date(nextMonthFirstDay-oneDay);
  70. var endDate = lastDate.getFullYear()+"-"+((lastDate.getMonth()+1)<10?"0":"")+(lastDate.getMonth()+1)+"-"+(lastDate.getDate()<10?"0":"")+lastDate.getDate();
  71. var end = endDate + ' 23:59:59';
  72. if (isStamp) {
  73. start = Times.tostrtime(start);
  74. end = Times.tostrtime(end);
  75. }
  76. return [start, end];
  77. },
  78. /**
  79. * 获取几天前零点到现在/昨日结束的时间
  80. * Notes: 本月开始和结束
  81. * isStamp: 是否转成时间戳
  82. */
  83. dayToNow: function(day, isStamp){
  84. day = day === undefined ? -1 : day;
  85. isStamp = isStamp === undefined ? true : isStamp;
  86. var start = Times.getDay(day) + " 00:00:00";
  87. var end = Times.getDay(day) + " 23:59:59";
  88. if (isStamp) {
  89. start = Times.tostrtime(start);
  90. end = Times.tostrtime(end);
  91. }
  92. return [start, end];
  93. },
  94. /**
  95. * 获取当前时间戳
  96. */
  97. curTimestamp : function() {
  98. return Date.parse(new Date()) / 1000
  99. }
  100. };