How to convert unix timestamp to calendar date moment.js
Categories:
Converting Unix Timestamps to Human-Readable Dates with Moment.js

Learn how to effortlessly transform Unix timestamps into formatted calendar dates using the Moment.js library in JavaScript.
Unix timestamps are a common way to represent points in time, often used in databases, APIs, and system logs. They are essentially a count of seconds (or milliseconds) that have elapsed since the Unix Epoch (January 1, 1970, 00:00:00 UTC). While efficient for computers, these raw numbers are not user-friendly. This article will guide you through converting these timestamps into easily understandable calendar dates using the popular JavaScript library, Moment.js.
Understanding Unix Timestamps
Before diving into conversion, it's crucial to understand the two main types of Unix timestamps you'll encounter:
- Seconds since Epoch: This is the traditional Unix timestamp, representing the number of seconds since January 1, 1970, UTC.
- Milliseconds since Epoch: Many JavaScript environments and APIs use milliseconds since Epoch, which is the seconds timestamp multiplied by 1000.
Moment.js can handle both, but it's important to know which one you're working with to ensure correct conversion. If your timestamp is a large number (e.g., 1678886400000), it's likely in milliseconds. If it's a smaller number (e.g., 1678886400), it's probably in seconds.
flowchart TD A[Unix Timestamp Input] --> B{Is it in milliseconds?} B -- Yes --> C[Use moment(timestamp)] B -- No --> D[Use moment.unix(timestamp)] C --> E[Format as desired] D --> E E --> F[Human-Readable Date Output]
Decision flow for converting Unix timestamps with Moment.js
Converting Timestamps with Moment.js
Moment.js provides straightforward methods for converting Unix timestamps. The primary methods are moment()
for millisecond timestamps and moment.unix()
for second timestamps. Once you have a Moment object, you can use its powerful format()
method to display the date in various human-readable formats.
// Example 1: Timestamp in milliseconds
const timestampMs = 1678886400000; // March 15, 2023 00:00:00 UTC
const dateFromMs = moment(timestampMs);
console.log('From milliseconds (default format):', dateFromMs.format());
console.log('From milliseconds (custom format):', dateFromMs.format('YYYY-MM-DD HH:mm:ss'));
// Example 2: Timestamp in seconds
const timestampSec = 1678886400; // March 15, 2023 00:00:00 UTC
const dateFromSec = moment.unix(timestampSec);
console.log('From seconds (default format):', dateFromSec.format());
console.log('From seconds (custom format):', dateFromSec.format('MMMM Do YYYY, h:mm:ss a'));
Converting Unix timestamps (milliseconds and seconds) to formatted dates.
npm install moment
and require('moment')
. For browser, include the Moment.js script tag.Formatting Options
The format()
method is incredibly versatile, allowing you to specify almost any date and time format. Here are some common format tokens:
YYYY
: 4-digit year (e.g., 2023)MM
: 2-digit month (01-12)DD
: 2-digit day of month (01-31)HH
: 2-digit hour (00-23)hh
: 2-digit hour (01-12)mm
: 2-digit minute (00-59)ss
: 2-digit second (00-59)a
: am/pmdddd
: Full day name (e.g., Wednesday)MMMM
: Full month name (e.g., March)Do
: Day of month with ordinal (e.g., 15th)
You can combine these tokens with literal strings to create custom output formats.
const now = moment(); // Current date and time
console.log('Full date and time:', now.format('dddd, MMMM Do YYYY, h:mm:ss a'));
console.log('Short date:', now.format('MM/DD/YYYY'));
console.log('Time only:', now.format('HH:mm'));
console.log('ISO 8601 format:', now.format()); // Default ISO 8601
console.log('Date with timezone:', now.format('YYYY-MM-DD HH:mm:ss Z'));
Examples of various date and time formatting options.
Practical Steps for Conversion
Here's a summary of the steps to convert a Unix timestamp to a calendar date using Moment.js:
1. Install Moment.js
If you haven't already, install Moment.js in your project using npm: npm install moment
or include it via a CDN in your HTML.
2. Import Moment.js
In your JavaScript file, import the library: const moment = require('moment');
(Node.js) or ensure it's globally available (browser).
3. Identify Timestamp Type
Determine if your Unix timestamp is in seconds or milliseconds. Millisecond timestamps are typically much larger numbers.
4. Create Moment Object
Use moment(timestamp)
for milliseconds or moment.unix(timestamp)
for seconds to create a Moment object.
5. Format the Date
Apply the format()
method to your Moment object with your desired format string, e.g., myMomentObject.format('YYYY-MM-DD HH:mm')
.