12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- const dateUtil = {
- // 日期标准格式
- DATE_yyyy_MM_dd() {
- return 'yyyy-MM-dd';
- },
- DATE_yyyy_MM_dd_TIME() {
- return 'yyyy-MM-dd HH:mm:ss';
- },
- DATE_full() {
- return 'yyyy-MM-dd hh:mm:ss';
- },
- DATE_yyyyMMdd() {
- return 'yyyyMMdd';
- },
- DATE_yyyyMMdd_china() {
- return 'yyyy年MM月dd日';
- },
- DATE_yyyyMMddTIME() {
- return 'yyyyMMddhhmmss';
- },
- DATE_yyyy_MM_dd_diagonal() {
- return 'yyyy/MM/dd';
- },
- // 日期格式化
- formateDate(newdate, format) {
- const date = {
- 'M+': newdate.getMonth() + 1,
- 'd+': newdate.getDate(),
- 'h+': newdate.getHours(),
- 'm+': newdate.getMinutes(),
- 's+': newdate.getSeconds(),
- 'q+': Math.floor((newdate.getMonth() + 3) / 3),
- 'S+': newdate.getMilliseconds()
- };
- if (/(y+)/i.test(format)) {
- format = format.replace(RegExp.$1, (newdate.getFullYear() + '').substr(4 - RegExp.$1.length));
- }
- for (const k in date) {
- if (new RegExp('(' + k + ')').test(format)) {
- format = format.replace(RegExp.$1, RegExp.$1.length === 1
- ? date[k] : ('00' + date[k]).substr(('' + date[k]).length));
- }
- }
- return format;
- },
- formatDate(dateString) {
- const year = dateString.slice(0, 4);
- const month = dateString.slice(4, 6);
- const day = dateString.slice(6, 8);
- return `${year}-${month}-${day}`;
- },
- // 根据时区转换时间
- timeZoneDate(currentDate) {
- // 获取设备的时区偏移量(分钟)
- const deviceTimezoneOffset = new Date().getTimezoneOffset();
- // 获取北京时间的时区偏移量(分钟)
- const beijingTimezoneOffset = -480; // -480分钟为北京时区偏移量
- // 计算设备时区与北京时区的偏移量差
- const offsetDifference = deviceTimezoneOffset - beijingTimezoneOffset;
- // 将设备的时区偏移量转换为毫秒数
- const offsetMilliseconds = offsetDifference * 60 * 1000;
- // 将北京时间的毫秒数加上设备的时区偏移量的毫秒数(仅对非北京时间进行调整)
- const adjustedMilliseconds = offsetMilliseconds !== 0 ? currentDate.getTime() + offsetMilliseconds : currentDate.getTime();
- // 使用调整后的时间创建一个新的Date对象
- const adjustedDate = new Date(adjustedMilliseconds);
- // 格式化调整后的时间
- const formattedDate = dateUtil.formateDate(adjustedDate, 'yyyy/MM/dd hh:mm:ss');
- return formattedDate;
- },
- // isSameWeek(old,now){
- // var oneDayTime = 1000*60*60*24;
- // var old_count =parseInt(old.getTime()/oneDayTime);
- // var now_other =parseInt(now.getTime()/oneDayTime);
- // return parseInt((old_count+4)/7) == parseInt((now_other+4)/7);
- // }
- // 获取本周第一天
- getFirstDayOfWeek(date = new Date()) {
- let firstDay = new Date(dateUtil.formateDate(date, "yyyy-MM-dd")); // 当前日期的 0 点
- let weekDay = firstDay.getDay(); // 星期几(0-6,0 表示星期天)
- let diff = weekDay === 0 ? 6 : weekDay - 1; // 本周第一天的偏移量
- firstDay.setDate(firstDay.getDate() - diff);
- return firstDay;
- },
- // 判断两个日期是否在同一周
- isSameWeek(date1, date2) {
- const startOfWeek = (date) => {
- const diff = date.getDay() - 1;
- return new Date(date.getFullYear(), date.getMonth(), date.getDate() - diff);
- };
- const start1 = startOfWeek(date1);
- const start2 = startOfWeek(date2);
- return start1.getTime() === start2.getTime();
- },
- }
- export default dateUtil;
|