Reputation: 615
I have a scenario where I have to parse two dates for example start date and end date.
var startdate = '30-04-2014 05:30:00 PM';
var enddate = '02-05-2014 05:52:36 PM';
But if we alert start date
alert(Date.Parse(startdate));
I will get 1465041600000
but if I alert enddate
alert(Date.Parse(enddate));
I will get 1391602956000
But this is not working properly, because the enddate
is larger than startdate
, but after parsing we will get larger value for start date. I want to get the difference between these 2 days.
Can anybody know a workaround for this?
Upvotes: 1
Views: 4582
Reputation: 1
I think your Strings are wrong. 02-05-2014 is February the 5th, and 30-04-2014 does not exist.
But there is the Date "04-30-2014" which is April the 30th.
I think that is your problem.
Upvotes: 0
Reputation:
Date.parse('30-04-2014 05:30:00 PM') // returns NAN!
I Assume you mean 30th April 2014 and 2nd May 2014 in which case it should be (mm-dd-yyyy)
Date.parse('04-30-2014 05:30:00 PM') // returns 1398844800000
and
Date.parse('05-02-2014 05:30:00 PM') // returns 1399018956000
Upvotes: 2
Reputation: 57
This can be done in java script itself, try following code
var x = new Date('your start goes here');
var y = new Date('your end date goes here');
var timeDiff = Math.abs(y.getTime() - x.getTime());
var days = Math.ceil(timeDiff / (1000 * 3600 * 24));
The variable days has diff b/w two days
Upvotes: 0
Reputation: 641
var date = new Date(); initialized date from the current time
date.setDate(arrival.getDate()+nights); sets the day of month in date to the day of month in arrival and added +nights.
If today is in May, date is in May, and if arrival is in June, the 2nd line does not set the month in date to June.
Try this:
var arrival = parseDate($('#arrivaldate').val());
var depart = new Date(arrival);
var nights = parseInt($('#nights').val());
depart.setDate(arrival.getDate()+nights);
console.log(depart);
Upvotes: 0
Reputation: 156938
Your date format is wrong. According to MDN:
Alternatively, the date/time string may be in ISO 8601 format. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed.
So:
ISO 8601 format: yyyy-mm-dd
Your format: dd-mm-yyyy
Upvotes: 1