最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check date with current date using jQuery - Stack Overflow

programmeradmin3浏览0评论

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
  • am just updating the value of $("#EndDate").val() is like that – SivaRajini Commented May 13, 2014 at 11:15
  • Updating a value would be $("#EndDate").val("5/13/2014") – putvande Commented May 13, 2014 at 11:15
  • no putvande ..am just saying the value of element is like but it is not a code understood. – SivaRajini Commented May 13, 2014 at 11:17
  • 1 @dowvoter without reason putting down votes is not good. – SivaRajini Commented May 13, 2014 at 11:57
  • Possible duplicate of How to check if input date is equal to today's date? – jww Commented Sep 18, 2018 at 5:28
Add a comment  | 

6 Answers 6

Reset to default 11

If:

$("#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..
}
发布评论

评论列表(0)

  1. 暂无评论