英文:
Updating DateTime property in C# and .NetStandard 2.1 Issues
问题
以下是翻译好的部分:
{
amount: 10000,
date: "2023-07-14T13:46:25.626",
endDate: "Sun Jul 16 2023 00:00:00 GMT+0100 (West Africa Standard Time)",
id: "323a3676-765b-cbea-57ee-08db8470b7c2",
isLastPayment: false,
profileId: "e9c79eaa-ca4e-c843-de40-08db7bd10853",
startDate: "Wed Jul 12 2023 00:00:00 GMT+0100 (West Africa Standard Time)"
}
{
id: '323a3676-765b-cbea-57ee-08db8470b7c2',
date: '2023-07-14T13:46:25.626',
amount: 10000,
endDate: 'Sun Jul 16 2023 00:00:00 GMT+0100 (West Africa Standard Time)',
isLastPayment: false,
profileId: "e9c79eaa-ca4e-c843-de40-08db7bd10853",
startDate: 'Wed Jul 12 2023 00:00:00 GMT+0100 (West Africa Standard Time)'
}
export class Payment {
id: string;
date: Date;
amount: number;
isLastPayment: boolean;
profileId: string;
//Date Range for Payment
startDate?: Date;
endDate?: Date
}
public class PaymentObject
{
public Guid Id { get; set; }
public DateTime Date { get; set; }
public decimal Amount { get; set; }
public bool IsLastPayment { get; set; }
public Guid ProfileId { get; set; }
//Date Range for Payment
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
这些是您提供的JSON数据,TypeScript模型,和后端对象模型的翻译。如果您有任何其他问题或需要进一步的帮助,请随时提出。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
{
amount:10000
date:"2023-07-14T13:46:25.626"
endDate:Sun Jul 16 2023 00:00:00 GMT+0100 (West Africa Standard Time) {}
id:"323a3676-765b-cbea-57ee-08db8470b7c2"
isLastPayment:false
profileId:"e9c79eaa-ca4e-c843-de40-08db7bd10853"
startDate:Wed Jul 12 2023 00:00:00 GMT+0100 (West Africa Standard Time) {}
}
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
{
id:'323a3676-765b-cbea-57ee-08db8470b7c2',
date: '2023-07-14T13:46:25.626',
amount: 10000,
endDate: Sun Jul 16 2023 00:00:00 GMT+0100 (West Africa Standard Time) {},
isLastPayment:false,
profileId:"e9c79eaa-ca4e-c843-de40-08db7bd10853",
startDate:Wed Jul 12 2023 00:00:00 GMT+0100 (West Africa Standard Time) {}
}
<!-- end snippet -->
I am using angular on the frontend to send this json to my API for and Update request.
I changed the dates properties to 12 and 16 of July 2023
Whent it gets to API Controller, the dates changes and the 11 and 15 of July 2023, it resets a day behind my intended dates
{
id:'323a3676-765b-cbea-57ee-08db8470b7c2',
date: '2023-07-14T13:46:25.626',
amount: 10000,
endDate: Sun Jul 15 2023 ......,
isLastPayment:false,
profileId:"e9c79eaa-ca4e-c843-de40-08db7bd10853",
startDate:Wed Jul 11 2023 ........
}
this is my typsecript model for the frontend
export class Payment {
id: string;
date: Date;
amount: number;
isLastPayment: boolean;
profileId: string;
//Date Range for Payment
startDate?: Date;
endDate?: Date
}
and this is my Object model for the backend
public class PaymentObject
{
public Guid Id { get; set; }
public DateTime Date { get; set; }
public decimal Amount { get; set; }
public bool IsLastPayment { get; set; }
public Guid ProfileId { get; set; }
//Date Range for Payment
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
The trick this is only affecting the date property.
Please I'll appreciate all the help I can get.
答案1
得分: 0
通常应用程序中有两种日期类型:时间戳(例如创建日期或某个短期截止日期,如下一个小时)和日历/业务日期(例如支付截止日期)。
时间戳是UTC日期(零偏移,可以在浏览器中转换为本地时间),日历日期类似于yyyy-mm-dd
(它不依赖于客户端时区,通常假定为您的业务时区)。
在您的情况下,您打算使用日期作为日历日期,但默认情况下由于客户端和服务器日期处理而不会按预期工作。
为了简洁起见,下面没有使用ISO格式。
当服务器序列化日期时,它不区分时间戳和日历日期,它们将以相同的方式序列化,当客户端接收到这些日期时(看起来像您有某处的new Date(jsonString)
),它将自动将其转换为本地浏览器时间,这对于日历日期来说是不正确的,因为日期被解释为时间戳,2023-06-21 00:00:00
可能会导致 2023-06-22
或 2023-06-21
,这取决于浏览器的时区偏移(例如,偏移可以是+3或-3)。
当您将日期发送回服务器时,浏览器会序列化日期,但不包括偏移信息,因此您会失去关于客户端时区偏移的信息,结果服务器不知道在浏览器中显示的日期是什么。例如,在具有+3偏移的时区中,2023-06-21 00:00:00
将序列化为 2023-06-20 21:00:00
,服务器得到的日期比实际日期要早一天。
长话短说,您应该使用类似yyyy-mm-dd
的字符串来传递日历日期,避免在其中使用js日期。
英文:
Usually there are two types of dates in applications: timestamps (e.g. created date, or some short deadline like until next hour) and calendar/business dates (e.g. payment due date).
Timestamp is date in UTC (zero offset, can be converted to local time in browser), calendar date is something like yyyy-mm-dd
(it does not depend on client timezone, usually it is assumed to be in your business timezone).
In your case you intend to use Date as calendar date but by default it will not work as expected due to client and server date handling.
I'm not using ISO format below for brevity.
When server serializes date it does not differentiate between timestamps and calendar dates, it will serialize them in the same way, and when client receives these dates (looks like your have new Date(jsonString)
somewhere) it will automatically convert it to local browser time which is not correct for calendar dates because date is interpreted as timestamp and 2023-06-21 00:00:00
can result in 2023-06-22
or 2023-06-21
depending on browser timezone offset (offset can be +3 or -3 for example).
When you send date back to server browser serializes dates without offset information, so you loose information about client timezone offset and as a result server does not know what date was displayed in browser. For example 2023-06-21 00:00:00
in timezone with +3 offset will be serialized as 2023-06-20 21:00:00
and server gets date that is one day behind.
Long story short, you should use strings like yyyy-mm-dd
to pass around calendar dates and avoid using js Date for them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论