如何将代码值转换为日期时间?
How to convert ticker value to datetime?
我有以下值:
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));
这给了我 worng 格式的日期,我明白了:
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
在这部分你要添加秒数:
public static DateTime DateFromTimestamp(long timestamp)
{
return _unixEpoch.AddSeconds(timestamp);
}
如果要传递刻度,需要添加刻度:
public static DateTime DateFromTimestamp(long timestamp)
{
return _unixEpoch.AddTicks(timestamp);
}
但您必须确保您的报价与 .NET 报价 的含义相同。一个 .NET tick 是 100 纳秒。如果您的刻度是不同的单位,您必须先将其转换为 .NET 刻度。
我使用了这个扩展程序
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
。
你的 2 个样本相隔 2 年,取这些刻度的差异并除以 2、365、24 和 3600 得到 1000,所以它们是毫秒。
快速检查发现它们确实基于 1-1-1970,所以
//return _unixEpoch.AddSeconds(timestamp);
return _unixEpoch.AddMilliSeconds(timestamp);
我有以下值:
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));
这给了我 worng 格式的日期,我明白了:
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
在这部分你要添加秒数:
public static DateTime DateFromTimestamp(long timestamp)
{
return _unixEpoch.AddSeconds(timestamp);
}
如果要传递刻度,需要添加刻度:
public static DateTime DateFromTimestamp(long timestamp)
{
return _unixEpoch.AddTicks(timestamp);
}
但您必须确保您的报价与 .NET 报价 的含义相同。一个 .NET tick 是 100 纳秒。如果您的刻度是不同的单位,您必须先将其转换为 .NET 刻度。
我使用了这个扩展程序
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
。
你的 2 个样本相隔 2 年,取这些刻度的差异并除以 2、365、24 和 3600 得到 1000,所以它们是毫秒。
快速检查发现它们确实基于 1-1-1970,所以
//return _unixEpoch.AddSeconds(timestamp);
return _unixEpoch.AddMilliSeconds(timestamp);