简体   繁体   English

C#DateTime.ToString()方法是否存在时区问题?

[英]Does C# DateTime.ToString() method have timezone issues?

My server is in UTC time zone, and my local system is in another time zone. 我的服务器在UTC时区,而我的本地系统在另一个时区。 I have a datetime object, pointing to the first second of a particular date, say the 7th of Jan, 2018 ie 07-01-2019 00:00:00 . 我有一个datetime对象,指向特定日期的第一秒,例如2018年1月7日,即07-01-2019 00:00:00 If I call the toString("dd MMM yyyy") method on this object and return it to my local system and display it, I observe that the date is off by a day, showing 6 Jan 2019. Could you explain why it might be so? 如果我在此对象上调用toString(“ dd MMM yyyy”)方法并将其返回到本地系统并显示,我观察到日期已经过一天了,显示为2019年1月6日。请您解释一下为什么会这样所以?

EDIT:Initially, my intention in asking the question was to know if the dateTime.ToString() method had any noticeable problems. 编辑:最初,我问这个问题的目的是要知道dateTime.ToString()方法是否有任何明显的问题。 Could someone please explain to me why this behaves the way it does? 有人可以向我解释为什么这种行为方式如此吗?

My testing of the problem revealed that the error is reproducible only when the serve hosting the webpage & the client interacting with the webpage are in different time zones. 我对问题的测试表明,仅当托管网页的服务器和与网页进行交互的客户端位于不同时区时,才可以重现该错误。 My client is in IST & the server is in UTC. 我的客户端在IST中,服务器在UTC中。 Suppose I pick 29th Jan, 2019 & 7th Feb, 2019 as the start & end dates respectively. 假设我分别选择2019年1月29日和2019年2月7日作为开始和结束日期。

The client side sends the following as parameters to the API. 客户端将以下内容作为参数发送到API。 I have noticed by switching to different timezones that the parameters sent are in UTC only. 通过切换到不同的时区,我注意到发送的参数仅采用UTC。

fromDate: "2019-01-28T18:30:00.000Z"
toDate: "2019-02-06T18:30:00.000Z"

The API accepts them and does the following API接受它们并执行以下操作

{
    DateTime utcStart = DateTime.SpecifyKind(Convert.ToDateTime(fromDate), 
    DateTimeKind.Unspecified);
    DateTime utcEnd = DateTime.SpecifyKind(Convert.ToDateTime(toDate), 
    DateTimeKind.Unspecified);
    dummyMethod(utcStart,utcEnd,true, null, out strDuration);
}

dummyMethod(DateTime? fromDate, DateTime? toDate, Boolean convertToUTC, int? timeZoneKey, out string displayString)
{
    DateTime centerStartDate = GetCurrentTime(timeZoneKey).Date;
    DateTime centerEndDate = GetCurrentTime(timeZoneKey).Date;

    if (fromDate.HasValue)
        centerStartDate = fromDate.Value.Date;
    if (toDate.HasValue)
        centerEndDate = toDate.Value.Date;

    displayString = centerStartDate.ToString("dd MMM yyyy") + " - " + centerEndDate.ToString("dd MMM yyyy");
}

The displayString is directly returned via JSON, and the displayString is where things seem to be going wrong. displayString通过JSON直接返回,而displayString似乎出了问题。

TIA TIA

如果要获取服务器/ Utc时间的日期,则必须致电:

DateTime.UtcNow

This is because the DateTime actually stores ticks from 00:00:00 01-01-0001 AD From MSDN: 这是因为DateTime实际上存储了来自MSDN的00:00:00 01-01-0001 AD的滴答声:

A particular date is the number of ticks since 12:00 midnight, January 1, 0001 AD (CE) in the GregorianCalendar calendar. 一个特定的日期是公历日历中自公元0001年1月1日午夜12:00(CE)起的滴答数。

Because of that, the DateTime object will have the UtcTime in terms of ticks, and when you receive it in your local system, you will get the adjusted DateTime when you print DateTime.ToString(); 因此,DateTime对象将具有单位滴答的UtcTime,并且当您在本地系统中收到它时,在打印DateTime.ToString();时将获得调整后的DateTime DateTime.ToString();

This can be corrected by using UtcTime. 可以使用UtcTime进行更正。

DateTime.UtcNow; DateTime.UtcNow;

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

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