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

The solution to the problem of time difference in MongoDB Storage

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

Share

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

Preface

When MongoDB stores time type data, it is first converted to UTC time and then stored in the database. When we take out the stored time, there will be the problem of jet lag.

For example, when we use Beijing time, we will see that the value we read is 8 hours less than the current time. Is it troublesome to say that we have to deal with the time separately each time we read it? In fact, we can handle it when we store it, simply by using the getTimezoneOffset () and toISOString () functions.

Concepts to understand:

Greenwich time

The local time on the Greenwich meridian, or the zone time in the zero time zone (middle time zone) is called Greenwich time, also known as Universal time. No more detailed concepts, we don't need it here. (for example, China is East eighth District, and Beijing time is (GMT+08:00)

Get the time difference between local and Greenwich mean time: new Date (). GetTimezoneOffset (), in minutes.

Conversion of correct local time to known Greenwich mean time

Local time = Greenwich mean time-time difference

Given local time, the conversion corresponds to Greenwich mean time:

Greenwich time = local time + time difference

Given local time, convert time to other time zones

Because the difference between time intervals is in hours. Therefore, after calculating the time of 0 time zone, subtract or add the corresponding hours (East N area + N hours, West N area-N hours). In order to facilitate the calculation, the East N area is recorded as a positive number and the West N area as a negative number, that is:

Target time zone time = local time + time difference + time zone interval

GetTimezoneOffset function: returns the time difference for this area (the regional time difference between local time and GMT Greenwich mean time) in minutes.

/ / We are var d = new Date (); var tz = d.getTimezoneOffset (); console.log (tz); / /-480

ToISOString () function: converts a Date object to a string using the ISO standard.

The standard is called ISO-8601 and the format is: YYYY-MM-DDTHH:mm:ss.sssZ.

Grammar

Date.toISOString ()

Return value

Type description time and date in StringISO standard format

Encapsulated time conversion function

LocalDate (v) {const d = new Date (v | | Date.now ()); d.setMinutes (d.getMinutes ()-d.getTimezoneOffset ()); return d.toISOString ();}

We can just call localDate () when we store the time. No matter which time zone you are in, the result will be the same as the local time.

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

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

Database

Wechat

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

12
Report