Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

Date string conversion to unix timestamp in JavaScript / jQuery

I have date string in this format:

2009-07-15 00:00:00 - 2009-07-15 08:16:23

Could anyone help me to tweak the date string to unix timestamp into something like this:

1247634000 - 1247663783

I am using GMT-5 Timezone.

What JavaScript or jQuery techniques could be used?

question from:https://stackoverflow.com/questions/11509814/date-string-conversion-to-unix-timestamp-in-javascript-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var input = "2009-07-15 00:00:00 - 2009-07-15 08:16:23";
input = input.split(" - ").map(function (date){
    return Date.parse(date+"-0500")/1000;
}).join(" - ");

Demo

Date.parse docs

Note: This won't work on old browsers since I'm using Array.map, but I think you should easily be able to shim it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...