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

You can use the format function from date-fns library to format a date object in a particular format. To include timezone in the output you can use the z or zzz token.

Here's an example code:

index.ts
import { format } from 'date-fns';

const date = new Date();
const formattedDate = format(date, 'yyyy-MM-dd HH:mm:ss.SSS zzz');
console.log(formattedDate);
// Output: 2022-01-01 13:30:45.678 UTC+05:30
201 chars
7 lines

In this example, we're formatting the current date and time in the format 'yyyy-MM-dd HH:mm:ss.SSS zzz'. The HH token is used for 24-hour format, zzz token is used to include timezone information in the output, and SSS token is used for milliseconds.

Note that the timezone information will be based on the timezone of the system where the code is executed. If you want to format the date in a specific timezone, you can use the utcToZonedTime function from date-fns-tz library to convert the date to the required timezone before formatting it.

gistlibby LogSnag