14

I need to convert a date in format eg. 2011-01-02 to a unix timestamp in JavaScript but do not exactly know how to accomplish it. I can get the current timestamp using the following functions Math.round((new Date()).getTime() / 1000); but do not know how to convert the given date to a unix timestamp?

1 Answer 1

34

You can write new Date("2011-01-02").getTime() / 1000

Sign up to request clarification or add additional context in comments.

3 Comments

Noting that "2011-01-02" will be parsed as UTC, not local as may be expected by those familiar with ISO 8601 but not ECMA-262. ;-)
Im here because I can't find a way to get YYYY-MM-DD into a locally correct timestamp....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.