admin管理员组文章数量:1403438
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
本文标签: javascriptHow to get date and time in mmddyyy hhmmss in typescriptStack Overflow
版权声明:本文标题:javascript - How to get date and time in mm-dd-yyy hh-mm-ss in typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744412561a2605028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论