簡體   English   中英

如何以所需格式獲取日期:ionic 4

[英]How to get the date as in required format: ionic 4

我想以(yyyy mm dd hh mm ss)的格式在下面的代碼中聲明日期。

postData(data) {
    let headers = new HttpHeaders();
    headers.append('Content-Type','application/json');
    var pingData = {
        date: new Date(year, month, day, hours, minutes, seconds, milliseconds)
    }
}

當我按上面的代碼所示聲明它時,出現以下錯誤:

找不到名稱“年份”。 找不到名稱“月” .....

我希望date變量為string類型。 那么,如何獲得上述格式中提到的日期?

試試這個可能對您有幫助

在AppModulePage中

import {DatePipe} from '@angular/common';

providers: [DatePipe]

在demo.component.ts中

import { DatePipe } from '@angular/common';
.
.
constructor(private datePipe: DatePipe) {}

ngOnInit() {
   var date = new Date();
   console.log(this.datePipe.transform(date,"yyyy mm dd hh mm ss")); 
}

使用這種格式將可以工作var d = new Date(2018, 11, 24, 10, 33, 30, 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM