date
milliseconds
programming
time
coding

Get current date in milliseconds

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Getting the current date in milliseconds usually means getting the current Unix timestamp in milliseconds since January 1, 1970 UTC. That value is useful for logs, cache keys, ordering events, and API payloads, but it is important to remember that wall-clock timestamps and elapsed-time measurements are not always the same problem.

Understand What the Millisecond Value Means

Most languages expose the current system time as a count relative to the Unix epoch. For example, if you ask for the current date in milliseconds, you usually get a large integer such as 1727300000123.

That value means:

  • it is based on wall-clock time,
  • it is usually measured in UTC relative to the epoch,
  • it can change if the system clock is adjusted.

This is excellent for timestamps, but not always ideal for measuring precise durations.

JavaScript Example

In JavaScript, use Date.now():

javascript
const ts = Date.now();
console.log(ts);

You can also get the same value from a Date object:

javascript
const ts = new Date().getTime();
console.log(ts);

Date.now() is shorter and clearer when you only need the timestamp.

Python Example

In Python, you can use the time module and convert seconds to milliseconds:

python
1import time
2
3ts_ms = int(time.time() * 1000)
4print(ts_ms)

If you are already using datetime, you can still produce a millisecond timestamp:

python
1from datetime import datetime, timezone
2
3ts_ms = int(datetime.now(timezone.utc).timestamp() * 1000)
4print(ts_ms)

The timezone.utc version is clearer when you want an explicitly UTC-aware datetime object before converting to a timestamp.

Java Example

In modern Java, use Instant.now():

java
1import java.time.Instant;
2
3public class Main {
4    public static void main(String[] args) {
5        long ts = Instant.now().toEpochMilli();
6        System.out.println(ts);
7    }
8}

Older Java code often uses System.currentTimeMillis(), which is still valid for many use cases:

java
long ts = System.currentTimeMillis();
System.out.println(ts);

Most other languages expose an equivalent wall-clock API as well, even if the exact name differs. The real question is usually not whether the language supports it, but whether milliseconds are the right representation for the job.

Timestamps Versus Duration Measurement

This distinction matters. If you want to record when something happened, epoch milliseconds are a good fit. If you want to measure how long something took, a monotonic timer is usually better because wall-clock time can jump due to clock synchronization or manual changes.

For example, in Python:

python
1import time
2
3start = time.perf_counter()
4# do work
5end = time.perf_counter()
6print(end - start)

That is better for duration measurement than subtracting two wall-clock timestamps.

For many systems, storing timestamps as integers also makes serialization easier across APIs and databases, especially when multiple services are written in different languages.

That consistency is one reason epoch milliseconds remain such a common exchange format, even though human-facing code should still convert them back into readable dates when displayed.

Common Pitfalls

  • Assuming the millisecond value is local time rather than an epoch-based timestamp.
  • Using wall-clock timestamps to measure precise elapsed duration.
  • Forgetting to convert seconds to milliseconds in languages where the base API returns seconds.
  • Storing timestamps in a type that is too small for modern millisecond values.
  • Mixing UTC-based timestamps with local-time display logic and thinking the raw value itself is timezone-specific.

Summary

  • "Current date in milliseconds" usually means Unix epoch time in milliseconds.
  • Use Date.now() in JavaScript, time.time() * 1000 in Python, and Instant.now().toEpochMilli() in Java.
  • These timestamps are great for logs, ordering, and persistence.
  • Use monotonic timers instead of wall-clock timestamps for duration measurement.
  • Keep UTC storage and local-time display as separate concerns.

Course illustration
Course illustration

All Rights Reserved.