SoFunction
Updated on 2025-03-03

A summary of 5 methods for getting the current timestamp by JavaScript

5 ways to get the current timestamp

1. Get the timestamp accurate to seconds, 13 digits

const timestamp = (new Date());
(timestamp);
//Output 1591669256000   13Bit

2. Get the timestamp accurate to milliseconds, 13 digits

const timestamp = (new Date());
(timestamp);
//Output 1591669961203   13Bit

3. Get the timestamp accurate to milliseconds, 13 bits

const timestamp = (new Date()).valueOf();
(timestamp);
//Output 1591670037603   13Bit

4. Get the timestamp accurate to milliseconds, 13 digits

const timestamp = new Date().getTime();
(timestamp);
//Output 1591670068833   13Bit

5. Get the timestamp accurate to milliseconds, 13 digits

const timestamp = +new Date();
(timestamp);
//Output 1591670099066   13Bit

js method to get the timestamps of today, this week, this month, this year

1. Today's time

// Time stamp of the start time todaynew Date(new Date().toLocaleDateString()).getTime()
// Time stamp of the end time of todaynew Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1

2. Time of this week

// Time stamp of the start time of the weeknew Date(new Date().setHours(0, 0, 0) - (new Date().getDay() - 1) *24 * 60 * 60 *1000)
// Time stamp of the end time of the weeknew Date(new Date().setHours(0, 0, 0) + (7 - new Date().getDay()) *24 * 60 * 60 *1000)

3. This month

// Time stamp of the start time of the monthnew Date(new Date(new Date().getFullYear(), new Date().getMonth(), 1).setHours(0, 0, 0))
// Time stamp of the end time of the monthnew Date(new Date(new Date().getFullYear(), new Date().getMonth()+ 1, 0).setHours(23, 59, 59, 59))

4. This year

// Time stamp of the start time of the yearnew Date(new Date().getFullYear(), 0, 1)
// Time stamp of the end time of the yearnew Date(new Date(new Date().getFullYear() + 1, 0, 0).setHours(23, 59, 59, 59))

Summarize

This is the end of this article about 5 methods for getting current timestamps in JavaScript. For more related JS content to get current timestamps, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!