检查日期大于2天从今天的日期JavaScript

u4vypkhs  于 2023-06-28  发布在  Java
关注(0)|答案(4)|浏览(105)

我需要检查日期是两天大于当前日期增量计数和显示
JS代码:

> var exceeds = 0;
>          var date = "25-02-2015";
>          var today = new Date();
>           if((new Date(today.getFullYear(), today.getMonth(), today.getDate()+2))>date) {    exceed+=1; }
> 
> console.log(exceeds);
x8diyxa7

x8diyxa71#

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = mm+'/'+dd+'/'+yyyy;
document.write(today);
var currentDate = "03/23/2015";
if(today <= currentDate ){
alert('yes');
}
yyhrrdl8

yyhrrdl82#

您可以将日期转换为Unix TimeStamp,即从1970-01-01 00:00:00到您指定日期的毫秒。这就是密码

var yourDate = new Date(Date.UTC(2015, 02, 25)).getTime(); //Get the timestamp 
var today = new Date().getTime();         
if(today - yourDate > 60 * 60 * 1000 * 24 * 2){alert('greater than 2 days')}
q3qa4bjr

q3qa4bjr3#

var exceeds = 0;
var date = "25-02-2015", date_val  = date.split("-");

// Getting timestamp of date
var otherDate = new Date(+date_val[2], +date_val[1], +date_val[0]).getTime();

// Get the timestamp for 2 days from now:
var timestamp = new Date().getTime() + (2* 24 * 60 * 60 * 1000)
if(otherDate>timestamp) exceeds++;
6qfn3psc

6qfn3psc4#

最好解析datetime而不是转换为timestamp:

const yourDate = Date.parse('2023-06-26 12:08:15');
const today = new Date().getTime();         
if(today - yourDate > 60 * 60 * 1000 * 24 * 2){
  alert('greater than 2 days')
}

相关问题