1

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 }));
Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
Rohan Agarwal
  • 2,441
  • 2
  • 18
  • 35
  • Hi, the solution to the question you provided is the one that I am using in the first place. However, I want to format THAT output to a particular format, without the use of momentjs or writing any particular formatting function. Thanks :) – Rohan Agarwal Aug 02 '18 at 07:19
  • @GONZALOPANIAGUA - its not duplicate , its question for angular and angular has DatePipe for doing such formating ..no need of long javascript code – Pranay Rana Aug 02 '18 at 07:26

1 Answers1

4

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

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263