Using Node.js, 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?
Using Node.js, 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?
If you're using Node.js, you're sure to have EcmaScript 5, and so Date has a toISOString
method. You're asking for a slight modification of ISO8601:
new Date().toISOString() > '2012-11-04T14:51:06.157Z'
So just cut a few things out, and you're set:
new Date().toISOString(). replace(/T/, ' '). // replace T with a space replace(/\..+/, '') // delete the dot and everything after > '2012-11-04 14:55:45'
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).
UPDATE 2017-03-29: Added date-fns, some notes on Moment and Datejs
UPDATE 2016-09-14: Added SugarJS which seems to have some excellent date/time functions.
OK, since no one has actually provided an actual answer, here is mine.
A library is certainly the best bet for handling dates and times in a standard way. There are lots of edge cases in date/time calculations so it is useful to be able to hand-off the development to a library.
Here is a list of the main Node compatible time formatting libraries:
There are also non-Node libraries:
There's a library for conversion:
npm install dateformat
Then write your requirement:
var dateFormat = require('dateformat');
Then bind the value:
var day=dateFormat(result.request_date, "yyyy-mm-dd h:MM:ss");
see dateformat
I have nothing against libraries in general. In this case a general purpose library seems overkill, unless other parts of the application process dates heavily.
Writing small utility functions such as this is also a useful exercise for both beginning and accomplished programmers alike and can be a learning experience for the novices amongst us.
function dateFormat (date, fstr, utc) { utc = utc ? 'getUTC' : 'get'; return fstr.replace (/%[YmdHMS]/g, function (m) { switch (m) { case '%Y': return date[utc + 'FullYear'] (); // no leading zeros required case '%m': m = 1 + date[utc + 'Month'] (); break; case '%d': m = date[utc + 'Date'] (); break; case '%H': m = date[utc + 'Hours'] (); break; case '%M': m = date[utc + 'Minutes'] (); break; case '%S': m = date[utc + 'Seconds'] (); break; default: return m.slice (1); // unknown code, remove % } // add leading zero if required return ('0' + m).slice (-2); }); } /* dateFormat (new Date (), "%Y-%m-%d %H:%M:%S", true) returns "2012-05-18 05:37:21" */
The javascript library sugar.js (http://sugarjs.com/) has functions to format dates
Example:
Date.create().format('{dd}/{MM}/{yyyy} {hh}:{mm}:{ss}.{fff}')
Easily readable and customisable way to get a timestamp in your desired format, without use of any library:
function timestamp(){ function pad(n) {return n<10 ? "0"+n : n