如何使用DatePipe在Angular 4,5,6及更高版本中获取日期和时间 [英] How to get date and time in Angular 4,5,6 and above using DatePipe
问题描述
我正在使用angular 4应用程序,在这里我需要获取当前的Date and Time Using
angular DatePipe
.
I am working in an angular 4 application, Here I need to get the current Date and Time Using
angular DatePipe
.
我想以以下格式获取日期和时间
I want to get the date and time in the following format
dd-mm-yyyy hh:MM:ss AM/PM
我通过使用Angular DatePipe达到了预期的效果
I got the expected by using the Angular DatePipe as follows
<p>{{today | date:'dd-MM-yyyy hh:mm:ss a':'+0530'}}</p>
输出:
10-05-2018 03:28:57 PM
在这里,我要做的是从我的app.component.ts中获得相同的输出,而无需触摸HTML
Here I What I want to do is get the same output from my app.component.ts without touching the HTML's
所以我尝试了下面的代码,但是它生成了13位的时间戳记
So I tried the below code but it generates a 13 digit timestamp
today = Date.now();
fixedTimezone = this.today;
那么我怎么能完全从app.component.ts文件中获得上述格式的日期和时间,而又不使用HTML.
SO how can I get the date and time as the mentioned format purely from app.component.ts file without using HTML's.
推荐答案
let dateFormat = require('dateformat');
let now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
Thursday, May 10th, 2018, 7:11:21 AM
这种格式与您的问题完全相同
And this format is exactly like your question
dateFormat(now, "dd, mm, yyyy, h:MM:ss TT");
返回10, 05, 2018 7:26:57 PM
您需要npm软件包npm i dateformat
这是npm软件包 https://www.npmjs.com/package/dateformat 的链接
you need npm package npm i dateformat
here is a link for the npm package https://www.npmjs.com/package/dateformat
这是另一个启发我的问题如何格式化JavaScript日期
Here is another question that inspires me How to format a JavaScript date
h:MM:ss TT
结果7:26:57 PM
HH:MM:ss
结果13:26:57
我希望能帮上忙.
这篇关于如何使用DatePipe在Angular 4,5,6及更高版本中获取日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!