Aya*_*ziz 1 datetime date typescript angular
当我将日期发送到 API 时,如何使用打字稿以特定格式 (DateTime) 将当前日期发送到 API,它以这种格式发送,所以我收到了错误。
我的 ts 代码:
date: Date;
constructor( private dataStorageService: DataStorageService) {
this.date = new Date();
}
onSubmit(Message , form : FormGroup){
let newMsg = {
username: Message.username,
header: Message.header,
content: Message.content,
file: Message.file
}
this.dataStorageService.postMessage(newMsg, this.Id , this.date).subscribe(data => {
console.log('done');
console.log(this.date);
}, error => {
console.error("Error saving jobs!");
})
}
Run Code Online (Sandbox Code Playgroud)
http://api.azharcouncil.com/api/notification/PostNotification?user_id=15&Header=hj&Content=hjh&File_Path=null& date=Wed%20Nov%2015%202017%2010:44:56%20GMT+0200%20(和埃及%20 %20 时间)
所以我的请求由于日期格式而无效...
这是您问题的完整答案。
从主模块中导入 DatePipe 并将其提供给您的提供程序数组中 main.module.ts 中的主模块,如下所示:
import {DatePipe} from '@angular/common';
providers: [DatePipe]
Run Code Online (Sandbox Code Playgroud)使用这些方法在 ts 文件中使用它,也在 ts 文件中从 @angular/common 导入 DatePipe。
import {DatePipe} from '@angular/common';
Run Code Online (Sandbox Code Playgroud)然后在你的构造函数中初始化:
constructor(private _http: HttpClient, public datepipe: DatePipe) { }
Run Code Online (Sandbox Code Playgroud)现在使用这些方法来获取正确的日期格式:
let myDate = new Date();
console.log(this.datepipe.transform(myDate, 'yyyy-mm-dd'));
someDateVar = this.datepipe.transform(myDate, 'yyyy-mm-dd');
Run Code Online (Sandbox Code Playgroud)现在 someDateVar 可用于将其发送到具有您需要的日期格式的服务器。另请注意,您还可以使用 Angular 日期管道文档中提供的多种其他日期格式。
归档时间: |
|
查看次数: |
12031 次 |
最近记录: |