• 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:

  • Event Calendar Code Generator
  • Check if a date is after another date
  • Simple Calendar
  • Check if a date is between two other dates

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us