Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does JS calculate the time difference

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report