时间(下)

获取当前的日期(返回格式: 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); }

上一篇:Leetcode Mysql 1565. 按月统计订单数与顾客数(DAY 10)


下一篇:Coffee and Coursework (Easy version)