JavaScript: Calculate the Difference Between Two Dates in Days (Without DST Bugs)
Calculate the number of days between two dates in JavaScript without daylight saving bugs. UTC method, inclusive counts, weeks, months and the Temporal API.
Notes
Security notes, SOC lab write-ups and tested code by Mahir Faysal, a SOC Level 1 cybersecurity learner in Bangladesh, plus notes from my Next.js and Gemini projects. Sample posts are marked and drafts stay in the admin area.
Calculate the number of days between two dates in JavaScript without daylight saving bugs. UTC method, inclusive counts, weeks, months and the Temporal API.
An ISO week number counts the weeks of the year under the ISO 8601 standard: every week runs Monday to Sunday, and week 1 is the week that contains the year's first Thursday (which is always the week containing 4 January).
There are 253 working days in 2027 in England and Wales, 252 in Scotland and 251 in Northern Ireland, assuming a Monday-to-Friday week.
Temporal is JavaScript’s modern replacement for Date, on by default in Node.js 26, Chrome 144 and Firefox 139. Tested examples for PlainDate, ZonedDateTime, Duration, adding months, time zones, DST and formatting.
Use Europe/London in PHP the safe way: date_default_timezone_set, DateTimeImmutable, GMT vs BST, store UTC, display dd/mm/YYYY, and avoid DST surprises.
Format any JavaScript date as dd/mm/yyyy with a five-line function or toLocaleDateString('en-GB'), plus time zones, parsing UK dates and the month-0 trap.
0%