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 }));
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 }));
Share
Improve this question
edited Aug 2, 2018 at 8:02
Bahman Parsa Manesh
2,3683 gold badges18 silver badges36 bronze badges
asked Aug 2, 2018 at 7:13
Rohan AgarwalRohan Agarwal
2,6093 gold badges19 silver badges37 bronze badges
2
- 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 Commented Aug 2, 2018 at 7: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 Commented Aug 2, 2018 at 7:26
1 Answer
Reset to default 4make 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/mon';
@Component({
selector: 'test-ponent',
templateUrl: './test-ponent.ponent.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