I want to check my date is greater than current date
$("#EndDate").val() ="5/13/2014" ->M/d/y
Please find below code
if (Date.parse(new Date()) > Date.parse($("#EndDate").val())) {
//condition satisfied for today date too.
}
so my end date is today date.but still current date greater than end date. why ? how can i check and validate this. i understood some time value is greater than end date. but i want to check only date/month/year not time.
I want to check my date is greater than current date
$("#EndDate").val() ="5/13/2014" ->M/d/y
Please find below code
if (Date.parse(new Date()) > Date.parse($("#EndDate").val())) {
//condition satisfied for today date too.
}
so my end date is today date.but still current date greater than end date. why ? how can i check and validate this. i understood some time value is greater than end date. but i want to check only date/month/year not time.
Share Improve this question edited Sep 18, 2018 at 5:29 jww 102k103 gold badges441 silver badges939 bronze badges asked May 13, 2014 at 11:09 SivaRajiniSivaRajini 7,37522 gold badges84 silver badges129 bronze badges 5 |6 Answers
Reset to default 11If:
$("#EndDate").val();
returns a string in m/d/y format, you can turn that into a date object using:
function parseDate(s) {
var b = s.split(/\D/);
return new Date(b[2], --b[0], b[1]);
}
To create a comparable date, do what you are already doing:
var today = new Date();
today.setHours(0,0,0,0);
So now you can do:
if (parseDate($("#EndDate").val()) > today) {
// date is greater than today
}
or if you really must:
if (+parseDate($("#EndDate").val()) > new Date().setHours(0,0,0,0)) ...
Please note that when you do:
Date.parse(new Date().setHours(0, 0, 0, 0))
firstly a new date is created from new Date()
. Calling setHours()
sets the time value, but the return value from the call is the UTC time value of the Date object.
Date.parse expects a string that looks something like a date and time, so if you pass it a number time value something like 1399903200000, the implementation will fall back to some implementation heuristics to either turn it into a Date or NaN.
So please don't do that. Parsing any string with Date.parse is implementation dependent (even the strings specified in ECMA5) and will return different results in different browsers. So please don't do that either.
Try as below:
var end_date = "05/12/2014"
if(new Date() > new Date(end_date))
{
alert('End date should be greater than Start date');
}
fiddle
i found solution my self by modifying code like this
if (Date.parse(new Date().setHours(0, 0, 0, 0)) > Date.parse($("#EndDate").val())) {
//
}
to avoid time comparison. is there any other better way to do compare dates in efficient manner without any plugin.
var startDate = moment.parse("18/02/2013", "DD/MM/YYYY");
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 = dd+'/'+mm+'/'+yyyy;
if (today.isAfter(startDate)) {
...
}
Your asking for
check my date is greater than current date
and your giving us this ..
if (Date.parse(new Date()) > Date.parse($("#EndDate").val())) {
//condition satisfied for today date too.
}
just have to reverse your condition like that :
if (Date.parse(new Date()) < Date.parse($("#EndDate").val())) {
//condition satisfied for today date too.
}
However, I am late to answer this, but here is my approach to solve this issue:
var TodayDate = new Date();
var endDate= new Date(Date.parse($("#EndDate").val()));
if (endDate> TodayDate) {
// throw error here..
}
$("#EndDate").val("5/13/2014")
– putvande Commented May 13, 2014 at 11:15