简体   繁体   English

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

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

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. 但是它根据当前时区返回时间戳值,并且我需要结果而不使用当前时区。

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 Kind of the DateTime you pass in isn't taken into account when you perform subtraction. 编辑:如评论中所述,执行减法时不会考虑传入的DateTime Kind You should really pass in a value with a Kind of Utc for this to work. 你应该用Kind Utc传递一个值Utc实现这一点。 Unfortunately, DateTime is a bit broken in this respect - see my blog post (a rant about DateTime ) for more details. 不幸的是, DateTime在这方面有点破碎 - 请参阅我的博客文章 (关于DateTime的咆哮)以获取更多详细信息。

You might want to use my Noda Time date/time API instead which makes everything rather clearer, IMO. 您可能希望使用我的Noda Time日期/时间API,这使得一切都相当清晰,IMO。

I'm not exactly sure what it is that you want. 我不确定你想要的是什么。 Do you want a TimeStamp? 你想要一个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? 既然你命名了一个变量纪元,你想要Unix时间等于你的日期吗?

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

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);
}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM