close

How do you format a Date/Time in TypeScript?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How do you format a Date/Time in TypeScript? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How do you format a Date/Time in TypeScript ?

  1. How do you format a Date/Time in TypeScript ?

    do you format a Date/Time in TypeScript In case you need it for angular, the simplest aproach with minimum code is:
    import {formatDate} from '@angular/common'; formatDate(Date.now(),'yyyy-MM-dd','en-US');

  2. How do you format a Date/Time in TypeScript ?

    do you format a Date/Time in TypeScript In case you need it for angular, the simplest aproach with minimum code is:
    import {formatDate} from '@angular/common'; formatDate(Date.now(),'yyyy-MM-dd','en-US');

Method 1

Option 1: Momentjs:

Install:

npm install moment --save

Import:

import * as moment from 'moment';

Usage:

let formattedDate = (moment(yourDate)).format('DD-MMM-YYYY HH:mm:ss')

Option 2: Use DatePipe if you are doing Angular:

Import:

import { DatePipe } from '@angular/common';

Usage:

const datepipe: DatePipe = new DatePipe('en-US')
let formattedDate = datepipe.transform(yourDate, 'DD-MMM-YYYY HH:mm:ss')

Method 2

In case you need it for angular, the simplest aproach with minimum code is:

import {formatDate} from '@angular/common';

formatDate(Date.now(),'yyyy-MM-dd','en-US');

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment