gpt4 book ai didi

javascript - 从 toUTCString() 开始的日期

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

我在下个月的随机日期创建随机时间,这样

var time = new Date(now.getFullYear(),
now.getMonth() + 1,
Math.floor(Math.random()*28),
Math.floor(Math.random()*25),
Math.floor(Math.random()*60),
Math.floor(Math.random()*60),
Math.floor(Math.random()*1000)
);

我想将此日期保存为字符串,然后将其转换回日期
我用

var time_for_save = time.toUTCString();

这给了我这样的字符串:

Sat, 01 Oct 2011 07:42:38 GMT

如何将这个日期转换回 Date 对象?
或者是否有更好的方法通过字符串保存/检索日期对象?

最佳答案

给定日期字符串表示,您可以使用 Date.parse函数获取“日期字符串与 1970 年 1 月 1 日午夜之间的毫秒数”。之后,您可以使用日期构造函数从“纪元毫秒”获取新的日期对象。

var date = new Date(Date.parse(time_for_save));

关于javascript - 从 toUTCString() 开始的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7318288/

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