【问题标题】:How to Convert Twitter Timestamp to DateTime?如何将 Twitter 时间戳转换为日期时间?
【发布时间】:2011-02-11 23:54:22
【问题描述】:

我已经在谷歌上搜索了一段时间,但我一直找不到我的问题的答案。

我正在使用 C# 和 Windows Presentation Foundation 制作 Twitter 客户端,但我不知道如何将 Twitter 提供的时间戳更改为 DateTime 或 UNIX 时间戳。

我知道使用 Regex 是可能的,但我从未找到解决方案。

是否有一些我不知道的简单方法可以做到这一点?我尝试转换的 Twitter 时间戳格式如下所示:

Fri Feb 11 23:45:15 +0000 2011

有什么想法吗?

【问题讨论】:

    标签: .net wpf c#-4.0 twitter


    【解决方案1】:

    基于sample elsewhere,如何使用ParseExact method

    const string format = "ddd MMM dd HH:mm:ss zzzz yyyy";
    my_date = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
    

    【讨论】:

      【解决方案2】:
      DateTimeOffset timestamp;
      if (DateTimeOffset.TryParseExact(
          "Fri Feb 11 23:45:15 +0000 2011",
          "ddd MMM dd HH:mm:ss K yyyy",
          null,
          DateTimeStyles.None,
          out timestamp))
        ; // use timestamp
      

      假设:

      • 当前文化是正确使用的文化(例如,“Fri”、“Feb”等)
      • 日期、小时、分钟和秒用 0 填充(例如,“Fri Feb 04 02:05:02 +0000 2011”)

      【讨论】:

      • +1 最好包含一种文化,其中包含正确的星期几;-)
      猜你喜欢
      • 2019-04-03
      • 1970-01-01
      • 2017-04-25
      • 2022-01-08
      • 1970-01-01
      • 2020-10-19
      相关资源
      最近更新 更多