gpt4 book ai didi

javascript - 本地存储返回无效的日期格式

转载 作者:行者123 更新时间:2023-11-29 16:11:09 26 4
gpt4 key购买 nike

 $localStorage.doctorDateTime.push({                            
fullDate : new Date(doctorDateTime)
});

我在新日期中传递了日期字符串,然后将其保存到本地存储,但是当我从本地存储中检索它时,它显示了这种格式:

2015-01-01T13:41:18.300Z

while if console.log(doctorDateTime)。它显示正确的日期字符串

最佳答案

localStorage 存储的数据仅为字符串。如果您尝试存储非字符串类型的内容,则会发生隐式类型强制转换。

但是,它看起来取决于您正在使用的某些 lib 实现,因为您得到的行为类似于 Date.prototype.toISOString() , 而下面的代码表现得像 Date.prototype.toString() :

localStorage.setItem("fullDate", new Date(doctorDateTime));

在设置为 localStorage 之前,您最好将 Date 对象显式转换为所需格式的字符串。

但是,您仍然可以使用 ISO time string 返回 Date 对象:

var str = '2015-01-01T13:41:18.300Z';
var time = new Date(str); // you got the object back!

关于javascript - 本地存储返回无效的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27731327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com