获取日期(昨天、今天、本周第一天和最后一天、本月第一天和最后一天、本年第一天和最后一天)

1.1获取昨天的日期

	function getYesterdayDate() {
    // 获取昨天的时间戳
    let date = new Date()
    let timeStamp = date.getTime() - (24 * 60 * 60 * 1000)
    date.setTime(timeStamp)  //给new Date设置一个新的日期   
    let yesterdayDate = date.getFullYear() + "-" + (date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)) + "-" + (date.getDate() >= 10 ? date.getDate() : '0' + date.getDate());
    return yesterdayDate  //2022-06-10
}

1.2获取今天的日期

function getDayDate() {
    let date = new Date()
    let dayDate = date.getFullYear() + '-' + (date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)) + '-' + (date.getDate() >= 10 ? date.getDate() : '0' + date.getDate())
    return dayDate
}

1.3获取周的第一天的日期

function getWeekStartDay() {
    // 如果当前星期跨月  警告
    // 0:星期天  1:星期1
    let date = new Date()
    let week = date.getDay();  //获取周几 /0是周一
    let minus = week ? week - 1 : 6;  // 当前日期需要减去的天数(才是周一的天使/例如当前是周三,那么需要减去2天才是周一)
    date.setTime(date.getTime() - (24 * 60 * 60 * 1000 * minus)) //重新设置标准时间
    let weekStartStartDay = date.getFullYear() + '-' + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-' + (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
    return weekStartStartDay
}

1.4获取周的最后一天的日期

function getWeekEndDay() {
    let date = new Date();
    let week = date.getDay();
    console.log('week', week);
    let minus = week ? 7 - week : 0;
    date.setTime(date.getTime() + (24 * 60 * 60 * 1000 * minus))
    let weekEndEndDay = date.getFullYear() + '-' + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + '-' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
    return weekEndEndDay
}

1.5获取本月的第一天的日期

function getMonthStartDay() {
    // setDate(15)  //设置一个月的某一天(这里是一个月的15号)
    let date = new Date()
    date.setDate(1)
    let monthStartDay = date.getFullYear() + '-' + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth()) + '-' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate)
    return monthStartDay
}

1.6获取本月的最后一天的日期

function getCurrentMonthLast() {
    //思路:获取下一个月的第一天, 然后减去一天就是当前这个月的最后一天了
    let date = new Date()
    let currentMoth = date.getMonth()
    let nextMonth = ++currentMoth  //下一个月
    let currentMothStartDay = new Date(date.getFullYear(), nextMonth, 01)
    let currentMOthEndDay = new Date(currentMothStartDay - 24 * 60 * 60 * 1000)
    let monthEndDay = currentMOthEndDay.getFullYear() + '-' + (currentMOthEndDay.getMonth() + 1 < 10 ? '0' + (currentMOthEndDay.getMonth() + 1) : (currentMOthEndDay.getMonth() + 1)) + '-' + (currentMOthEndDay.getDate < 10 ? '0' + currentMOthEndDay.getDate() : currentMOthEndDay.getDate())
    return monthEndDay
}

1.7获取本年的第一天的日期

function getYearStartDay() {
    let date = new Date()
    let year = date.getFullYear()
    let yearStartDate = year + '-01' + '-01'
    date.setTime(yearStartDate)
    return yearStartDate
}

1.8获取本年的最后一天的日期

function getYearEndDay() {
    let date = new Date()
    let year = date.getFullYear()
    let yearEndDate = year + '-' + '12' + '-' + '31'
    date.setTime(yearEndDate)  //date.setTime('2022-12-31') //转换为标准时间
    return yearEndDate
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值