获取今天日期(yyy-MM-dd格式)
function getCurDate () {
const date = new Date()
const year = date.getFullYear()
let month = date.getMonth() + 1
month = month > 9 ? month : `0${
month}`
let day = date.getDate()
day = day > 9 ? day : `0${
day}`
return `${
year}-${
month}-${
day}`
},
时间转换为yyyy-MM-dd
function getDate (d) {
var date = new Date(d)
var year = date.getFullYear()
var month = date.getMonth() + 1
month = month > 9 ? month : `0${
month}`
var day = date.getDate()
day = day > 9 ? day : `0${
day}`
return `${
year}-${
month}-${
day}`
}
时间转换为(yyyy-MM-dd hh:mm:ss格式)
function formatTime (d) {
var date = new Date(d)
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
获取某月份的天数
// 获取某月份下的天数
function getSomeMonthDays (year, month) {
let febDays = year % 4 === 0 ? 29 : 28
let days = [31, febDays, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (month <= days.length) {
return days[month - 1]
}
return ''
},
获取当天周几(具体日期周几)
function getTodayWeek(date) {
var arr = new Array("日", "一", "二", "三", "四", "五", "六");
var week;
if (date === undefined) {
week = new Date().getDay();
} else {
week = new Date(date).getDay();
}
return '周' + arr[week]
}
获取某个月份1号周几
// 获取某个月份1号周几
function getSomeMonthFirDayWeek (year, month) {
const firDate = new Date(`${
year}-${
month}-1`)
return firDate.getDay()
},
获取当前周(或具体日期的当周)周一日期格式:yyyy-MM-dd
function getMondayDate(date) {
var now = new Date();;
var nowTime;
if (date === undefined) {
nowTime = now.getTime();
} else {
now = new Date(date);
nowTime = now.getTime();
}
var day = now.getDay();
var oneDayTime = 24 * 60 * 60 * 1000;
//显示周一
var MondayTime = nowTime - (day - 1) * oneDayTime;
//初始化日期时间
var monday = new Date(MondayTime);
var y = monday.getFullYear();
var m = monday.getMonth() + 1;
m = m > 9 ? m : `0${
m}`;
var d = monday.getDate();
d = d > 9 ? d : `0${
d}`;
return `${
y}-${
m}-${
d}`;
}
获取当前周(具体天当周)周日日期格式:yyyy-MM-dd
function getSundayDate(date) {
var now = new Date();
var nowTime;
if (date === undefined) {
nowTime = now.getTime();
} else {
now = new Date(date);
nowTime = now.getTime();
}
var day = now.getDay();
var oneDayTime = 24 * 60 * 60 * 1000;
//显示周日
var MondayTime = nowTime + (7 - day) * oneDayTime;
//初始化日期时间
var monday = new Date(MondayTime);
var y = monday.getFullYear();
var m = monday.getMonth() + 1;
m = m > 9 ? m : `0${
m}`;
var d = monday.getDate();
d = d > 9 ? d : `0${
d}`;
return `${
y}-${
m}-${
d}`;
}
获取某月第一天和最后一天日期时间戳数组
// 获取当前月第一天和最后一天日期时间戳数组例如yearMonth=2022-11
function getCurrentMonthStarEnd (yearMonth) {
let year = Number(yearMonth.split('-')[0])
let month = Number(yearMonth.split('-')[1])
let arr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (year % 4 === 0) {
arr.splice(1, 1, 29)
}
let lastDayTimeStamp = new Date(`${
year}-${
month}-${
arr[month - 1]} 23:59:59`).getTime()
let firstDayTimeStamp = new Date(`${
year}-${
month}-01 00:00:00`).getTime()
return [firstDayTimeStamp, lastDayTimeStamp]
}
获取当月星期数组(日历)
//获取当月星期数组
function getWeekArr (year,month) {
let week = getSomeMonthFirDayWeek(year,month) // x月第一天周几
let weekArr = ['日', '一', '二', '三', '四', '五', '六']
let dataList = []
let totalDays=getSomeMonthDays(year,month)//当月总天数
for (let i = 0; i < totalDays; i++) {
dataList.push(weekArr[(week + i) % 7])
}
return dataList
}
// 获取某个月份1号周几
function getSomeMonthFirDayWeek (year, month) {
const firDate = new Date(`${
year}-${
month}-1`)
return firDate.getDay()
}
// 获取某月份下的天数
function getSomeMonthDays (year, month) {
let febDays = year % 4 === 0 ? 29 : 28
let days = [31, febDays, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (month <= days.length) {
return days[month - 1]
}
return ''
}
两个日期对比
扫描二维码关注公众号,回复:
14623812 查看本文章
function dateCompare(startDate, endDate){
startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
if (startDate <= endDate) {
return true
} else {
return false
}
}