How to get a Timestamp in JavaScript

3 minutes read

 Timestamp JavaScript

Method getTime() returns the total number of milliseconds between midnight of January 1, 1970 and the date specified by you. The often requirement to calculate with unix timestamp.


 
Syntax:
Date.getTime()
 
Date Objects Creation
The Date object allows programmer work smoothly with dates. Constructor new Date() is used to create date objects. The date can be initiated in 4 different ways.

1. new Date()– It will create a new date object with respect to the current date and time.
2. new Date(date string)- For creation of new date object from the specified date and time
3. new Date(number)– This will create a new date object as zero time plus the number.
4. new Date(7 numbers)-To create a new date object with respect to the specified date and time.
 
These 7 numbers specify the year, month, day, hour, minute, second, and
millisecond
 
There are multiple ways to fetch the timestamp. For current unix timestamp easiest and fastest way is

const dateTime = Date.now();
const timestamp = Math.floor(dateTime / 1000);

or

const dateTime = new Date().getTime();
const timestamp = Math.floor(dateTime / 1000);
 
Pass YYYY-MM-DD or YYYY-MM-DDT00:00:00Z as parameter of Date constructor in order to get unix timestamp of a specific date. For example.
const dateTime = new Date(‘2012-06-08’).getTime();
const timestamp = Math.floor(dateTime / 1000);
 
Simply add a + sign while declaring a Date object like below
const dateTime = +new Date();
const timestamp = Math.floor(dateTime / 1000);
 
For specific date
const dateTime = +new Date(‘2012-06-08’);
const timestamp = Math.floor(dateTime / 1000);

Date.now() is compatible with all major browser except IE8 and earlier.
 
For timestamp in milliseconds
if (!Date.now) {
Date.now = function() { return new Date().getTime(); }
}
 
For timestamp in seconds
Math.floor(Date.now() / 1000)
 
A faster alternative
Date.now() / 1000 | 0
 
Date.now() method apart from being short also, does not create a new Date object. If you want to experience maximum compatibility try older method to get timestamp in milliseconds:
new Date().getTime()
 
This can be then convert to seconds as below:
Math.round(new Date().getTime()/1000)

 

Looking forward to responding to your queries and comments regarding JavaScript TimeStamp. If any information you want to get included I will update this article with it.
 
About Singsys Pte. Ltd. Singsys is a solution provider that offer user friendly solution on cutting edge technologies to engage customers and boost your brand online results from a set of certified developers, designers who prefer optimized utilization of the available resources to align client’s idea with their skillset to reflect it into a Mobile applicationWeb application or an E-commerce solution
 
You may be interested in following:

  1. How to Append Anything into JavaScript Array
  2. Remove Specific Element from JavaScript Array
  3. HTML CSS and JavaScript tools and libraries for Productivity Boost
  4. Random Whole Number Generation in Javascript Within a Range
  5. 5 Best JavaScript Testing Tools for Web Developers

Related Posts...

Javascript