Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date from toUTCString()

I'm creating random time 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?

like image 947
Marek Sebera Avatar asked Oct 28 '25 20:10

Marek Sebera


1 Answers

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));
like image 107
Alex Pacurar Avatar answered Oct 30 '25 09:10

Alex Pacurar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!