• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Time & Date / How To Get a Timestamp in JavaScript

How To Get a Timestamp in JavaScript

How To Get a Timestamp in JavaScript

JavaScript provides several ways to get the current timestamp, which is the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC. Here are a few examples of how to get the timestamp in JavaScript:

Using the Date.now() method

const timestamp = Date.now();
console.log(timestamp); // 1613231960828 (example output)

Using the Date() constructor

const timestamp = new Date().getTime();
console.log(timestamp); // 1613231960828 (example output)

Using the performance.now() method

const timestamp = performance.now();
console.log(timestamp); // 1613231960828 (example output)

The first two methods use the built-in Date object to get the current timestamp. The Date.now() method returns the current timestamp, while the Date() constructor returns a new Date object, and the getTime() method returns the timestamp of the object.

The performance.now() method returns the timestamp with a higher precision than Date.now() and new Date().getTime() and it is more accurate on the browser.

You can also get the timestamp in a specific format using the toString() method or a library like Moment.js

Note that the timestamp is the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC. To convert it to a human-readable format, you can use the toString() method, or a library like Moment.js

const date = new Date(timestamp);
console.log(date.toString()); // "Mon May 24 2021 11:20:05 GMT-0700 (Pacific Daylight Time)" (example output)

Time & Date

Related Snippets:

  • Check if a date is after another date
  • Alarm Clock
  • Check if the given year is a leap year
  • Get the amount of time from now for a date

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers