js日期操作

时间:2021-10-22 05:43:06

1、最基本的日期操作

var mydate = new Date();

  • set/get   FullYear,Month,Date,Hour,Minutes,Second可以随意拼接
  • toLocaleDateString,toLocaleTimeString,toLocaleString可以获取时间字符串

2、前1天,后1天..

 function getNextDay(currentDate, scaleStep) {
var nextDate = new Date(currentDate);
for ( var hourIncreates = 1;; hourIncreates++) {
nextDate.setHours(nextDate.getHours() + scaleStep * hourIncreates);
if (nextDate.getDate() != currentDate.getDate()) {
dateStr = nextDate.getFullYear() + "-"
+ toDouble((nextDate.getMonth() + 1) + '') + "-"
+ toDouble(nextDate.getDate() + '');
break;
}
}
return dateStr;
}