Get the date of each day of the current month, get the date of each day of the current week
Calling code:
console.log('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --', getNowM(), getWeekDay())
Copy the code
Result: I am 2020-2-28 today
Encapsulation method:
function getDay(num, str) {
var today = new Date(a);var nowTime = today.getTime();
var ms = 24 * 3600 * 1000 * num;
today.setTime(parseInt(nowTime + ms));
var oYear = today.getFullYear();
var oMoth = (today.getMonth() + 1).toString();
if (oMoth.length <= 1) oMoth = '0' + oMoth;
var oDay = today.getDate().toString();
if (oDay.length <= 1) oDay = '0' + oDay;
return oYear + str + oMoth + str + oDay;
}
// Format the date
function formatDate(date) {
date = new Date(date);
let myyear = date.getFullYear();
let mymonth = date.getMonth() + 1;
let myweekday = date.getDate();
mymonth < 10 ? mymonth = "0" + mymonth : mymonth;
myweekday < 10 ? myweekday = "0" + myweekday : myweekday;
return `${myyear}-${mymonth}-${myweekday}`;
}
// Get the number of days in the current month
function mGetDate() {
var date = new Date(a);var year = date.getFullYear();
var month = date.getMonth() + 1;
var d = new Date(year, month, 0);
return d.getDate();
}
// Get the date of the week
function getWeekDay() {
let dateString = formatDate(new Date());// The date of the day, e.g. 2020-2-28
let presentDate = new Date(dateString);
lettoday = presentDate.getDay() ! = =0 ? presentDate.getDay() : 7;
return Array.from(new Array(7), function (val, index) {
return formatDate(new Date(presentDate.getTime() - (today - index - 1) * 24 * 60 * 60 * 1000));
});
}
// Get all the dates of the current month
function getNowM(e) {
let now = new Date(a);let current_month_num = mGetDate();
let current_month = [];
for (let i = 1; i <= current_month_num; i++) {
let day = now.setDate(i);
let everyDay = formatDate(day);
current_month.push(everyDay);
}
return current_month;
}
Copy the code