【问题标题】:How to convert ticker value to datetime?如何将股票代码值转换为日期时间?
【发布时间】:2016-12-13 12:17:47
【问题描述】:

我有以下价值观:

1465509600000

1402437600000

我尝试了以下方法:

尝试1:

    public long? CommitmentStartDate { get; set; }

    public long? CommitmentEndDate { get; set; }

    public DateTime? CommitmentStartDateDate => new DateTime( (CommitmentStartDate != null ? (long)CommitmentStartDate: Convert.ToInt64(DateTime.MinValue)) );

    public DateTime? CommitmentEndDateDate => new DateTime(CommitmentEndDate != null ? (long)CommitmentEndDate: Convert.ToInt64(DateTime.MinValue));

这给了我磨损格式的日期,我明白了:

0001-01-02 16:42:30

0001-01-02 14:57:23

尝试2:

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

    public static DateTime DateFromTimestamp(long timestamp)
    {
        return _unixEpoch.AddSeconds(timestamp);
    }

    public long? CommitmentStartDate { get; set; }

    public long? CommitmentEndDate { get; set; }

    public DateTime? CommitmentStartDateDate =>  DateFromTimestamp(CommitmentStartDate != null ? (long)CommitmentStartDate: Convert.ToInt64(DateTime.MinValue));

    public DateTime? CommitmentEndDateDate => DateFromTimestamp(CommitmentEndDate != null ? (long)CommitmentEndDate: Convert.ToInt64(DateTime.MinValue));

这给了我一个 argumentOutOfRange 异常。

我该怎么做?

编辑:

预期值:

2014-06-11

2016-06-10

编辑 2:

来自日期的刻度

1402437600000 ----> 2014-06-11

1465509600000 ----> 2016-06-10

【问题讨论】:

  • 绒毛太多了。请仅发布 1 个或更多示例及其预期日期值。
  • @François Wahl 这就是我在尝试转换它时使用的线程,如果您在投票关闭之前用代码通读整个问题会很好
  • DateTimes 没有与之关联的特定格式。
  • 你能具体说明“刻度”代表什么吗?

标签: c# datetime ticker


【解决方案1】:

您的 2 个样本相隔 2 年,将这些刻度的差除以 2、365、24 和 3600 得到 1000,因此它们是毫秒。

快速检查显示它们确实基于 1-1-1970 所以

//return _unixEpoch.AddSeconds(timestamp);
return _unixEpoch.AddMilliSeconds(timestamp);

【讨论】:

  • 无论如何,使用示例“ticks” OP 显示,结果将是2016-06-09 22:00:002014-06-10 22:00:00
  • 是的,似乎涉及时区偏移,我得到的基数是 1-1-70, 02:00。很容易补偿,但为您的传入数据制定规范总是要好得多。
【解决方案2】:

在这一部分中,您将添加秒数:

public static DateTime DateFromTimestamp(long timestamp)
{
    return _unixEpoch.AddSeconds(timestamp);
}

如果要传递刻度,则需要添加刻度:

public static DateTime DateFromTimestamp(long timestamp)
{
    return _unixEpoch.AddTicks(timestamp);
}

但您必须确保您的刻度与 .NET 刻度的含义相同。一个 .NET 滴答是 100 纳秒。如果您的刻度是不同的单位,您必须先将其转换为 .NET 刻度。

【讨论】:

    【解决方案3】:

    我用过这个扩展

    public static DateTime FromUnixTicks(this double ms)
    {
        DateTime d1 = new DateTime(1970, 1, 1);
        return d1.AddMilliseconds(ms).ToLocalTime();
    }
    

    和示例:

    1465509600000.0.FromUnixTicks()
    

    然后转换回来

    public static double ToUnixTicks(this DateTime dt)
    {
        DateTime d1 = new DateTime(1970, 1, 1);
        DateTime d2 = dt.ToUniversalTime();
        TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
        return ts.TotalMilliseconds;
    }
    

    我在这里需要double,但我猜你可以使用long

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多