Angular2 + C#Web Api-服务器端用错误的时间保存日期时间

20.*_*les 5 c# asp.net-mvc asp.net-web-api angular

我正在尝试保存日期时间,但是当webapi收到日期时,它将日期更改为错误的时区。

我的角度应用程序返回:Wed May 22 2019 11:03:35 GMT+0200 但是我的Web Api返回:22/05/2019 09:03:35所以在我的SQL Server数据库中,它被错误地保存了。

我希望它被完全保存 22/05/2019 11:03:35

在我的角度应用程序中,我有这个:

myComponent.component.html

<div class="row">
  <div class="col-4" style="float:left;">
      <div class="form-group">                                
          <div class="input-group">
              <input [ngModel]="newDate | date:'dd/MM/yyyy HH:mm:ss'" 
                  (ngModelChange)="newDate=$event" 
                  name="newDate" 
                  type="datetime"
                  id="new-date"
                  class="form-control">
          </div>
      </div>                    
  </div>
</div>   
<div class="row">
    <div class="col">
        <div class="form-group">                                                                               
            <button type="submit" (click)="onSubmit()" class="btn btn-primary">Submit</button>
        </div>
    </div>            
</div>    
<br>
{{saveDate}}
Run Code Online (Sandbox Code Playgroud)

myComponent.component.ts

import { Component, OnInit, ViewChild, } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit  {  
  newDate = Date.now()  
  saveDate: Date;
  ngOnInit() {    
  }

  onSubmit() {
    this.saveDate = new Date(this.newDate);    
    // call web api function
    // ...
    this.reportService.register(this.saveDate).subscribe(() => {});         
  }
}
Run Code Online (Sandbox Code Playgroud)

这是一个stackblitz示例:https ://stackblitz.com/edit/angular-uadhxa

我的Web API函数是这样的:

[ResponseType(typeof(DateTime))]
public async Task<IHttpActionResult> PostDatetime(DateTime _dateTime)
{            
   if (!ModelState.IsValid)
   {
      return BadRequest(ModelState);
   }

   using (db)
   {
      db.Reports.Add(_dateTime);
      await db.SaveChangesAsync();                
   }         

   return CreatedAtRoute("DefaultApi", new { id = report.ReportID }, _dateTime);
}
Run Code Online (Sandbox Code Playgroud)

我不明白这是我如何从angular应用程序传递日期时间的问题还是webapi上的问题。

你能帮助我吗?非常感谢

Pra*_*ale 4

在组件中使用DatePipe以根据您的要求格式化日期:

TS代码:

import { Component, OnInit, ViewChild, } from '@angular/core';
import { DatePipe } from '@angular/common'  // Import this

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [DatePipe] // Add this
})
export class AppComponent implements OnInit {
  newDate = Date.now()
  saveDate: any; // change to any 

  constructor(private datePipe: DatePipe) { } // In constructor
  ngOnInit() {
  }

  onSubmit() {
    this.saveDate = this.datePipe.transform(this.newDate, 'dd/MM/yyyy HH:mm:ss');
    console.log(saveDate ); // and use it as
    // call web api function
    // ...
    //this.reportService.register(this.saveDate).subscribe(() => {});  
  }
}
Run Code Online (Sandbox Code Playgroud)

Working_Demo