如何使用 C#/.NET 将日期时间转换为时间戳(忽略当前时区)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9814060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 10:43:30  来源:igfitidea点击:

How to convert datetime to timestamp using C#/.NET (ignoring current timezone)

c#asp.nettimezone

提问by User

How do I convert datetime to timestamp using C# .NET (ignoring the current timezone)?

如何使用 C# .NET(忽略当前时区)将日期时间转换为时间戳?

I am using the below code:

我正在使用以下代码:

private long ConvertToTimestamp(DateTime value)
{
    long epoch = (value.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    return epoch;
}

But it returns the timestamp value according to the current time zone & and I need the result without using the current timezone.

但它根据当前时区 & 返回时间戳值,我需要不使用当前时区的结果。

回答by Jensen

I'm not exactly sure what it is that you want. Do you want a TimeStamp? Then you can do something simple like:

我不确定你想要什么。你想要一个时间戳吗?然后你可以做一些简单的事情,比如:

TimeStamp ts = TimeStamp.FromTicks(value.ToUniversalTime().Ticks);

Since you named a variable epoch, do you want the Unix time equivalent of your date?

由于您命名了一个变量 epoch,您是否想要与您的日期等效的 Unix 时间?

DateTime unixStart = DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc);
long epoch = (long)Math.Floor((value.ToUniversalTime() - unixStart).TotalSeconds);

回答by Jon Skeet

At the moment you're calling ToUniversalTime()- just get rid of that:

在您打电话的那一刻ToUniversalTime()- 只需摆脱它:

private long ConvertToTimestamp(DateTime value)
{
    long epoch = (value.Ticks - 621355968000000000) / 10000000;
    return epoch;
}

Alternatively, and rather more readably IMO:

或者,更易读的 IMO:

private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
...

private static long ConvertToTimestamp(DateTime value)
{
    TimeSpan elapsedTime = value - Epoch;
    return (long) elapsedTime.TotalSeconds;
}

EDIT: As noted in the comments, the Kindof the DateTimeyou pass in isn't taken into account when you perform subtraction. You should really pass in a value with a Kindof Utcfor this to work. Unfortunately, DateTimeis a bit broken in this respect - see my blog post(a rant about DateTime) for more details.

编辑:如评论中所述KindDateTime执行减法时不考虑传入的 。你真的应该传入一个带有Kindof的值Utc才能工作。不幸的是,DateTime在这方面有点破 -有关更多详细信息,请参阅我的博客文章(关于 的咆哮DateTime)。

You might want to use my Noda Timedate/time API instead which makes everything rather clearer, IMO.

你可能想使用我的Noda Time日期/时间 API,这让一切都变得更加清晰,IMO。

回答by Bronumski

JonSkeet has a good answer but as an alternative if you wanted to keep the result more portable you could convert the date into an ISO 8601format which could then be read into most other frameworks but this may fall outside your requirements.

JonSkeet 有一个很好的答案,但作为替代,如果您想让结果更便携,您可以将日期转换为ISO 8601格式,然后可以将其读入大多数其他框架,但这可能超出您的要求。

value.ToUniversalTime().ToString("O");

回答by Vivekveer

Find timestamp from DateTime:

从 DateTime 中查找时间戳:

private long ConvertToTimestamp(DateTime value)
{
    TimeZoneInfo NYTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    DateTime NyTime = TimeZoneInfo.ConvertTime(value, NYTimeZone);
    TimeZone localZone = TimeZone.CurrentTimeZone;
    System.Globalization.DaylightTime dst = localZone.GetDaylightChanges(NyTime.Year);
    NyTime = NyTime.AddHours(-1);
    DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
    TimeSpan span = (NyTime - epoch);
    return (long)Convert.ToDouble(span.TotalSeconds);
}