• <tfoot id='UDJ6E'></tfoot>

    • <bdo id='UDJ6E'></bdo><ul id='UDJ6E'></ul>
    <legend id='UDJ6E'><style id='UDJ6E'><dir id='UDJ6E'><q id='UDJ6E'></q></dir></style></legend>

    <i id='UDJ6E'><tr id='UDJ6E'><dt id='UDJ6E'><q id='UDJ6E'><span id='UDJ6E'><b id='UDJ6E'><form id='UDJ6E'><ins id='UDJ6E'></ins><ul id='UDJ6E'></ul><sub id='UDJ6E'></sub></form><legend id='UDJ6E'></legend><bdo id='UDJ6E'><pre id='UDJ6E'><center id='UDJ6E'></center></pre></bdo></b><th id='UDJ6E'></th></span></q></dt></tr></i><div id='UDJ6E'><tfoot id='UDJ6E'></tfoot><dl id='UDJ6E'><fieldset id='UDJ6E'></fieldset></dl></div>

        <small id='UDJ6E'></small><noframes id='UDJ6E'>

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

        时间:2023-10-07

          <small id='vBCY2'></small><noframes id='vBCY2'>

          <tfoot id='vBCY2'></tfoot>
          • <i id='vBCY2'><tr id='vBCY2'><dt id='vBCY2'><q id='vBCY2'><span id='vBCY2'><b id='vBCY2'><form id='vBCY2'><ins id='vBCY2'></ins><ul id='vBCY2'></ul><sub id='vBCY2'></sub></form><legend id='vBCY2'></legend><bdo id='vBCY2'><pre id='vBCY2'><center id='vBCY2'></center></pre></bdo></b><th id='vBCY2'></th></span></q></dt></tr></i><div id='vBCY2'><tfoot id='vBCY2'></tfoot><dl id='vBCY2'><fieldset id='vBCY2'></fieldset></dl></div>

              <tbody id='vBCY2'></tbody>
              <bdo id='vBCY2'></bdo><ul id='vBCY2'></ul>
            • <legend id='vBCY2'><style id='vBCY2'><dir id='vBCY2'><q id='vBCY2'></q></dir></style></legend>

                1. 本文介绍了如何使用 C#/.NET 将日期时间转换为时间戳(忽略当前时区)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  我正在使用以下代码:

                  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.

                  推荐答案

                  此时你正在调用 ToUniversalTime() - 摆脱它:

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

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

                  另外,更易读的 IMO:

                  Alternatively, and rather more readably 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;
                  }
                  

                  如评论中所述,执行减法时不考虑您传入的 DateTimeKind.您应该真正使用 UtcKind 传递一个值,以使其正常工作.不幸的是,DateTime 在这方面有点破旧 - 请参阅 我的博文(关于DateTime的咆哮)了解更多详情.

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

                  您可能想要使用我的 Noda Time 日期/时间 API,这会使一切变得更加清晰,IMO.

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

                  这篇关于如何使用 C#/.NET 将日期时间转换为时间戳(忽略当前时区)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以在 ASP.NET 中获取浏览器时区还是必须依靠 JS 操作来检索信息? 下一篇:防止对 DateTime 值的反序列化进行时区转换

                  相关文章

                  <legend id='ZATtS'><style id='ZATtS'><dir id='ZATtS'><q id='ZATtS'></q></dir></style></legend>
                2. <tfoot id='ZATtS'></tfoot>

                        <bdo id='ZATtS'></bdo><ul id='ZATtS'></ul>

                      <small id='ZATtS'></small><noframes id='ZATtS'>

                      <i id='ZATtS'><tr id='ZATtS'><dt id='ZATtS'><q id='ZATtS'><span id='ZATtS'><b id='ZATtS'><form id='ZATtS'><ins id='ZATtS'></ins><ul id='ZATtS'></ul><sub id='ZATtS'></sub></form><legend id='ZATtS'></legend><bdo id='ZATtS'><pre id='ZATtS'><center id='ZATtS'></center></pre></bdo></b><th id='ZATtS'></th></span></q></dt></tr></i><div id='ZATtS'><tfoot id='ZATtS'></tfoot><dl id='ZATtS'><fieldset id='ZATtS'></fieldset></dl></div>