【问题标题】:Setting and retrieving DateTime using Datetime-local HTML type使用 Datetime-local HTML 类型设置和检索 DateTime
【发布时间】:2021-12-11 15:07:40
【问题描述】:

在客户端不使用momentjs。我需要将日期/时间正确保存到数据库并将其拉回,然后将其转换为本地。

我注意到 material-ui datepicker 之前处理过这个问题,但现在我需要自己做。

html 输入 'datetime-local' 不喜欢接收时区,所以我不确定如何完成整个流程。

我想我需要像这样在客户端为数据库获取 UTC 日期(?) 2021-10-26T01:14:27.920+00:00

然后以某种方式将其转换为输入 'datetime-local' 将接受的内容。

任何帮助表示赞赏

【问题讨论】:

    标签: node.js datetime ecmascript-6 momentjs datetime-format


    【解决方案1】:

    您可以通过以下方式实现

    // Create date object from datetime string
    var date = new Date('2021-10-26T01:14:27.920+00:00');
    
    // Coverting local datetime back to UTC
    console.log(date.toUTCString()); // Output "Tue, 26 Oct 2021 01:14:27 GMT"
    
    // Coverting to local datetime 
    console.log(date.toString());  // Output "Tue Oct 26 2021 06:44:27 GMT+0530 (India Standard Time)"
    
    // Coverting to local timezone 
    console.log(date.toLocaleString()); // Output "10/26/2021, 6:44:27 AM"
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-02
    • 2014-09-20
    • 2017-04-12
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多