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).

Answer from chbrown on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Date
Date - JavaScript | MDN - Mozilla
Note: You are encouraged to make sure your input conforms to the date time string format above for maximum compatibility, because support for other formats is not guaranteed. However, there are some formats that are supported in all major implementations โ€” like RFC 2822 format โ€” in which case their usage can be acceptable.
Top answer
1 of 16
731

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).

2 of 16
135

UPDATE 2021-10-06: Added Day.js and remove spurious edit by @ashleedawg
UPDATE 2021-04-07: Luxon added by @Tampa.
UPDATE 2021-02-28: It should now be noted that Moment.js is no longer being actively developed. It won't disappear in a hurry because it is embedded in so many other things. The website has some recommendations for alternatives and an explanation of why.
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:

  • Day.js [added 2021-10-06] "Fast 2kB alternative to Moment.js with the same modern API"
  • Luxon [added 2017-03-29, thanks to Tampa] "A powerful, modern, and friendly wrapper for JavaScript dates and times." - MomentJS rebuilt from the ground up with immutable types, chaining and much more.
  • Moment.js [thanks to Mustafa] "A lightweight (4.3k) javascript date library for parsing, manipulating, and formatting dates" - Includes internationalization, calculations and relative date formats - Update 2017-03-29: Not quite so light-weight any more but still the most comprehensive solution, especially if you need timezone support. - Update 2021-02-28: No longer in active development.
  • date-fns [added 2017-03-29, thanks to Fractalf] Small, fast, works with standard JS date objects. Great alternative to Moment if you don't need timezone support.
  • SugarJS - A general helper library adding much needed features to JavaScripts built-in object types. Includes some excellent looking date/time capabilities.
  • strftime - Just what it says, nice and simple
  • dateutil - This is the one I used to use before MomentJS
  • node-formatdate
  • TimeTraveller - "Time Traveller provides a set of utility methods to deal with dates. From adding and subtracting, to formatting. Time Traveller only extends date objects that it creates, without polluting the global namespace."
  • Tempus [thanks to Dan D] - UPDATE: this can also be used with Node and deployed with npm, see the docs

There are also non-Node libraries:

  • Datejs [thanks to Peter Olson] - not packaged in npm or GitHub so not quite so easy to use with Node - not really recommended as not updated since 2007!
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ node.js โ€บ node-js-date-format-api
Node.js Date.format() API - GeeksforGeeks
January 8, 2025 - Example 1: index.js ยท // Node.js ... the date and time // by using date.format() method const value = date.format(now,'YYYY/MM/DD HH:mm:ss'); // Display the result console.log("current date and time : " + value) Run the index.js ...
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ date-format
date-format - npm
September 26, 2022 - var format = require('date-format'); format(); // defaults to ISO8601 format and current date format(new Date()); // defaults to ISO8601 format format('hh:mm:ss.SSS', new Date()); // just the time format(format.ISO8601_WITH_TZ_OFFSET_FORMAT, ...
      ยป npm install date-format
    
Published ย  Sep 26, 2022
Version ย  4.0.14
Author ย  Gareth Jones
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ dateformat
dateformat - npm
February 19, 2022 - import dateFormat, { masks } from "dateformat"; const now = new Date(); // Basic usage dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT"); // Saturday, June 9th, 2007, 5:46:21 PM // You can use one of several named masks dateFormat(now, ...
      ยป npm install dateformat
    
Published ย  Feb 19, 2022
Version ย  5.0.3
Author ย  Steven Levithan
๐ŸŒ
GitHub
github.com โ€บ felixge โ€บ node-dateformat
GitHub - felixge/node-dateformat: A node.js package for Steven Levithan's excellent dateFormat() function.
August 2, 2024 - Simply pass in // true as an additional argument (no argument skipping allowed in this case): dateFormat(now, "longTime", true); // 10:46:21 PM UTC // ...Or add the prefix "UTC:" or "GMT:" to your mask.
Starred by 1.3K users
Forked by 157 users
Languages ย  JavaScript 98.6% | Shell 1.4% | JavaScript 98.6% | Shell 1.4%
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ node.js โ€บ how-to-format-the-current-date-in-mm-dd-yyyy-hhmmss-format-using-node-js
How to format the current date in MM/DD/YYYY HH:MM:SS format using Node? - GeeksforGeeks
July 23, 2025 - const date = new Date(); // Function ... 12; } return input; }; // Data about date const format = { dd: formatData(date.getDate()), mm: formatData(date.getMonth() + 1), yyyy: date.getFullYear(), HH: formatData(date.getHours()), hh: ...
Find elsewhere
๐ŸŒ
Node-RED
discourse.nodered.org โ€บ general
Format date at 'YYYY:MM:DD HH:MM;SS' - General - Node-RED Forum
November 23, 2023 - How can you format the date in the logs? now I have it '23 Nov 2023 hh:mm:ss' it must have been so 'YYYY:MM:DD HH:MM;SS' Writing a function just adds the date again(( const currentDate = new Date(); const formattedDateTime = currentDate.toI...
๐ŸŒ
Medium
medium.com โ€บ @techsolutionstuff โ€บ node-js-get-current-date-and-time-example-e7893d95c174
Node JS Get Current Date And Time Example | by Techsolutionstuff | Medium
September 29, 2023 - Also, you can get the current date, month, year, hour, minutes & seconds in node js. In node.js you can get the current date-time formatted as YYYY-MM-DD hh:mm:ss, YYYY-MM-DD, DD-MM-YYYY, etc.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_date_formats.asp
JavaScript Date Formats
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... The ISO format follows a strict standard in JavaScript. The other formats are not so well defined and might be browser specific. Independent of input format, JavaScript will (by default) output dates in full text string format:
๐ŸŒ
GitHub
github.com โ€บ nomiddlename โ€บ date-format
GitHub - nomiddlename/date-format: node.js formatting of Date objects as strings. Probably exactly the same as some other library out there.
February 18, 2025 - var format = require('date-format'); format(); // defaults to ISO8601 format and current date format(new Date()); // defaults to ISO8601 format format('hh:mm:ss.SSS', new Date()); // just the time format(format.ISO8601_WITH_TZ_OFFSET_FORMAT, ...
Starred by 60 users
Forked by 17 users
Languages ย  JavaScript 100.0% | JavaScript 100.0%
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ node.js โ€บ formatting dates in nodejs
How to Format Dates in NodeJS | Delft Stack
February 2, 2024 - Day.js is a minimalist Node js library that parses, validates, manipulates, and displays dates and times for modern browsers. We can install Day.js using the npm install command below. ... We can now make our main file using the require() method and print the current local date-time below. const dayjs = require('dayjs'); let today = dayjs(); console.log(today.format());
๐ŸŒ
Till it's done
tillitsdone.com โ€บ blogs โ€บ node-js-date-formatting-guide
Format Dates and Times in Node.js with Moment.js - Tillitsdone
November 7, 2024 - Learn how to effectively handle date and time formatting in Node.js using Moment.js. Master basic formatting, time zones, relative time, and best practices for your Node.js applications.
๐ŸŒ
Newline
newline.co โ€บ @anthonygore โ€บ formatting-dates-in-node-with-momentjs--5a41bbff
Formatting Dates in Node with Moment.js - Newline.co
September 23, 2020 - Working with dates in a Node app can be tricky as there are so many ways to format and display them.
๐ŸŒ
Thomascollart
thomascollart.com โ€บ javascript-date-formating
How to Format a Date in JavaScript? | Thomas Collart
November 21, 2023 - Moment.js allows parsing, validating, manipulating, and displaying dates and times in your JS code. Like other node packages, you can install moment.js with npm, yarn, or another node package manager. npm install moment --save # with npm yarn add moment # with Yarn ยท Another solution is to use the moment.js library. Using the unix function takes your UNIX timestamp in the correct format without requiring you to multiply it by 1000.
๐ŸŒ
date-fns
date-fns.org
date-fns - modern JavaScript date utility library
date-fns provides the most comprehensive yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.
๐ŸŒ
Squash
squash.io โ€บ how-to-format-javascript-date-as-yyyy-mm-dd
How to Format JavaScript Dates as YYYY-MM-DD - Squash.io
Code Snippet: Formatting Current Date as YYYY MM DD in Node.js ยท Table of Contents ยท To format a JavaScript date as YYYY-MM-DD, you can use various methods available in the JavaScript Date object.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-format-a-date-with-javascript-date-formatting-in-js
How to Format a Date with JavaScript โ€“ Date Formatting in JS
November 7, 2024 - Apart from formatting dates for display, it's essential to handle user input for dates effectively. Here are a few considerations: Parsing User Input: Use the Date.parse() method or external libraries like Moment.js or Luxon to parse user-provided dates into valid Date objects.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Intl โ€บ DateTimeFormat
Intl.DateTimeFormat - JavaScript | MDN
The initial value of the [Symbol.toStringTag] property is the string "Intl.DateTimeFormat". This property is used in Object.prototype.toString(). ... Getter function that formats a date according to the locale and formatting options of this DateTimeFormat object.