date.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. export default {
  2. /*** 根据时间返回时间字符串 Input => date Output => 2022-11-12**/
  3. getDateStrByTime(date) {
  4. date = new Date(date)
  5. let year = date.getFullYear();
  6. let month = date.getMonth() + 1 >= 10 ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1));
  7. let day = date.getDate() >= 10 ? date.getDate() : ('0' + date.getDate());
  8. return `${year}/${month}/${day}`;
  9. },
  10. /***根据时间增减天数返回时间字符串 Input => date,count(正负增减) Output => 2022-11-12**/
  11. getStrByTimeIncreaseDay(date, count) {
  12. date = new Date(date)
  13. date = new Date(date.setDate(date.getDate() + count));
  14. return this.getDateStrByTime(date)
  15. },
  16. /***根据开始时间、结束时间获取该时间段日期字符串数组 Input => date1,date2 Output => ["2022-04-19",2022-04-20"] **/
  17. getDayAll(starDay, endDay) {
  18. var arr = [];
  19. var dates = [];
  20. // 设置两个日期UTC时间
  21. var db = new Date(starDay);
  22. var de = new Date(endDay);
  23. // 获取两个日期GTM时间
  24. var s = db.getTime() - 24 * 60 * 60 * 1000;
  25. var d = de.getTime() - 24 * 60 * 60 * 1000;
  26. // 获取到两个日期之间的每一天的毫秒数
  27. for (var i = s; i <= d;) {
  28. i = i + 24 * 60 * 60 * 1000;
  29. arr.push(parseInt(i))
  30. }
  31. // 获取每一天的时间 YY-MM-DD
  32. for (var j in arr) {
  33. var time = new Date(arr[j]);
  34. var year = time.getFullYear(time);
  35. var mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1));
  36. var day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate());
  37. var YYMMDD = year + '/' + mouth + '/' + day;
  38. dates.push(YYMMDD)
  39. }
  40. return dates
  41. },
  42. changeDateTime(date, format) {
  43. date = new Date(date)
  44. let year = date.getFullYear();
  45. let month = date.getMonth() + 1 >= 10 ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1));
  46. let day = date.getDate() >= 10 ? date.getDate() : ('0' + date.getDate());
  47. let str = year + "/" + month + "/" + day + ' ' + format
  48. console.log(str)
  49. return new Date(str);
  50. },
  51. isSameDay(date1, date2) {
  52. date1 = new Date(date1)
  53. date2 = new Date(date2)
  54. if (date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate()) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. }