Understanding Date and Time Handling in JavaScript for Internationalization
This article explains JavaScript date and time concepts—including GMT, UTC, ISO 8601, RFC2822, timestamps, parsing, formatting, timezone offsets, localization, and practical internationalization strategies—providing developers with the knowledge to correctly manage time across different regions and platforms.
Time Concepts
When working on international projects, handling dates and times is essential. This section introduces basic time concepts such as GMT, UTC, CST, PST, and DST.
International Standards
GMT (Greenwich Mean Time)
GMT is the world standard time at the prime meridian, representing the 0‑offset time zone.
Calling new Date().toString() returns a US‑English date string that includes a GMT description.
UTC (Coordinated Universal Time)
UTC is theoretically equivalent to GMT, differing only by occasional leap seconds, which are negligible for everyday use.
JavaScript’s Date object provides several UTC‑based methods.
CST, PST
CST: UTC +8 (used in China, parts of the US, Australia, Cuba)
PST: UTC ‑8 (Pacific Standard Time)
DST (Daylight Saving Time)
Regions that observe DST are listed on Wikipedia.
Date and Time Syntax
RFC2822
ISO 8601
Differences Between RFC2822 and ISO 8601
1. Without an explicit timezone, RFC2822 is parsed as UTC, while ISO 8601 is treated as local time.
2. RFC2822 is more permissive, accepting formats like Mon, 06 Mar 2017 21:22:23 +0000 .
3. RFC2822 uses a four‑digit timezone offset (e.g., +0800 ), whereas ISO 8601 uses +08:00 .
Time Zones
UTC Time Zone
Appending a “Z” to a time (e.g., 09:30Z ) indicates UTC +0.
UTC Offset
Offsets are expressed as ±hh:mm , ±hhmm , or ±hh (e.g., Beijing is +08:00 ).
Time‑Zone Abbreviations
Abbreviations like EST, WST, CST are not part of ISO 8601 and are discouraged.
TZ Database
Identifiers such as Asia/Shanghai are used in many systems; see the Wikipedia list for details.
Timestamp
Unix Timestamp
The Unix timestamp counts seconds since 1970‑01‑01 00:00:00 UTC (ignoring leap seconds).
JavaScript Timestamp
JavaScript timestamps are milliseconds since the Unix epoch, i.e., 1000 × the Unix timestamp.
Common ways to obtain it include Date.now() (the fastest) and new Date().getTime() .
Date Object
The JavaScript Date object is part of the standard library, but its behavior can vary across host environments.
Note the difference between Date() (returns a date string, ignores arguments) and new Date() (creates a Date instance based on arguments).
Date() returns the current time as a string.
new Date() returns a Date object.
Parsing
new Date() without arguments creates an object for the current system time.
new Date(1524662090537) parses a JavaScript timestamp (milliseconds).
new Date('December 17, 1995 03:24:00') and new Date('1995-12-17T03:24:00') parse strings that must conform to RFC2822 or ISO 8601.
new Date(1995, 11, 17, 3, 24, 0) interprets the arguments as local time (no timezone information).
Out‑of‑range values are normalized (e.g., new Date(2018, 0, 32) becomes February 1).
Operations
Get / Set
Various get and set methods exist; for example, Date.prototype.getMonth() returns 0‑11.
Set methods automatically adjust overflow values (e.g., adding two days).
Comparison
Date instances can be compared directly or subtracted; avoid addition as it results in string concatenation.
Subtracting timestamps is the safest way to compare times, avoiding timezone issues.
Formatting
The Date prototype provides many toXXX methods for converting dates to strings or numbers; refer to the official documentation for details.
Localization
Date and Time
The browser’s reported timezone depends on the system settings; Date.prototype.getTimezoneOffset() returns the offset in minutes.
Language and Region
Locale‑sensitive methods like .toLocaleString() are affected by the user’s language and region settings, which may vary across browsers.
Business Practices
International Configuration
Common Time Zones for International Events
India: +05:30
Indonesia: +07:00
Russia: +03:00
Pakistan: +05:00
Bangladesh: +06:00
Vietnam: +07:00
Typical Date‑Time Expressions
India: May 6, 2018, 00:00:00 (IST)
Indonesia: May 30, 2018 23:59:59 (WIB)
External Display
For multi‑region sites, APIs should return timestamps; the frontend converts them to the appropriate local representation.
Internal Configuration
Internal schedules (e.g., cron jobs) can use Beijing time for clarity among domestic teams.
Events tied to local calendars (e.g., Ramadan new moon) should be configured using the local timezone to avoid hidden risks.
Technical Impact
Browser Time Validation
Since client‑side time can be manipulated, rely on server time via synchronized timestamps or periodic Ajax heartbeats.
Server Network Time Sync
Use NTP (Network Time Protocol) to keep server clocks aligned with UTC, especially on long‑running Linux machines.
MySQL Time
For high‑precision requirements, use datetime(6) ; otherwise, standard datetime suffices, and leverage MySQL’s built‑in time functions.
Reference Documents
Date – JavaScript | MDN: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date
JS原生Date类型方法的一些冷知识 | 我很好奇: http://chitanda.me/2015/08/21/the-trivia-of-js-date-function
UC Tech Team
We provide high-quality technical articles on client, server, algorithms, testing, data, front-end, and more, including both original and translated content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.