Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  8] [ 6]  / answers: 1 / hits: 38753  / 12 Years ago, sat, june 23, 2012, 12:00:00

Using javascript, How can I convert a human time string like Wed Jun 20 19:20:44 +0000 2012 into a timestamp value like 1338821992?


More From » date

 Answers
8

Just create a Date object from it and do .getTime() or use Date.parse():



var d = new Date(Wed Jun 20 19:20:44 +0000 2012);
d.getTime(); //returns 1340220044000

//OR

Date.parse(Wed Jun 20 19:20:44 +0000 2012); //returns 1340220044000


Works great if your human time string is in a format that the Date constructor understands (which the example you posted is).






EDIT



Realized you may mean a Unix timestamp, which is seconds passed since the epoch (not ms like JS timestamps). In that case simply divide the JS timestamp by 1000:



//if you want to truncate ms instead of rounding just use Math.floor()
Math.round(Date.parse(Wed Jun 20 19:20:44 +0000 2012) / 1000); //returns 1340220044

[#84708] Thursday, June 21, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckinleyi

Total Points: 121
Total Questions: 100
Total Answers: 109

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
;