首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在C#中解析unix时间

在C#中解析unix时间
EN

Stack Overflow用户
提问于 2009-11-04 22:46:50
回答 8查看 16.8K关注 0票数 21

有没有一种方法可以在C#中快速/轻松地解析Unix时间?我是一个全新的语言,所以如果这是一个令人痛苦的显而易见的问题,我道歉。即我有一个格式为秒的字符串,从Epoch.milliseconds开始。在C#中有没有与Java的SimpleDateFormat等价物?

EN

回答 8

Stack Overflow用户

发布于 2011-07-21 00:07:37

这是C#中人们做的一件很常见的事情,但是还没有相应的库。

我创建这个迷你库https://gist.github.com/1095252是为了让我的生活(我希望你的也是)变得更容易。

票数 6
EN

Stack Overflow用户

发布于 2009-11-04 22:49:47

// This is an example of a UNIX timestamp for the date/time 11-04-2005 09:25.
double timestamp = 1113211532;

// First make a System.DateTime equivalent to the UNIX Epoch.
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

// Add the number of seconds in UNIX timestamp to be converted.
dateTime = dateTime.AddSeconds(timestamp);

// The dateTime now contains the right date/time so to format the string,
// use the standard formatting methods of the DateTime object.
string printDate = dateTime.ToShortDateString() +" "+ dateTime.ToShortTimeString();

// Print the date and time
System.Console.WriteLine(printDate);

来源:http://www.codeproject.com/KB/cs/timestamp.aspx

票数 5
EN

Stack Overflow用户

发布于 2009-11-04 22:50:11

var date = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
               .AddSeconds(
               double.Parse(yourString, CultureInfo.InvariantCulture));
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1674215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档