Get the App
SLTechnology News&Howtos  ›  Development  › 

How does JS calculate the time difference

Shulou Source: shulou.com Published: 2022-06-02 15:49:55 09月18日 Update

This article mainly explains "how to calculate the time difference in JS". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to calculate the time difference in JS".

Usually, js calculates the time difference by obtaining two time data, and then using the getTime () method to return the time difference from the fixed January 1, 1970. Through the difference in milliseconds returned, it is converted into time units to get the time difference between the two times.

Js calculates the time difference principle:

GetTime () method

Method definition: the getTime () method returns the number of milliseconds between January 1, 1970.

Start the operation:

First you will have a string of initial time data, and then convert your data into a Date object through newDate (your time data).

Vart1= "2017Universe 08Compact 2804VO4VUR 56"; / / data

VardateBegin=newDate (T1); / / converted to the form of a Date object

/ / MonAug28201704:56:38GMT+0800 (China Standard time) here is the data form of the Date object

Time format

Here, we should pay attention to the problem of the time data format given by the backend, such as the following two:

The first one: "2017Accord 08According to 2804 56Rich 38" / / this format no longer needs to be processed.

The second kind: "2017-08-0118VRV 56RV 38" / / this format needs to be processed.

Because the newDate () method cannot handle the second data, we need to convert the second data format to the first data format.

Vart1= "2017-05-1200 1315"

VardateBegin=newDate (d1.replace (/-/ g, "/")); / / replace method converts-to /

I don't know what's going on, anyway, because the data we gave me at the back end is the second kind, so I'll mention this thing (cover my face).

Another time data:

Since it is a time difference, there must be two data, otherwise how to compare the two, generally there will be a current time data in the two data.

Js calculates two time difference codes

VardateEnd=newDate (); / / current time data

Code that fully calculates the time difference (days, hours, minutes, seconds):

First get the millisecond difference between the milliseconds, through the millisecond difference to convert the units of time you need, and then the conversion between the units of time is based on their multiple relationship.

Thank you for your reading, the above is the content of "how to calculate the time difference in JS". After the study of this article, I believe you have a deeper understanding of how to calculate the time difference in JS, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Time data time difference method format two between unit object that is form processing learning conversion code content situation problem no thing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft MariaDB NVidia Shulou Tech Info