4

1、将毫秒,转换成时间字符串。例如说,xx 分钟

export function getDate(ms) {
  const day = Math.floor(ms / (24 * 60 * 60 * 1000));
  const hour =  Math.floor((ms / (60 * 60 * 1000) - day * 24));
  const minute =  Math.floor(((ms / (60 * 1000)) - day * 24 * 60 - hour * 60));
  const second =  Math.floor((ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60));
  if (day > 0) {
    return day + "天" + hour + "小时" + minute + "分钟";
  }
  if (hour > 0) {
    return hour + "小时" + minute + "分钟";
  }
  if (minute > 0) {
    return minute + "分钟";
  }
  if (second > 0) {
    return second + "秒";
  } else {
    return 0 + "秒";
  }
}

2、获取当前时间 timeStr 时分秒 字符串 格式为 xx:xx:xx(23:59:59)

export function getNowDateTime(timeStr) {
  let now = new Date();
  let year = now.getFullYear(); //得到年份
  let month = (now.getMonth() + 1).toString().padStart(2, "0"); //得到月份
  let day = now.getDate().toString().padStart(2, "0"); //得到日期

  if (timeStr != null) {
    return `${year}-${month}-${day} ${timeStr}`;
  }
  let hours = now.getHours().toString().padStart(2, "0") // 得到小时;
  let minutes = now.getMinutes().toString().padStart(2, "0") // 得到分钟;
  let seconds = now.getSeconds().toString().padStart(2, "0") // 得到秒;
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

3、时间戳转年月日时分秒(例:Thu Sep 01 2022 08:00:00 GMT+0800 (中国标准时间))

export function timestampToTime(time) {
    if (time) {
        var date = new Date(time) //时间戳为10位需*1000,时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '-';
        let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
        let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
        let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
        let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
        let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        return Y + M + D + h + m + s;
    } else {
        return ''
    }
}

4、时间戳转年月日星期(例:Thu Sep 01 2022 08:00:00 GMT+0800 (中国标准时间))

export function timestampToDay(time) {
    if (time) {
        var date = new Date(time) //时间戳为10位需*1000,时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '年';
        let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '月' : date.getMonth() + 1 + '月';
        let D = date.getDate() < 10 ? '0' + date.getDate() + '日' : date.getDate() + '日';
        const w = date.getDay();
        const weekObj = {
            1: '星期一',
            2: '星期二',
            3: '星期三',
            4: '星期四',
            5: '星期五',
            6: '星期六',
            0: '星期日',
        }
        return Y + M + D + weekObj[w];
    } else {
        return ''
    }
}

5、时间戳转年月(例:Thu Sep 01 2022 08:00:00 GMT+0800 (中国标准时间))

export function timestampToMonth(time) {
    if (time) {
        var date = new Date(time) //时间戳为10位需*1000,时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '年';
        let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '月' : date.getMonth() + 1 + '月';
        return Y + M;
    } else {
        return ''
    }
}

6、时间戳转年月日(例:Thu Sep 01 2022 08:00:00 GMT+0800 (中国标准时间))

export function timestamp(time) {
    if (time) {
        var date = new Date(time) //时间戳为10位需*1000,时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '-';
        let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
        let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate();
        return Y + M + D;
    } else {
        return ''
    }
}

7、获取本年第几周

export function getYearWeek (a, b, c) {//a为年 b为月 c为日
    /*  
    date1是当前日期  
    date2是当年第一天  
    d是当前日期是今年第多少天  
    用d + 当前年的第一天的周差距的和在除以7就是本年第几周  
    */
        var date1 = new Date(a, parseInt(b) - 1, c),
            date2 = new Date(a, 0, 1),
            d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
        return Math.ceil((d + ((date2.getDay() + 1) - 1)) / 7);
};

getYearWeek ('2019', '04', '01')  //14

8、获取本月第几周

export function getMonthWeek(a, b, c) {
            /**
        * a = d = 当前日期
        * b = 6 - w = 当前周的还有几天过完(不算今天)
        * a + b 的和在除以7 就是当天是当前月份的第几周
        */
        var date = new Date(a, parseInt(b) - 1, c),
            w = date.getDay(),
            d = date.getDate();
        if(w==0){
            w=7;
        }
        var config={
            getMonth:date.getMonth()+1,
            getYear:date.getFullYear(),
            getWeek:Math.ceil((d + 6 - w) / 7),
        }
        return config;
    };
    var getDate=getMonthWeek("2018", "12", "31");
    console.log("今天是 " + getDate.getYear + " 年的第 "+ getDate.getMonth + " 月的第 " + getDate.getWeek + " 周");

墨城
1.7k 声望2.1k 粉丝