admin管理员组文章数量:1355626
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?
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?
Share Improve this question asked Apr 6, 2017 at 16:28 FrenkyBFrenkyB 7,22716 gold badges74 silver badges125 bronze badges 3- Possible duplicate of Javascript code for showing yesterday's date and todays date – Hodrobond Commented Apr 6, 2017 at 16:30
- If you can bring in a library I suggest using MomentJS to do this. momentjs./docs/#/durations/subtract – Stephen Gilboy Commented Apr 6, 2017 at 16:32
- Yes I know for moment.js, but I can't use it right now. – FrenkyB Commented Apr 6, 2017 at 16:32
3 Answers
Reset to default 9Use the Date object to do what you want - construct a Date object for each date, then pare them using the >, <, <= or >=.
Use of parison ( ==, !=, ===, 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 );
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.
Yes you can check it with the if condition
if (parseInt(date_variable_data) > parseInt(second_date_variable_data))
{
//do action
}else
{
//do action
}
本文标签: jqueryJavascript check if date fits in last 24 hoursStack Overflow
版权声明:本文标题:jquery - Javascript check if date fits in last 24 hours - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743993898a2572640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论