This article describes how to use JavaScript to converttimestamps to date format。
Mainly usedJavaScript Date object。
The Date object is used to handle dates and times.
Create a Date object:new Date()
The following four methods can also create Date objects:
var d = new Date(); var d = new Date(milliseconds); // milliseconds 为毫秒 var d = new Date(dateString); var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);Common methods of the Date object include:
date.getFullYear(); // Get the full year (4 digits, 1970)
date.getMonth(); // Get the month (0-11, 0 represents January, remember to add 1 when using it)
date.getDate(); // Get the day (1-31)
date.getTime(); // Get the time (milliseconds since January 1, 1970)
date.getHours(); // Get the hours (0-23)
date.getMinutes(); // Get the minutes (0-59)
date.getSeconds(); // Get the seconds (0-59)
date.getMonth(); // Get the month (0-11, 0 represents January, remember to add 1 when using it)
date.getDate(); // Get the day (1-31)
date.getTime(); // Get the time (milliseconds since January 1, 1970)
date.getHours(); // Get the hours (0-23)
date.getMinutes(); // Get the minutes (0-59)
date.getSeconds(); // Get the seconds (0-59)
In the following example, we convert the timestamp 1655185405 seconds toyyyy-MM-dd hh:mm:ssformat:
Example
var date = new Date(1655185405 * 1000); // The parameter requires milliseconds, so here multiply the seconds by 1000
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds();
document.write(Y+M+D+h+m+s);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds();
document.write(Y+M+D+h+m+s);
Try it Yourself »
We can also convert a date to a timestamp:
Example
var strtime = '2022-04-23 12:25:19';
var date = new Date(strtime);
// Get the timestamp in the following three ways
time1 = date.getTime();
time2 = date.valueOf();
time3 = Date.parse(date);
var date = new Date(strtime);
// Get the timestamp in the following three ways
time1 = date.getTime();
time2 = date.valueOf();
time3 = Date.parse(date);
Try it Yourself »
Use Date() to get the current system time, and use getFullYear(), getMonth(), getDate(), getHours(), getMinutes(), getSeconds() and other methods to generate a time in a specific format:
Example
var today = new Date();
// Date
var DD = String(today.getDate()).padStart(2, '0'); // Get the day
var MM = String(today.getMonth() + 1).padStart(2, '0'); // Get the month, January is 0
var yyyy = today.getFullYear(); // Get the year
// Time
hh = String(today.getHours()).padStart(2, '0'); // Get the current hours (0-23)
mm = String(today.getMinutes()).padStart(2, '0'); // Get the current minutes (0-59)
ss = String(today.getSeconds()).padStart(2, '0'); // Get the current seconds (0-59)
today = yyyy + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
document.write(today);
// Date
var DD = String(today.getDate()).padStart(2, '0'); // Get the day
var MM = String(today.getMonth() + 1).padStart(2, '0'); // Get the month, January is 0
var yyyy = today.getFullYear(); // Get the year
// Time
hh = String(today.getHours()).padStart(2, '0'); // Get the current hours (0-23)
mm = String(today.getMinutes()).padStart(2, '0'); // Get the current minutes (0-59)
ss = String(today.getSeconds()).padStart(2, '0'); // Get the current seconds (0-59)
today = yyyy + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
document.write(today);
Try it Yourself »