Rohan Agarwal
Rohan Agarwal

Reputation: 2609

How to get date and time in mm-dd-yyy hh-mm-ss in typescript?

Currently, I am getting date time in the following format 'mm/dd/yyyy, hh:mm:ss'. How to get it in the following format 'mm-dd-yyyy hh-mm-ss'.

How to achieve this without using any library, and preferably by passing some args to the function itself?

Below is the code that am currently using (in Angular 5)

console.log(new Date().toLocaleString(undefined, { hour12: false }));

Upvotes: 2

Views: 10940

Answers (1)

Pranay Rana
Pranay Rana

Reputation: 176886

make use of DatePipe , that is provided by angular framework

{{ strDate | date :'MM-dd-yyyy hh-mm-ss' }

or in code you can do like this

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

@Component({
 selector: 'test-component',
  templateUrl: './test-component.component.html'
})
class TestComponent {

  constructor(private datePipe: DatePipe) {}

  formatDate(date= new Date()) {
    return this.datePipe.transform(date,'MM-dd-yyyy hh-mm-ss' );
  }
}

check here : DatePipe

Upvotes: 4

Related Questions