SoFunction
Updated on 2025-03-09

JS gets the related functions of time and the conversion between timestamp and time date

Conversion of timestamps and time dates is a common operation. Let’s introduce how to achieve mutual conversion between them through code examples.

Before studying this article, let me introduce the parameters of the Date() constructor in javascript:

Everyone must be familiar with Date objects. Using the Date() constructor to create a time object is the most basic operation, for example:

var theDate=new Date();
();

Use the above code to get the days of the current date.

The above is the simplest application of the Date() constructor. The Date object has multiple constructors. The following is a brief list:

new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)

The following is a simple analysis of the above constructors.

Date(), when there are no parameters, the current time and date object is created.

Date(milliseconds), when the parameter is a number, then this parameter is a timestamp, which is regarded as milliseconds, creating a time and date object with a specified milliseconds from January 1, 1970.

Date(datestring), this parameter is a string, and this string must be converted using ().

4. The following six constructors are precisely defined:

1).year is an integer. If it is 0-99, then add 1900 on this basis, and the others are returned as it is.
2).month, is an integer with a range of 0-11.

, is an integer with a range of 1-31.

, is an integer with a range of 0-23.

, is an integer with a range of 0-59.

, is an integer with a range of 0-59.

, is an integer with a range of 0-9999.

Code example:

var d1=new Date();
var d2=new Date(1320336000000);
var d3=new Date("2013-8-20 18:20:30");
var d4=new Date(2013,7,26);

1. Convert time and date to timestamp:

Now there is a time date: "2013/5/12 20:10:20", which is converted to timestamp form:

The code is as follows:

var dateStr="2013/5/12 20:10:20";
var date=new Date(dateStr);
(()); 

Use the getTime() function to get the timestamp of the specified time and date object.

Next, I will introduce to you the getTime() method of the Date object of javascript

Definition and usage of getTime() method:

This method returns the number of milliseconds between the current time and January 1, 1970.

Note: This method needs to be called using a Date object.

Click to see more related Date object methods and properties.

Syntax structure:

()

Example code:

var myDate=new Date()
(())

The above code can output the number of milliseconds between the current time and January 1, 1970.

In fact, there are many other methods, which I won’t introduce here. You just need to know the methods and you can accumulate others slowly by yourself.

2. Convert timestamps to event date:

Example Code 1:

var date=new Date(1368360620000);
(()); 

The definition and usage of the toLocaleString() method of the Date object of javascript:

This method converts the Date object to a string based on local time and returns this string.

Note: This method requires an instance call with the Date object.

Syntax structure:

()

Example code:

var d=new Date();
(());

Let's talk about the toLocaleString() method of the Date object of javascript

Definition and usage of toLocaleString() method:

This method converts the Date object to a string based on local time and returns this string.

Note: This method requires an instance call with the Date object.

Syntax structure:

()

Example code:

var d=new Date();
(());

Example 2:

function formatDate(now)
{ 
var year=(); 
var month=()+1; 
var date=(); 
var hour=(); 
var minute=(); 
var second=(); 
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
} 
var d=new Date(1368360620000); 
(formatDate(d));