时间(下)
獲取當前的日期(返回格式: 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); }
總結
- 上一篇: [20170619]11G expand
- 下一篇: 易维帮助台:企业售后服务如何高效派单