// 获取现在日期格式
window.getNowFormatDate = function (format, seperator1) {
format = format || 'date'
seperator1 = seperator1 || '-';
let date = new Date(),
year = date.getFullYear(),
month = date.getMonth() + 1,
strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
switch (format) {
case 'time':
return currentdate = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
break;
case 'date':
return currentdate;
break;
case 'datetime':
return currentdate += ' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds()
break;
}
}
// 获取今日本周本月日期格式
window.getTimeFormatDate = function (category,format) {
format = format || 'date'
let date = new Date(), startTime = '', endTime = '', year = date.getFullYear(), month = date.getMonth()+1, today = date.getDate();
switch (category) {
case 'today':
// 本周一的日期
startTime = year + '-' + month + '-' + today;
// 本周日的日期
endTime = year + '-' + month + '-' + today;
break;
case 'week':
// 本周日的日期
let nowDayOfWeek = date.getDay() - 1 ,weekEndDate = new Date(year,date.getMonth(),today + (6 - nowDayOfWeek))
,myyear = weekEndDate.getFullYear(),mymonth = weekEndDate.getMonth() + 1,myweekday = weekEndDate.getDate();
if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
endTime = myyear + "-" + mymonth + "-" + myweekday;
date.setDate(date.getDate() - date.getDay() + 1);
startTime = year + '-' + month + '-' + date.getDate();
break;
case 'month':
startTime = new Date(year, month, 1)
endTime = new Date(year, month + 1, 0)
// 本月第一天
startTime = year + '-' + month + '-' + startTime.getDate();
// 本月最后一天
endTime = year + '-' + month + '-' + endTime.getDate();
break;
}
switch (format) {
case 'date':
return startTime + ' - ' + endTime;
case 'datetime':
return startTime + ' 00:00:00' + ' - ' + endTime + ' 23:59:59';
}
}
利用 Javascript Date实例来获取,现在、本周、本月、本日的时间格式
最新推荐文章于 2024-10-22 08:53:18 发布
本文介绍了一组JavaScript函数,用于获取当前日期、本周及本月的日期格式化字符串。包括了不同格式的需求如仅日期、仅时间及日期时间组合。
405





