有没有一种方法可以在C#中快速/轻松地解析Unix时间?我是一个全新的语言,所以如果这是一个令人痛苦的显而易见的问题,我道歉。即我有一个格式为秒的字符串,从Epoch.milliseconds开始。在C#中有没有与Java的SimpleDateFormat等价物?
发布于 2011-07-21 00:07:37
这是C#中人们做的一件很常见的事情,但是还没有相应的库。
我创建这个迷你库https://gist.github.com/1095252是为了让我的生活(我希望你的也是)变得更容易。
发布于 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);
发布于 2009-11-04 22:50:11
var date = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
.AddSeconds(
double.Parse(yourString, CultureInfo.InvariantCulture));
https://stackoverflow.com/questions/1674215
复制相似问题