Reputation: 7197
I have a date time in javascript given in format dd.MM.yyyy HH.mm
.
What I need to check is whether this date fits in last 24 hours or not.
Example: If date and time now is 06.04.2017 18:26 (dd.MM.yyyy HH.mm) then minimal date allowed to enter is 05.04.2017 18:26. Is it possible to do this check with javascript?
Upvotes: 3
Views: 12724
Reputation: 34168
Use the Date object to do what you want - construct a Date object for each date, then compare them using the >, <, <= or >=.
Use of comparison ( ==, !=, ===, and !== ) requires you use date.getTime()
as in:
var date1 = new Date();
var date2 = new Date(date1);
var same = date1.getTime() === date2.getTime();
var notSame = date1.getTime() !== date2.getTime();
console.log(same,notSame);
var date1 = new Date('06.04.2017 18:26');
var date2 = new Date('05.04.2017 18:26');
var isGreaterorEq = date1.getTime() >= date2.getTime();
var lessThan = date2.getTime() < date1.getTime();
console.log(isGreaterorEq ,lessThan );
As for the 24 hours thing:
var date1 = new Date('05.06.2017 01:26');
var timeStamp = Math.round(new Date().getTime() / 1000);
var timeStampYesterday = timeStamp - (24 * 3600);
var is24 = date1 >= new Date(timeStampYesterday*1000).getTime();
console.log(is24,timeStamp,date1,timeStampYesterday );
Upvotes: 9
Reputation: 3040
First you have convert your date to time stamp. Then you have to do this calculation time stamp(now)-86400 you get the time stamp before 24hrs let us call the variable before_24 Then check your date time stamp if it is > before_24 or no.
Upvotes: 0
Reputation: 1883
Yes you can check it with the if condition
if (parseInt(date_variable_data) > parseInt(second_date_variable_data))
{
//do action
}else
{
//do action
}
Upvotes: -4