Javascript 如何使用 DatePipe 在 Angular 4、5、6 及更高版本中获取日期和时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50270330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:34:56  来源:igfitidea点击:

How to get date and time in Angular 4,5,6 and above using DatePipe

javascriptangulartypescriptdatedate-pipe

提问by Nikson

I am working in an angular 4 application, Here I need to get the current Date and Time Usingangular DatePipe.

我正在使用 angular 4 应用程序,在这里我需要获取当前的Date and Time Usingangular DatePipe

I want to get the date and time in the following format

我想以以下格式获取日期和时间

dd-mm-yyyy hh:MM:ss AM/PM

dd-mm-yyyy hh:MM:ss 上午/下午

I got the expected by using the Angular DatePipe as follows

我通过使用 Angular DatePipe 得到了预期的结果,如下所示

<p>{{today | date:'dd-MM-yyyy hh:mm:ss a':'+0530'}}</p> 

output :

输出 :

10-05-2018 03:28:57 PM

Here I What I want to do is get the same output from my app.component.ts without touching the HTML's

在这里,我想要做的是从我的 app.component.ts 获得相同的输出,而无需触及 HTML

So I tried the below code but it generates a 13 digit timestamp

所以我尝试了下面的代码,但它生成了一个 13 位的时间戳

today = Date.now();
    fixedTimezone = this.today;

SO how can I get the date and time as the mentioned format purely from app.component.ts file without using HTML's.

那么如何在不使用 HTML 的情况下完全从 app.component.ts 文件中获取上述格式的日期和时间。

回答by xdeepakv

Will works on Angular 6or above

将适用于Angular 6或更高版本

You don't need any 3rd party library. You can format using angular method/util. import formatDatefrom the common package and pass other data. See the below-given example.

您不需要任何 3rd 方库。您可以使用 angular 进行格式化method/utilformatDate从公共包中导入并传递其他数据。请参阅下面给出的示例。

import { Component } from '@angular/core';
import {formatDate } from '@angular/common';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  today= new Date();
  jstoday = '';
  constructor() {
    this.jstoday = formatDate(this.today, 'dd-MM-yyyy hh:mm:ss a', 'en-US', '+0530');
  }
}

stackblitz: https://stackblitz.com/edit/angular-vjosat?file=src%2Fapp%2Fapp.component.ts

stackblitz:https://stackblitz.com/edit/angular-vjosat ?file =src%2Fapp%2Fapp.component.ts

回答by bereket gebredingle

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"); 

returns 10, 05, 2018 7:26:57 PM

返回 10, 05, 2018 7:26:57 PM

you need npm package npm i dateformathere is a link for the npm package https://www.npmjs.com/package/dateformat

你需要 npm 包npm i dateformat这里是npm 包的链接https://www.npmjs.com/package/dateformat

Here is another question that inspires me How to format a JavaScript date

这是另一个启发我的问题How to format a JavaScript date



h:MM:ss TTresults 7:26:57 PM

h:MM:ss TT结果 7:26:57 PM

HH:MM:ssresults 13:26:57

HH:MM:ss结果 13:26:57

Here is it https://jsfiddle.net/5z1tLspw/

这是https://jsfiddle.net/5z1tLspw/

I hope that helps.

我希望这有帮助。

回答by Rajan Desai

Uses the function formatDate to format a date according to locale rules.

使用函数 formatDate 根据区域设置规则格式化日期。

{{ value_expression | date [ : format [ : timezone [ : locale ] ] ] }}

It may be useful:)

它可能有用:)