时间(下)

获取当前的日期(返回格式: YYYY-mm-dd)

function getCurrentDate(date) {

let month = parseInt(date.getMonth() + 1);

let day = date.getDate();

if (month < 10) {

month = '0' + month

} if (day < 10) {

day = '0' + day ​ } return date.getFullYear() + '-' + month + '-' + day; }

获取本周的第一天

返回格式: YYYY-mm-dd

例子: 当日为: 2020-11-27

返回日期为: 2020-11-23

function getCurrentWeekFirstDay(date) {

let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)

let firstMonth = Number(weekFirstDay.getMonth()) + 1

if (firstMonth < 10) {

firstMonth = '0' + firstMonth }

let weekFirstDays = weekFirstDay.getDate();

if (weekFirstDays < 10) {

weekFirstDays = '0' + weekFirstDays; } ​ return weekFirstDay.getFullYear() + '-' + firstMonth + '-' + weekFirstDays; }

获取本周的最后一天

返回格式: YYYY-mm-dd

例子: 当日为: 2020-11-27

返回日期为: 2020-11-29 function getCurrentWeekLastDay(date) {

let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)

let weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000)

let lastMonth = Number(weekLastDay.getMonth()) + 1

if (lastMonth < 10) {

lastMonth = '0' + lastMonth ​ } ​ let weekLastDays = weekLastDay.getDate();

if (weekLastDays < 10) {

weekLastDays = '0' + weekLastDays; } ​ return weekFirstDay.getFullYear() + '-' + lastMonth + '-' + weekLastDays; }

获取当前月的第一天

返回格式: YYYY-mm-dd

例子: 当日为: 2020-11-27

返回日期为: 2020-11-01 function getCurrentMonthFirstDay() {

let date = new Date();

date.setDate(1);

let month = parseInt(date.getMonth() + 1);

let day = date.getDate();

if (month < 10) {

month = '0' + month ​ } if (day < 10) {

day = '0' + day }

return date.getFullYear() + '-' + month + '-' + day; }

获取当前月的最后一天

返回格式: YYYY-mm-dd

例子: 当日为: 2020-11-27

返回日期为: 2020-11-30

function getCurrentMonthLastDay() {

let date = new Date();

let currentMonth = date.getMonth();

let nextMonth = ++currentMonth;

let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);

let oneDay = 1000 * 60 * 60 * 24;

let lastTime = new Date(nextMonthFirstDay - oneDay);

let month = parseInt(lastTime.getMonth() + 1);

let day = lastTime.getDate();

if (month < 10) {

month = '0' + month

}

if (day < 10) {

day = '0' + day } return date.getFullYear() + '-' + month + '-' + day; }

​
let date = new Date();
// 例当日时间是 2020-11-27
getCurrentMonthIssue(date);         // result: 2020-11  --期号
getCurrentDate(date);               // result: 2020-11-27  --当前日期
getCurrentWeekFirstDay(date);       // result: 2020-11-23  --本周第一天时间
getCurrentWeekLastDay(date);        // result: 2020-11-29  --本周最后一天时间
getCurrentMonthFirstDay(date);      // result: 2020-11-01  --本月第一天时间
getCurrentMonthLastDay(date);       // result: 2020-11-30  --本月最后一天时间

获取上周第一天日期

getLastWeekData(){

let lastweek={};

let date=new Date();

date.setDate(date.getDate() - 7 -date.getDay() + 1);

lastweek.start_day=date.getFullYear() + "-" +(date.getMonth()+1) + "-" +date.getDate();

return lastweek.start_day

},

获取上周最后一天日期

getLastWeekData1(){

let lastweek={};

let date=new Date();

date.setDate(date.getDate() - 1 -date.getDay() + 1);

lastweek.end=date.getFullYear() + "-" +(date.getMonth()+1) + "-" + date.getDate();

return lastweek.end

},

获取当季第一天

function getFirstDayOfSeason (date) { ​ var month = date.getMonth(); ​ if(month <3 ){ ​ date.setMonth(0); ​ }else if(2 < month && month < 6){ ​ date.setMonth(3); ​ }else if(5 < month && month < 9){ ​ date.setMonth(6); ​ }else if(8 < month && month < 11){ ​ date.setMonth(9); ​ } ​ date.setDate(1); ​ return timeFormat(date); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值