newDate(0);//当天日期
newDate(-3);//3天前日期
newDate(3);//3天后日期
function newDate(day){
var today = new Date();
var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
today.setTime(targetday_milliseconds); //注意,这行是关键代码
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = doHandleMonth(tMonth + 1);
tDate = doHandleMonth(tDate);
return tYear+"-"+tMonth+"-"+tDate;
}
tab('2021-10-10','2021-10-11');
function tab(date1,date2){
var oDate1 = new Date(date1);
var oDate2 = new Date(date2);
if(oDate1.getTime() > oDate2.getTime()){
console.log('第一个大');
} else {
console.log('第二个大');
}
}
js获取当前日期,前几天日期,后几天日期,并比较两个日期大小
于 2022-03-25 09:41:32 首次发布
本文介绍了一个实用的JavaScript函数newDate,用于获取指定天数前后的日期,并通过一个示例展示了如何比较两个日期的大小。此外,还提供了一个tab函数来实现日期之间的比较。
9202

被折叠的 条评论
为什么被折叠?



