开发者

date from toUTCString()

开发者 https://www.devze.com 2023-04-03 11:26 出处:网络
I\'m creating random t开发者_如何转开发ime in random date in next month like this var time = new Date(now.getFullYear(),

I'm creating random t开发者_如何转开发ime in random date in next month like this

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)
                    );

I want to save this Date as String and after convert it back to Date

I use

var time_for_save = time.toUTCString();

which gives me string like this:

Sat, 01 Oct 2011 07:42:38 GMT

How can I convert this date back to Date object?

Or is there better way to save/retrieve Date object via string?


Given a date string representation, you can use Date.parse function to get a 'the number of milliseconds between the date string and midnight of January 1, 1970.'; After that you can use the date constructor to get a new date object from the 'epoch milliseconds'.

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


The Date constructor accepts a string:

var restoredDate = new Date(time_for_save);
0

精彩评论

暂无评论...
验证码 换一张
取 消