Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Fix a Temporal Polyfill Rounding a Date Wrong

- 20.11.26 - ErcanOPAK

⚡ Fix a Temporal Polyfill Rounding a Date Wrong at a Boundary

Calling .round() or .until()/.since() on the Temporal API’s polyfill without an explicit roundingMode defaults to halfExpand, a rounding rule that behaves differently on negative durations than most developers’ mental model of Math.round() built from years of working with positive numbers only. A duration that lands exactly on a half-unit boundary – common right at a date subtraction’s sign flip – can round in a direction that surprises anyone who assumed the default would just match Math.round().

⚡ The Problem

const start = Temporal.PlainDate.from("2026-03-10");
const end = Temporal.PlainDate.from("2026-03-08");

// Negative duration, exactly 2 days - no roundingMode specified,
// so it falls back to the default "halfExpand" behavior, which
// treats the sign of the duration differently than Math.round()
// would for the equivalent positive number.
const diff = end.until(start, { smallestUnit: "days" })
              .round({ smallestUnit: "weeks", roundingMode: undefined });

✅ Fix: Make the Rounding Mode an Explicit Choice

  • Pass roundingMode explicitly – trunc for a display-only truncated countdown, halfExpand only when you genuinely want conventional rounding – instead of relying on the default, since the default behaves differently on negative durations than most developers expect from Math.round().
  • Normalize a duration’s sign before rounding when only its magnitude matters, taking the absolute duration first rather than rounding a signed duration and then taking the absolute value afterward, since the two orders of operations don’t always agree exactly at a boundary.
  • Add a unit test for a date pair that produces an exact half-unit duration, not just a pair a few days or weeks apart, since boundary-rounding bugs by definition only show up exactly at the boundary.

🚨 Why This Is Dangerous

  • A countdown or “time ago” display that’s off by one unit only at exact half-boundaries is the kind of bug that passes every manual test someone tries by hand, then gets reported by confused users days apart with none of them easy to reproduce on demand.

A rounding mode you never chose is still a rounding mode – it’s just one the polyfill picked for you, and not always the one you’d have picked yourself.

— JavaScript Tip

Related posts:

JavaScript: Fix Array.prototype.sort() Changing the Order of Equal-Looking Elements After a Browser ...

JavaScript: Fix Array.prototype.sort() Changing the Order of Equal-Looking Elements After a Browser ...

Throttle Requests to Save Backend

Throttle Requests to Save Backend

JavaScript: Master Functional Array Methods for Clean Code

JavaScript: Master Functional Array Methods for Clean Code

Views: 5

Post navigation

C#: Fix a Partial Property Losing Its Backing Field Attribute
HTML5: Fix a Popover Attribute Closing the Wrong Element

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com