Get the App
SLTechnology News&Howtos  ›  Database  › 

The solution to the problem of time difference in MongoDB Storage

Shulou Source: shulou.com Published: 2022-06-01 03:20:26 09月29日 Update

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.

Tags: Time Green Greenwich time Zone time difference Storage function hour Standard Unit time conversion content region data format concept Type Beijing Zone 8 Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker Xiaomi NVidia vpn