0

*在不了解 mongoose 和 mongoDb 的文档后,我在这里

我知道这是一个简单的问题。

我需要使用以下格式将日期发送到 mongoDb:“10/15/2020 16:00”

所以,当我将它发送到数据库时,它保存了“2022-11-10T14:00:00.000Z”

我没有将其保存在 iso 中的问题,但是->小时更改了 2 小时

在正确的时间里,我能做些什么来节省分贝?我知道这与 UTC 或其他东西有关,但我不知道该怎么做。

  • 我使用moment库进行格式,该项目是react-native,mongoDb,mongoose,
4

1 回答 1

0

您必须使用带有 moment 和 moment-timezone 的语言环境来将 mongodb 时间转换为您的语言环境时间。

这是一个将 mongodb 转换为 Europe/Paris 时间的示例:

import moment from 'moment'
import momentTZ from "moment-timezone";
import "moment/locale/fr-ca";

const getDateByTimeZone = (data: string): string => {
    const rightTime: string = momentTZ.tz(timeToUpdate, "Europe/Paris").format(); //Convert the date returned by mongoDB to the local time of Paris
    return rightTime
}

export default function useMoment(): (date: string) => string {
    return (date) => moment(getDateByTimeZone(date)).format("L")
}
于 2020-11-17T22:24:55.003 回答