简体   繁体   English

C#DateTime不正确

[英]C# DateTime not correct

I have a problem with the difference in getting the time which has arisen this week, possibly due to Daylight Savings Time in the US There is an hour difference in C# and Java (Android). 我在获取本周出现的时间方面存在差异,这可能是由于美国的夏令时造成的。在C#和Java(Android)中存在小时差异。 I want the Java to behave the same as the C#. 我希望Java的行为与C#相同。

Windows 10 - C# Windows 10-C#

value = DateTime.Now;
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
System.Diagnostics.Trace.WriteLine("span: " + (int)span.TotalSeconds);

Unix epoch seconds: Unix纪元秒:
1489764265 1489764265
Which converts to: 转换为:
Fri, 17 Mar 2017 15:24:25 GMT 2017年3月17日星期五15:24:25 GMT

Java - Android Studio - Run on Samsung T5330-NU Java-Android Studio-在Samsung T5330-NU上运行

Date date = new Date(); // current date and time
Integer.toString(ConvertToTimestamp(date))

Unit epoch seconds: 单位纪元秒:
1489760686 1489760686
Which converts to: 转换为:
Fri, 17 Mar 2017 14:24:46 GMT 2017年3月17日星期五14:24:46 GMT

The seconds (:25 vs :46) is fine. 秒(:25与:46)很好。 The hours are the problem (15 vs 14) Somehow daylight savings time must be involved. 时间是问题所在(15比14)。必须以某种方式节省夏时制。

What can be done? 该怎么办?

Using this to convert Unix epoch time: 用这个来转换Unix时代的时间:
http://www.onlineconversion.com/unix_time.htm http://www.onlineconversion.com/unix_time.htm

Edit: 编辑:
Original Java code gets current date/time like so: 原始Java代码会像这样获取当前日期/时间:

Calendar cal = Calendar.getInstance();

Based on the comment by @AkosNagy, I tried: 根据@AkosNagy的评论,我尝试了:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("MST"));

and

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("MDT"));

but there is no difference. 但没有区别。

Edit: 编辑:
Changed the title because this is in the end a question about C# and why the C# is not giving the correct time. 更改了标题,因为这最终是关于C#的问题,以及为什么C#没有给出正确的时间。

Edit: This is the line I was looking for: 编辑:这是我正在寻找的行:

Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime() isn't returning what you think it is. new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime()不会返回您认为的样子。 You need to specify the timezone: 您需要指定时区:

new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()

1970/1/1 00:00:00.000 in London was actually 1970/1/1 01:00:00.000 UTC (we were in permanent daylight savings at Unix epoch). 伦敦的1970/1/1 00:00:00.000实际上是1970/1/1 01:00:00.000 UTC(在Unix时代,我们处于永久的夏令时)。


Or your computer's clock/timezone may be wrong. 否则计算机的时钟/时区可能错误。 After all, you posted your code before Fri, 17 Mar 2017 15:24:25 GMT (it was about Fri, 17 Mar 2017 14:40:00 GMT ). 毕竟,您在Fri, 17 Mar 2017 15:24:25 GMT之前发布了代码(大约是Fri, 17 Mar 2017 14:40:00 GMT )。

But in any case, it makes the code more robust to specify the UTC timezone, since you won't be dependent upon the local computer's configuration. 但是无论如何,由于您不必依赖本地计算机的配置,因此它可以使代码更加健壮地指定UTC时区。

(CHECK EDIT BELOW) (请检查下面的内容)

In c#, DateTime.Now returns the local time of your computer so time zones don't matter, as you can see in MSDN documentation . 在c#中, DateTime.Now返回计算机的本地时间,因此时区无关紧要,如MSDN文档中所示

For java, I found these code snippets which use other methods like LocalDateTime.now() . 对于Java,我发现这些代码段使用了其他方法,如LocalDateTime.now()

EDIT: 编辑:

after reading your answer again, I understood that your problem is with the c# code. 再次阅读您的答案后,我了解到您的问题出在C#代码上。 that is my solution: instead of that Unix epoch seconds thing: 那是我的解决方案:而不是那个Unix纪元秒的事情:

value = DateTime.Now;
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
System.Diagnostics.Trace.WriteLine("span: " + (int)span.TotalSeconds);

just use this: 只需使用此:

//first, get the corrent time
DateTime date = DateTime.Now;
//print the time
Console.WriteLine(date.ToString());
// For en-US culture, for other culture type refer to MSDN link
//output mm/dd/yyyy hh:mm:ss AM/pm

If you realy need the Unix epoch seconds, refer to this thread also check your system clock. 如果您确实需要Unix纪元秒,请参考此线程并检查您的系统时钟。

Hope I helped! 希望我能帮上忙!

EDIT: I changed the link to what you needed 编辑:我将链接更改为您需要的

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

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