从毫秒到DateTime格式的转换 [英] Conversion from milliseconds to DateTime format

查看:89
本文介绍了从毫秒到DateTime格式的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,它是表示形式的:

I got a string which is representend like this :

string startdatetime = "13988110600000"

我想要做的就是将此字符串(毫秒)转换为DateTime变量。这就是我正在做的:

What I want to do is to convert this string (which are milliseconds) to a DateTime variable. This is what I'm doing :

double ticks = double.Parse(startdatetime);
TimeSpan time = TimeSpan.FromMilliseconds(ticks);
DateTime startdate = new DateTime(time.Ticks);

结果几乎不错:我有一个奇怪的约会,但时间还可以(30/04 / 0045 18:00:00)。

The result is almost good : I've got a weird date but time is okay (30/04/0045 18:00:00).

有什么理由吗?

推荐答案

DateTime 初始化为 0001-01-01 00:00:00 ,然后添加您的 TimeSpan ,这似乎是45年。

DateTime in .NET is initialized to 0001-01-01 00:00:00 and then you add your TimeSpan, which seems to be 45 Years.

它是(毫秒)秒时间定义的常见开始于 1970-01-01 00:00:00 ,因此以下内容可能会给您带来预期的结果:

It is common for such (milli)-second time definitions to start at 1970-01-01 00:00:00, so maybe the following gives you the expected result:

double ticks = double.Parse(startdatetime);
TimeSpan time = TimeSpan.FromMilliseconds(ticks);
DateTime startdate = new DateTime(1970, 1, 1) + time;

或简单地

var date = (new DateTime(1970, 1, 1)).AddMilliseconds(double.Parse(startdatetime));

这篇关于从毫秒到DateTime格式的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆