Unix时间戳到.NET日期时间 [英] Unix timestamp to .net DateTime

查看:267
本文介绍了Unix时间戳到.NET日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做一个明显的错误,但我不明白。

我导入存储在MySQL数据库中的日期(它是由前pressionEngine CMS存储)。这是一个Unix时间戳,即秒起1/1/1970 00:00。

所以我在做这样的事情:

  DateTime的日期时间=新的System.DateTime(1970年,1,1,0,0,0,0);
日期时间= dateTime.AddSeconds(订购日期/ *的int从数据库* /读取);
 

不幸的是我没有得到正确的结果。下面是一个例子:

1258598728 (这是一个订单日期)

从数据库读取值

贝宝发送的电子邮件在 2009年11月18日建立的顺序12时45分20秒的PST

PHP Web站点,它读取数据库此值,并知道如何正确地将其显示为显示该日期 2009-11-18下午3时45 (这似乎是正确的,因为我主持在东海岸的服务器)

我的code以上给出的 11/19/2009上午2点45分28秒! (UTC这给11/18/2009下午9:45东部时间,即6个小时的期望是什么差)

如果我使用的DateTimeOffset采取把正确的时区的护理得到同样的结果。

知不知道我做错了什么?

解决方案

试试这个:

  DateTime的划时代=新的日期时间(1970,1,1,0,0,0,0,DateTimeKind.Utc);
日期时间指明MyDate = epoch.AddSeconds(1258598728).toLocalTime();
 

I must be doing an obvious mistake but I can't figure it out.

I am importing a date stored in a mysql database (it is stored by the ExpressionEngine CMS). It is a unix timestamp, i.e. seconds since 1/1/1970 00:00.

So I'm doing something like this:

DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(orderdate /* int read from the database */);

Unfortunately I don't get the right result. Here is an example:

Value read from the DB: 1258598728 (this is an order date)

Paypal sent an email establishing the order at Nov 18, 2009 12:45:20 PST

The php web site that reads this value in the DB and knows how to display this date correctly displays it as 2009-11-18 03:45 PM (which seems correct since I’m hosted at a server on the east coast)

My code above gives 11/19/2009 2:45:28 AM !! (UTC which gives 11/18/2009 9:45 PM east time, i.e. 6 hours difference with what is expected)

I get the same result if using DateTimeOffset taking care of putting the right timezone.

Any idea what I'm doing wrong?

解决方案

Try this:

DateTime epoch = new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);
DateTime myDate = epoch.AddSeconds(1258598728).toLocalTime();

这篇关于Unix时间戳到.NET日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆