SShortSingh.
Back to feed

Counting calendar days in JavaScript without off-by-one errors

0
·11 views

Calculating the number of days between two dates in JavaScript is trickier than it looks, due to time zones, daylight saving time, and how the Date object parses strings. Subtracting local-midnight timestamps across a clock change yields a 23- or 25-hour day, producing fractional or wrong results. Date-only ISO strings are parsed as UTC while date-time strings are parsed as local time, which can shift the day for some users. The suggested fix is to strip the time and zone by building UTC midnight from each date's calendar parts, parsing YYYY-MM-DD strings manually, and using Intl.DateTimeFormat to determine the user's current day in a given time zone. The author tested these approaches in Node 20.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Production PWA Lessons: Manifests, Workbox Caching, and Push Notifications

A DEV Community article argues that standard PWA tutorials stop short of what matters once real users install and rely on an app. It highlights manifest choices such as tagging start_url with a query parameter to track installs separately, and using properly padded maskable icons listed as separate entries. The piece recommends Workbox over hand-written service worker code, with a Laravel-oriented example that pairs NetworkFirst for pages, StaleWhileRevalidate for API data, and CacheFirst for images. It also addresses the problem of stale UIs after deployments, where an older service worker keeps controlling open tabs.

0
ProgrammingDEV Community ·

Sepuluh dari 30 Image Docker Populer Masih Memuat Root CA eMudhra yang Dicabut

Browser seperti Chrome dan Firefox sudah mencabut kepercayaan pada root emSign ECC Root CA - G3 setelah insiden kunci lemah di delapan intermediate CA eMudhra. Namun, image container tidak ikut diperbarui secara otomatis, sehingga aplikasi di dalamnya bisa tetap menerima sertifikat dari root tersebut. Pemindaian terhadap 30 image Docker populer pada 24 Oktober 2026 menemukan 10 image yang masih membawa G3. Sebagian image yang tampak sudah diperbaiki juga ternyata masih menyimpan root yang dicabut, seperti salinan certifi milik pip atau cacerts milik Java. Para pengembang disarankan memeriksa seluruh trust store dalam image dan melakukan rebuild dengan --pull.

0
ProgrammingDEV Community ·

Structured Chatbot Search for Gaming Supplier Invoices Prioritizes Field Accuracy

A developer argues that a gaming company's chatbot for finding supplier invoices must return correct, specific fields rather than plausible-sounding answers. The proposed design pairs embedding-based semantic search to retrieve relevant invoice passages with a chat model that outputs schema-constrained JSON. Keeping retrieval and extraction separate makes it easier to tell whether errors come from finding the wrong evidence or mapping it wrongly. The article recommends preserving source identifiers, validating output, and marking absent fields as missing, and it proposes evaluating exact field accuracy on held-out invoices. It also compares OpenAI, Google Vertex AI, Cohere, Pinecone, and an OpenAI-compatible gateway by setup cost and fit.

0
ProgrammingDEV Community ·

Ten Tools to Catch Bugs in AI-Generated Code Before Merge

Lint, type checks and security scans often pass AI-written pull requests, leaving logic flaws, missing edge cases and breaks outside the diff to reach main. A survey cited in the article found 28.2% of developers care a lot about AI code quality yet still rely on existing review to catch problems. The author recommends a four-layer pre-merge pipeline and ranks ten tools by the layer each fills, with Qodo placed first for checking whether code matches the linked ticket. Researchers found that up to 17% of LLM-written regression tests in 145 merged PRs asserted faulty behavior. Agent-written refactors also showed a higher rate of potential breaking changes than human ones, according to an MSR 2026 study.