If a start date and number of days are given, I need to calculate the end date by adding the number of days to the start date.
This is the code snippet I used:
var endDate=new Date(startDate.getTime()+ONE_DAY);
Everything was working fine until I noticed that for 25 and 26 October, it gives one day less than expected.
For example:
2014-01-01 + 2 days = 2014-01-03
2014-10-25 + 2 days = 2014-10-26 (this is where the issue arises).
The discrepancy occurs due to the clock going back by 1 hour. Essentially, 2014-10-27 00:00:00
becomes 2014-10-26 23:00:00
.
A simple solution would be to perform the calculation at a different time (e.g., 3 AM). However, I prefer displaying a message when this situation occurs.
For instance, if the user enters 2014-10-25
, I want to show a popup message [something].
Now comes the real challenge... I am struggling to find an algorithm that determines when the clocks change in a specific year.
For example, in 2014, the change is on 26th October. In 2016, it's on 30th October (). Why are these dates chosen? It seems random to me, but there must be some reason behind it. So, how are the dates for daylight saving time determined?
EDIT: I appreciate all the answers and comments suggesting ways to fix the problem. However, I have moved past that phase. My curiosity now lies in understanding the logic behind determining the days when the clock changes.