有没有办法在 C# 中快速/轻松地解析 Unix 时间?我是这门语言的新手,所以如果这是一个非常明显的问题,我深表歉意。IE 我有一个格式为 [自纪元以来的秒数].[毫秒] 的字符串。C# 中是否有与 Java 的 SimpleDateFormat 等价的东西?
问问题
16937 次
9 回答
43
最简单的方法可能是使用类似的东西:
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0,
DateTimeKind.Utc);
...
public static DateTime UnixTimeToDateTime(string text)
{
double seconds = double.Parse(text, CultureInfo.InvariantCulture);
return Epoch.AddSeconds(seconds);
}
需要注意的三点:
- 如果你的字符串肯定是“xy”而不是“x,y”的形式,你应该使用如上所示的不变文化,以确保“。” 被解析为小数点
- 您应该在构造函数中指定 UTC
DateTime
以确保它认为它不是本地时间。 - 如果您使用的是 .NET 3.5 或更高版本,您可能需要考虑使用
DateTimeOffset
而不是DateTime
.
于 2009-11-04T14:50:52.473 回答
6
这是 C# 中人们很常见的事情,但没有用于此的库。
我创建了这个迷你库https://gist.github.com/1095252让我的生活(我也希望你的)更轻松。
于 2011-07-20T16:07:36.510 回答
5
// 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-04T14:49:47.173 回答
4
var date = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
.AddSeconds(
double.Parse(yourString, CultureInfo.InvariantCulture));
于 2009-11-04T14:50:11.337 回答
2
我意识到这是一个相当古老的问题,但我想我会发布我的解决方案,它使用了 Nodatime 的Instant 类,该类有一个专门用于此的方法。
Instant.FromSecondsSinceUnixEpoch(longSecondsSinceEpoch).ToDateTimeUtc();
我完全明白,对于某些人来说,加入 Nodatime 可能会很沉重。对于依赖膨胀不是主要问题的项目,我宁愿依赖维护的库解决方案,而不是必须维护自己的解决方案。
于 2015-07-09T12:46:45.383 回答
2
从 .NET 4.6 开始,您可以使用DateTimeOffset.FromUnixTimeSeconds()
and DateTimeOffset.FromUnixTimeMilliseconds()
:
long unixTime = 1600000000;
DateTimeOffset dto = DateTimeOffset.FromUnixTimeSeconds(unixTime);
DateTime dt = dto.DateTime;
于 2020-09-28T12:53:08.290 回答
1
// 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(numSeconds);
// Then add the number of milliseconds
dateTime = dateTime.Add(TimeSpan.FromMilliseconds(numMilliseconds));
于 2009-11-04T14:49:21.620 回答
1
这是来自Stefan Henke 的博客文章:
private string conv_Timestamp2Date (int Timestamp)
{
// calculate from Unix epoch
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
// add seconds to timestamp
dateTime = dateTime.AddSeconds(Timestamp);
string Date = dateTime.ToShortDateString() +", "+ dateTime.ToShortTimeString();
return Date;
}
于 2009-11-04T14:50:24.160 回答
0
这是一个方便的扩展方法
public static DateTime UnixTime(this string timestamp)
{
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return dateTime.AddSeconds(int.Parse(timestamp));
}
于 2014-10-10T15:16:56.177 回答