为什么Date区域会自动改变,因为它少了一天

本文关键字:因为 一天 改变 区域 Date 为什么 | 更新日期: 2023-09-27 18:10:54

我使用一个角UI日期时间选择器来选择日期,当用户提交表单时,前端的日期在GMT区域,但是当对象传递到web api时,它被转换为ISO格式,因为日期少了一天,即如果选择的日期是9月24日,在数据库中它被存储为9月23日。

在不使用任何其他js(如moment.js或dateFormat.js)的情况下,是否有任何解决方案?

下面是我使用的:-

'use strict';
module App.Controllers {
export interface IPitch {
        RegionId: number;
        CountryId: number;
        CityId: number;
        GHCId: number;
        DateInvited: Date;
        EstimatedAnnualBillingLocal: number;
        EstimatedUpsideAnnualBillingLocal: number;
        EstimatedAnnualBillingUS: number;
        EstimatedUpsideAnnualBillingUS: number;
    }
export interface IPitch {
        RegionId: number;
        CountryId: number;
        CityId: number;
        GHCId: number;
        DateInvited: Date;
        EstimatedAnnualBillingLocal: number;
        EstimatedUpsideAnnualBillingLocal: number;
        EstimatedAnnualBillingUS: number;
        EstimatedUpsideAnnualBillingUS: number;
    }
export class AddPitchCtrl implements IAddPitchCtrl {
        public static controllerId = 'addPitchCtrl';
        common: App.Shared.ICommon;
        controllerId: string;
        private log: Function;
        title: string;
        inputs: any;
        pitch: IPitch; 
 constructor(common: App.Shared.ICommon, mi: any, pInputs: any, datacontext: App.Services.IDatacontext, m: any, commondata: App.Services.ICommonData) 
{
this.pitch = new Pitch();
}
//this method is called on button click//
private addPitch() {
//when i post the data using this method the property "this.pitch.DateInvited" is **Thu Sep 24 2015 00:00:00 GMT+0530** (Indian Standard Time)(i.e. in controller)
//the below method calls the web api which actually stores data in db//
return this.datacontext.addPitch(this.pitch).then(data => {
            toastr.success('Pitch information added successfully.',    'Success');
            this.mi.close("");
        }, errorData => {
                toastr.error('Sorry, Something went wrong', 'Error');
                this.isAddBtnDisable = false;
            });
}
} 
}//Controller Ends here
//web api method//
public Pitch Add(PitchInfo pitchEntity)
    {
// here if i see pitchEntity.DateInvited it shows **"{9/23/2015 6:30:00 PM}"**//
        try
        {
Pitch objPitch = new Pitch();
            objPitch.RegionId = pitchEntity.RegionId;
            objPitch.CountryId = pitchEntity.CountryId;
            objPitch.CityId = pitchEntity.CityId;
            objPitch.GHCId = pitchEntity.GHCId;
            objPitch.TypeId = pitchEntity.TypeId;
            objPitch.SourceId = pitchEntity.SourceId;
            objPitch.LevelId = pitchEntity.LevelId;
            objPitch.StatusId = pitchEntity.StatusId;
            objPitch.DateInvited = pitchEntity.DateInvited; // 9/23/2015 6:30:00 PM
            objPitch.EstimatedAnnualBillingLocal = pitchEntity.EstimatedAnnualBillingLocal;
            objPitch.EstimatedAnnualBillingUS = pitchEntity.EstimatedAnnualBillingUS;
            objPitch.EstimatedUpsideAnnualBillingLocal = pitchEntity.EstimatedUpsideAnnualBillingLocal;
            objPitch.EstimatedUpsideAnnualBillingUS = pitchEntity.EstimatedUpsideAnnualBillingUS;
}
catch (Exception ex)
        {
            log.Error(string.Format("Error: {0}'n Stack Trace:{1}", ex.Message, ex.StackTrace));
            throw ex;
        }
    }
}

为什么Date区域会自动改变,因为它少了一天

我通过创建一个新的date对象并将其传递给web api来解决这个问题。

说我使用角日期时间选择器选择的日期绑定到一个模型"vm"。date应邀"所以在控制器中我输入的是

this.DateInvited=new Date(Date.UTC(this.DateInvited.getFullYear(),this.DateInvited.getMonth(),this.DateInvited.getDate()));

您可以通过使用Datetime来处理这个问题。UtcNow财产。你不需要在客户端做任何改变(比如angular/jquery),只需要在服务器端做一些修改。