简体   繁体   English

Angular 6中Bootstrap日期选择器中的日期格式

[英]Date format in Bootstrap Date Picker in Angular 6

In Angular 6, I apply date picker with a bsDaterangepicker class for a date range. 在Angular 6中,我将日期选择器与bsDaterangepicker类一起应用于日期范围。

<input type="text" (ngModelChange)="dateFilterChanged($event)" [(ngModel)]="myDateField" value="{{ myDateField | date:'yyyy-MM-dd' }}" [bsConfig]="{dateInputFormat: 'yyyy-MM-dd'}" bsDaterangepicker>

and emit the value with the following function and emitter: 并使用以下函数和发射器发射该值:

dateFilterChanged(filterValue: string) {
    console.log('filterValue', filterValue);
    this.dateFilterChanged.emit(filterValue)
}

The problem is, the format of the emitted date is not "yyyy-MM-dd", but a gmt string: 问题是,发出日期的格式不是“ yyyy-MM-dd”,而是gmt字符串:

[Wed May 01 2019 14:04:12 GMT+0300 (GMT+03:00), Sat Jun 15 2019 14:04:12 GMT+0300 (GMT+03:00)]

How can I get emit the date value in "yyyy-MM-dd" format? 如何获得以“ yyyy-MM-dd”格式发出的日期值?

You can use date pipe like this : 您可以像这样使用日期管道:

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

For more information visit this link : 有关更多信息,请访问此链接:

https://angular.io/api/common/DatePipe https://angular.io/api/common/DatePipe

you can use it in typescript also :: 您也可以在打字稿中使用它::

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

constructor(private datePipe: DatePipe,){
}
// in your function 
 element.last_assessment_date = this.datePipe.transform(element.last_assessment_date, 'yyyy-MM-dd');

You can use Date Pipe for this. 您可以为此使用日期管道。
Example: 例:

let dateObj = Your object;

And use the Pipe like below. 并使用如下所示的管道。

{{ dateObj | date }} // output is 'Jun 15, 2015'

Or there is a JS library called moment. 还是有一个称为moment的JS库。

You can get that from https://momentjs.com/ and follow the instruction to install it. 您可以从https://momentjs.com/获得,并按照说明进行安装。 And they have mentioned all the Format Dates. 他们提到了所有格式日期。 Just pass the GMT string to moment() and you will get your desired format. 只需将GMT字符串传递给moment(),您将获得所需的格式。

You can get that from https://momentjs.com/ and follow the instruction to install it. 您可以从https://momentjs.com/获得,并按照说明进行安装。 And they have mentioned all the Format Dates. 他们提到了所有格式日期。 Just pass the GMT string to moment() and you will get your desired format. 只需将GMT字符串传递给moment(),您将获得所需的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM