format a date in to the format 'yyyy-mm-dd hh:mm:ss.fff zzz' in javascript

To format a date in JavaScript, use the Date constructor and toDateString() method. However, for more advanced date formatting, you can use a third-party library like moment.js.

Here is an example of how to format a date in yyyy-mm-dd hh:mm:ss.fff zzz format using moment.js and timezone-js libraries:

index.tsx
// install moment.js and timezone-js libraries using npm
// npm install moment timezone-js

// import the moment.js and timezone-js libraries
const moment = require('moment');
const TimeZone = require('timezone-js');

// create a new `Date` object with the current date and time
const date = new Date();

// create a new `tz` object using the `timezone-js` library
const tz = new TimeZone();

// get the offset in minutes for the current timezone
const offset = tz.getTZO(date);

// get the formatted date string using `moment.js` library
const formattedDate = moment(date).utcOffset(offset).format('YYYY-MM-DD HH:mm:ss.SSS Z');

console.log(formattedDate);
658 chars
21 lines

This will output the current date and time in the format yyyy-mm-dd hh:mm:ss.fff zzz.

gistlibby LogSnag