dateUtil.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const dateUtil = {
  2. // 日期标准格式
  3. DATE_yyyy_MM_dd() {
  4. return 'yyyy-MM-dd';
  5. },
  6. DATE_yyyy_MM_dd_TIME() {
  7. return 'yyyy-MM-dd HH:mm:ss';
  8. },
  9. DATE_full() {
  10. return 'yyyy-MM-dd hh:mm:ss';
  11. },
  12. DATE_yyyyMMdd() {
  13. return 'yyyyMMdd';
  14. },
  15. DATE_yyyyMMdd_china() {
  16. return 'yyyy年MM月dd日';
  17. },
  18. DATE_yyyyMMddTIME() {
  19. return 'yyyyMMddhhmmss';
  20. },
  21. DATE_yyyy_MM_dd_diagonal() {
  22. return 'yyyy/MM/dd';
  23. },
  24. // 日期格式化
  25. formateDate(newdate, format) {
  26. const date = {
  27. 'M+': newdate.getMonth() + 1,
  28. 'd+': newdate.getDate(),
  29. 'h+': newdate.getHours(),
  30. 'm+': newdate.getMinutes(),
  31. 's+': newdate.getSeconds(),
  32. 'q+': Math.floor((newdate.getMonth() + 3) / 3),
  33. 'S+': newdate.getMilliseconds()
  34. };
  35. if (/(y+)/i.test(format)) {
  36. format = format.replace(RegExp.$1, (newdate.getFullYear() + '').substr(4 - RegExp.$1.length));
  37. }
  38. for (const k in date) {
  39. if (new RegExp('(' + k + ')').test(format)) {
  40. format = format.replace(RegExp.$1, RegExp.$1.length === 1
  41. ? date[k] : ('00' + date[k]).substr(('' + date[k]).length));
  42. }
  43. }
  44. return format;
  45. },
  46. formatDate(dateString) {
  47. const year = dateString.slice(0, 4);
  48. const month = dateString.slice(4, 6);
  49. const day = dateString.slice(6, 8);
  50. return `${year}-${month}-${day}`;
  51. },
  52. // 根据时区转换时间
  53. timeZoneDate(currentDate) {
  54. // 获取设备的时区偏移量(分钟)
  55. const deviceTimezoneOffset = new Date().getTimezoneOffset();
  56. // 获取北京时间的时区偏移量(分钟)
  57. const beijingTimezoneOffset = -480; // -480分钟为北京时区偏移量
  58. // 计算设备时区与北京时区的偏移量差
  59. const offsetDifference = deviceTimezoneOffset - beijingTimezoneOffset;
  60. // 将设备的时区偏移量转换为毫秒数
  61. const offsetMilliseconds = offsetDifference * 60 * 1000;
  62. // 将北京时间的毫秒数加上设备的时区偏移量的毫秒数(仅对非北京时间进行调整)
  63. const adjustedMilliseconds = offsetMilliseconds !== 0 ? currentDate.getTime() + offsetMilliseconds : currentDate.getTime();
  64. // 使用调整后的时间创建一个新的Date对象
  65. const adjustedDate = new Date(adjustedMilliseconds);
  66. // 格式化调整后的时间
  67. const formattedDate = dateUtil.formateDate(adjustedDate, 'yyyy/MM/dd hh:mm:ss');
  68. return formattedDate;
  69. },
  70. // isSameWeek(old,now){
  71. // var oneDayTime = 1000*60*60*24;
  72. // var old_count =parseInt(old.getTime()/oneDayTime);
  73. // var now_other =parseInt(now.getTime()/oneDayTime);
  74. // return parseInt((old_count+4)/7) == parseInt((now_other+4)/7);
  75. // }
  76. // 获取本周第一天
  77. getFirstDayOfWeek(date = new Date()) {
  78. let firstDay = new Date(dateUtil.formateDate(date, "yyyy-MM-dd")); // 当前日期的 0 点
  79. let weekDay = firstDay.getDay(); // 星期几(0-6,0 表示星期天)
  80. let diff = weekDay === 0 ? 6 : weekDay - 1; // 本周第一天的偏移量
  81. firstDay.setDate(firstDay.getDate() - diff);
  82. return firstDay;
  83. },
  84. // 判断两个日期是否在同一周
  85. isSameWeek(date1, date2) {
  86. const startOfWeek = (date) => {
  87. const diff = date.getDay() - 1;
  88. return new Date(date.getFullYear(), date.getMonth(), date.getDate() - diff);
  89. };
  90. const start1 = startOfWeek(date1);
  91. const start2 = startOfWeek(date2);
  92. return start1.getTime() === start2.getTime();
  93. },
  94. }
  95. export default dateUtil;