首页 > 解决方案 > 数据格式错误

问题描述

我正在尝试将数据从我的后端插入到 MongoDb。例如,如果我想插入“2021-06-03”,我将得到“2021-06-02T21:00:00.000+00:00”。模型 :

const organizerSchema = new mongoose.Schema({
description: { type: String, required: true },
duration: { type: Number },
location: { type: String },
date: { type: Date, default: Date.now },

和路线:

const { description } = req.body;
const { date } = req.body;
const { location } = req.body;
const { duration } = req.body;


const newAdd = new Add({
  description,
  date,
  location,
  duration, 
 });

标签: javascriptnode.jsmongodb

解决方案


为什么不尝试使用 moment( moment().unix() )将其保存为 unix?这是一个数字,您应该能够轻松地将其转换回日期。

否则使用时刻将其转换为正确的时区

时刻(日期).utcOffset('+0300').format(格式)


推荐阅读