admin管理员组文章数量:1134247
I have a form input with an id of 'date_trans'. The format for that date input (which is validated server side) can be any of:
- dd/mm/yyyy
- dd-mm-yyyy
- yyyy-mm-dd
- yyyy/mm/dd
However, before posting the form, I'd like to check if the date_trans field has a date that is equal to today's date. Its ok if the date taken is the client's date (i.e. it uses js), since I run a double check on the server as well.
I'm totally lost on how to do the date comparrison in jQuery or just plain old javascript. If it helps, I am using the jquery datepicker
I have a form input with an id of 'date_trans'. The format for that date input (which is validated server side) can be any of:
- dd/mm/yyyy
- dd-mm-yyyy
- yyyy-mm-dd
- yyyy/mm/dd
However, before posting the form, I'd like to check if the date_trans field has a date that is equal to today's date. Its ok if the date taken is the client's date (i.e. it uses js), since I run a double check on the server as well.
I'm totally lost on how to do the date comparrison in jQuery or just plain old javascript. If it helps, I am using the jquery datepicker
Share Improve this question asked Nov 21, 2011 at 17:02 JonoBJonoB 5,88717 gold badges59 silver badges77 bronze badges 4- BTW, those are some silly date formats you're accepting. If you have any control over the decision to accept them, I'd suggest rethinking the usefulness/necessity of them all. – Phrogz Commented Nov 21, 2011 at 17:13
- What's silly about them? We operate in multiple countries, and they are accepted formats. I suppose that you think that everyone works on the (ridiculous) US format? – JonoB Commented Nov 21, 2011 at 20:52
- 2 Goodness, no! The US Format is appalling, but it's not present in your list and yet would be easily confused by your first case. If you are sure that all four are necessary, by all means use them. To me it seems like you have chosen to be both arbitrarily permissive and arbitrarily restrictive at the same time. shrug – Phrogz Commented Nov 21, 2011 at 21:04
- The US Format works just fine and has worked for many MANY years. – metal_jacke1 Commented Oct 21, 2020 at 17:56
12 Answers
Reset to default 237A simple date comparison in pure JS should be sufficient:
// Create date from input value
var inputDate = new Date("11/21/2011");
// Get today's date
var todaysDate = new Date();
// call setHours to take the time out of the comparison
if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
// Date equals today's date
}
Here's a working JSFiddle.
For completeness, taken from this solution:
You could use toDateString
:
const today = new Date()
const isToday = today.toDateString() === otherDate.toDateString()
No library dependencies, and looking cleaner than the setHours()
approach shown in a previous answer, imho.
Try using moment.js
moment('dd/mm/yyyy').isSame(Date.now(), 'day');
You can replace 'day' string with 'year, month, minute' if you want.
function sameDay( d1, d2 ){
return d1.getUTCFullYear() == d2.getUTCFullYear() &&
d1.getUTCMonth() == d2.getUTCMonth() &&
d1.getUTCDate() == d2.getUTCDate();
}
if (sameDay( new Date(userString), new Date)){
// ...
}
Using the UTC* methods ensures that two equivalent days in different timezones matching the same global day are the same. (Not necessary if you're parsing both dates directly, but a good thing to think about.)
Just use the following code in your javaScript:
if(new Date(hireDate).getTime() > new Date().getTime())
{
//Date greater than today's date
}
Change the condition according to your requirement.Here is one link for comparision compare in java script
The following solution compares the timestamp integer divided by the values of hours, minutes, seconds, millis.
var reducedToDay = function(date){return ~~(date.getTime()/(1000*60*60*24));};
return reducedToDay(date1) == reducedToDay(date2)
The tilde truncs the division result (see this article about integer division)
Try this
// method to check date is less than today date
isLessDate(schedule_date : any){
var _schedule_date = new Date(schedule_date);
var date = new Date();
var transformDate = this.datePipe.transform(date, 'yyyy-MM-dd');
var _today_date = new Date(''+transformDate);
if(_schedule_date < _today_date){
return 'small'
}
else if(_schedule_date > _today_date){
return 'big'
}
else {
return 'same'
}
}
You can use this function to figure this one out (86_400_000 is milliseconds per a day).
function isSameDay(date1, date2) {
return Math.abs(date1.getTime() - date2.getTime()) < 86_400_000
&& date1.getDate() === date2.getDate();
}
isSameDay(new Date("2011-11-21"), new Date("2011-11-21")); // true
isSameDay(new Date("2011-11-21"), new Date("2011-11-22")); // false
Date.js is a handy library for manipulating and formatting dates. It can help in this situation.
The Best way and recommended way of comparing date in typescript
is:
var today = new Date().getTime();
var reqDateVar = new Date(somedate).getTime();
if(today === reqDateVar){
// NOW
} else {
// Some other time
}
TodayDate = new Date();
if (TodayDate > AnotherDate) {} else{}
< = also works, Although with =, it might have to match the milliseconds.
There is a simpler solution
if (inputDate.getDate() === todayDate.getDate()) {
// do stuff
}
like that you don't loose the time attached to inputDate
if any
本文标签: javascriptHow to check if input date is equal to today39s dateStack Overflow
版权声明:本文标题:javascript - How to check if input date is equal to today's date? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736761763a1951587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论