用户输入日期时,存储在MongoDB中时,如何将其存储在原始状态?

发布于 2025-02-02 03:25:38 字数 1254 浏览 3 评论 0原文

我正在餐厅网站上进行练习项目,该网站可以选择保留餐桌。由于MongoDB的UTC省时系统,用户以用户形式输入的日期时间更改。在印度,时间是UTC+5:30,因此相应地改变了。如何修改用户输入的时间,以便以其原始状态保存?我是MongoDB和Mongoose的新手,因此我对可以使用的技术并不精通。

例如:假设用户将日期时间输入 26-5-22 8:27 PM

在MongoDB中,它保存为 iso(2022-05-26T14:57:00Z)

我的MongoDB的JS代码如下:

const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main(){
    await mongoose.connect('mongodb://localhost:27017/HamdansEatery');
}
const reservationSchema= new mongoose.Schema({
    fname: String,
    lname: String,
    tel: Number,
    email: String,
    no_of_persons: Number,
    date_time: Date
})

const Reservation = mongoose.model("Reservations",reservationSchema);
//----------------------------------------->
app.post('/reserve_a_table', (req,res) => {
    let yourData = new Reservation(req.body);
    yourData.save().then(()=>{
        res.send("Data has been saved successfully!");
    }).catch(() => {
        res.status(400).send("Data not saved due to some issues.");
    })
})

前端是 html (输入类型= DateTime-local)

我看到了一些相关问题,但在用户输入的日期和时间找不到一个问题。 我应该如何进步?如何修改日期示意图以使其在保存MongoDB时可以在某种程度上添加5:30小时,以便我可以在印度标准时间获得IE的结果?

I am making a practice project on a restaurant website that has an option of table reservation. The date-time entered in the form by the user changes when stored in MongoDB due to the UTC time-saving system of MongoDB. In India, the time is UTC+5:30 so it changes accordingly. How do I modify the time entered by the user so that it can be saved in its original state? I am new to MongoDB and mongoose so I am not well versed with the techniques which can be used.

Eg: Suppose the user entered the date-time as 26-5-22 8:27 pm

In mongodb, it saves as ISO(2022-05-26T14:57:00Z)

MY js code for MongoDB is below:

const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main(){
    await mongoose.connect('mongodb://localhost:27017/HamdansEatery');
}
const reservationSchema= new mongoose.Schema({
    fname: String,
    lname: String,
    tel: Number,
    email: String,
    no_of_persons: Number,
    date_time: Date
})

const Reservation = mongoose.model("Reservations",reservationSchema);
//----------------------------------------->
app.post('/reserve_a_table', (req,res) => {
    let yourData = new Reservation(req.body);
    yourData.save().then(()=>{
        res.send("Data has been saved successfully!");
    }).catch(() => {
        res.status(400).send("Data not saved due to some issues.");
    })
})

The frontend is HTML form with (input type = datetime-local)

I saw some related questions but couldn't find one for a user-entered date and time.
How should I progress? How can I modify the Date SchemaType such that it can in a way add 5:30hrs while saving in MongoDB so I can get the result I want i.e. time in Indian Standard Time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文