Using NodeJS, I want to format a Date into the following string format:
var ts_hms = new Date(UTC);
ts_hms.format("%Y-%m-%d %H:%M:%S");
How do I do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re using Node.js, you’re sure to have EcmaScript 5, and so Date has a
toISOStringmethod. You’re asking for a slight modification of ISO8601:So just cut a few things out, and you’re set:
Or, in one line:
new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')ISO8601 is necessarily UTC (also indicated by the trailing Z on the first result), so you get UTC by default (always a good thing).