Getting Current Time in JavaScript
JavaScript provides multiple ways to retrieve the current time, each designed for different use cases. From simple epoch timestamps to high-resolution performance measurements and the upcoming modern Temporal API, understanding these options is essential for building performant web applications.
Key methods covered:
Date.now()- Standard timestamp since epochperformance.now()- High-resolution monotonic timingTemporal.Now- Future-proof date/time handling
Date.now() - The Standard Timestamp Method
The Date.now() static method returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC (the Unix epoch). This method has been a JavaScript standard since ECMAScript 1.
Key characteristics:
- Returns numeric value representing milliseconds since epoch
- Cannot be instantiated - static method on Date object
- Useful for generating unique IDs and measuring elapsed time
- Affected by system clock adjustments (unlike performance.now())
- Privacy features in some browsers reduce precision to 2ms or 100ms
Browser support: Widely available since July 2015
1// Get current timestamp2const timestamp = Date.now();3console.log(timestamp);4 5// Calculate elapsed time6const start = Date.now();7doSomething();8const elapsed = Date.now() - start;9console.log(`Execution took ${elapsed} ms`);10 11// Create timestamp object12function createRecord() {13 return {14 id: Date.now(),15 createdAt: new Date(Date.now()).toISOString()16 };17}1// Performance timing with high precision2const t0 = performance.now();3doExpensiveOperation();4const t1 = performance.now();5console.log(`Operation took ${t1 - t0} milliseconds`);6 7// Tracking page load performance8const pageStart = performance.now();9window.addEventListener('load', () => {10 const loadTime = performance.now() - pageStart;11 console.log(`Page loaded in ${loadTime.toFixed(2)} ms`);12});performance.now() - High-Resolution Performance Timing
The performance.now() method returns a high-resolution timestamp in milliseconds, representing the time elapsed since Performance.timeOrigin. Unlike Date.now(), this method uses a monotonic clock.
Key advantages:
- Sub-millisecond precision - Up to microsecond accuracy
- Monotonic clock - Always increases, immune to NTP adjustments
- Relative timing - Based on page load or browsing context creation
- Security coarsening: 5μs for cross-origin isolated, 100μs otherwise
- Available in Web Workers since 2015
- Supported in all major browsers since September 2015
Security note: Precision is reduced based on cross-origin isolation status to prevent timing attacks and fingerprinting. For comprehensive application performance optimization, explore our web development services.
| Feature | Date.now() | performance.now() |
|---|---|---|
| Precision | 1 millisecond | Sub-millisecond (microseconds) |
| Clock Type | Wall-clock (system time) | Monotonic (never decreases) |
| Clock Adjustments | Affected by NTP/system changes | Immune to adjustments |
| Use Case | Timestamps, IDs, elapsed days | Performance measurement, benchmarks |
| Browser Support | Since July 2015 | Since September 2015 |
| Web Workers | Supported | Supported |
Temporal API - The Future of JavaScript Date/Time
Temporal is a new JavaScript API designed to replace the Date object with a modern, full-featured solution. Currently at Stage 3 in the TC39 process, it offers over 200 utility methods.
Benefits over legacy Date:
- Built-in time zone support - No more UTC/local confusion
- Multiple calendar systems - Gregorian, Hebrew, Chinese, and more
- Immutable operations - All methods return new instances
- Plain types - Separate objects for date, time, date+time, and more
- Intuitive API - Designed to prevent common mistakes
Temporal.Now provides multiple ways to get the current time:
Temporal.Now.instant()- Unix epoch millisecondsTemporal.Now.plainDateTimeISO()- Date and time without time zoneTemporal.Now.plainDateISO()- Date onlyTemporal.Now.plainTimeISO()- Time only
Note: Temporal requires a polyfill in most current browsers. This modern approach to JavaScript development represents the future of date/time handling.
1// Using Temporal (requires polyfill)2const now = Temporal.Now.plainDateTimeISO();3console.log(now.toString());4 5// Get current instant6const instant = Temporal.Now.instant();7console.log(instant.toMillis());8 9// Get plain date10const date = Temporal.Now.plainDateISO();11console.log(date.toString());12 13// Get plain time14const time = Temporal.Now.plainTimeISO();15console.log(time.toString());Choosing the Right Method
When should I use Date.now()?
Use Date.now() for generating unique IDs, creating timestamps for database records, or when you need wall-clock time. It's the simplest choice with universal browser support.
When should I use performance.now()?
Use performance.now() for measuring code execution time, tracking performance metrics, benchmarking, and any scenario where monotonic timing accuracy matters.
Should I use Temporal API now?
For production applications, use a polyfill like @js-temporal/polyfill. Temporal is ideal for new projects as it provides a cleaner API for complex date/time operations.
What's the difference between monotonic and wall-clock time?
Wall-clock time reflects the system's current time and can jump forward or backward (NTP adjustments, DST). Monotonic time only increases, making it reliable for measuring elapsed durations.