• 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 / Unix timestamp from date

Unix timestamp from date

Unix timestamp from date

Gets the Unix timestamp from a Date object.

Use Date.prototype.getTime() to get the timestamp in milliseconds and divide by 1000 to get the timestamp in seconds.

Use Math.floor() to appropriately round the resulting timestamp to an integer.

Omit the argument, date, to use the current date.

const getTimestamp = (date = new Date()) => Math.floor(date.getTime() / 1000);

// EXAMPLE
getTimestamp(); // 1602162242

Source

https://www.30secondsofcode.org/js/s/get-timestamp

Time & Date

Related Snippets:

  • Check if the given year is a leap year
  • Alarm Clock
  • Simple Calendar
  • Date difference in months

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