Hello I have date in this format 03/08/12
where 03 is month, 08 is day and 12 is year. Now I want to add days in it. I have tried following but not getting the exact result
checkindate = new Date($("#checkindate").val());
checkindate.setDate(checkindate.getDate()+no_of_nights);
$("#checkoutdate").val((checkindate.getMonth()+1)+"/"+checkindate.getDate()+"/"+checkindate.getYear());
But is is giving unexpected result and I am unable to understand in which part of date it is adding. Can Any body tell me how can I do this?
Regards
What you have now will not work in all situations because you are adding the no_of_nights
to the Day value which means you could have 31 + 5, setDate() will not give you the expected outcome with a value of 36.
You must figure out how many months, days, years, etc. you want to add.
Javascript works off of miliseconds from 1/1/1970 and not normal time.
Your code works perfectly, but make sure no_of_nights
is not a string . That's probably your issue: the addition operation checkindate.getDate()+no_of_nights
becomes a concatenation operation if no_of_nights
is a string.
8 + 5 = 13
8 + "5" = "85"
Very different!
Solution: use parseInt(no_of_nights)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.