javascript - Calculate difference between 2 dates considering Daylight Saving Time -



javascript - Calculate difference between 2 dates considering Daylight Saving Time -

given start date, , number of days, need display end date = start date + number of days.

so did this:

var enddate=new date(startdate.gettime()+one_day);

everything works fine, except 25 , 26 oct gives 1 day less.

ex.:

2014-01-01 + 2 days = 2014-01-03 2014-10-25 + 2 days = 2014-10-26 (here case need treat).

this difference appear because of clock going 1 hour. practically 2014-10-27 00:00:00 becomes 2014-10-26 23:00:00.

a simple solution compute @ hr (example 3 am). want display note when happens.

for example, if user inputs 2014-10-25, show popup saying [something].

now here real problem... can't seem find algorithm says when clocks goes in year x.

example... in 2014 day 26 october. in 2016 30 oct (https://www.gov.uk/when-do-the-clocks-change). why? date looks random be, don't think is. so... when clock go back/forward?

edit: answers/comments helpful related how prepare problem. but... passed stage. have itch "how on earth days when clock changed computed?".

to find difference between 2 dates in whole days, create date objects, subtract 1 other, split milliseconds in 1 day , round. remainder out 1 hr daylight saving round right value.

you may need little function convert strings dates:

// homecoming date given iso date yyyy-mm-dd function parseisodate(ds) { var d = ds.split(/\d/); homecoming new date(d[0], --d[1], d[2]); }

get difference in days:

function datediff(d0, d1) { homecoming math.round((d1 - d0)/8.64e7); } // 297 console.log(datediff(parseisodate('2014-01-01'), parseisodate('2014-10-25')));

if want add together days date, like:

// add together 2 days 2014-10-25 var d = new date(2014, 9, 25); d.setdate(d.getdate() + 2); console.log(d); // 2014-10-27

the built–in date object takes business relationship of daylight saving (thought there bugs in browsers).

javascript date clock hour

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -