在C#中将双精度值转换为DateTime [英] convert double value into DateTime in c#

查看:193
本文介绍了在C#中将双精度值转换为DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个双精度值

例如:

I have a double value
Ex:

double currentDate = 20161011;  

无法将此双精度值转换为日期格式,如 10/11/2016

Unable to convert this double value into Date format like "10/11/2016"

我尝试使用

 DateTime newDate = DateTime.FromOADate(currentDate);

但通过例外情况


不是合法的OleAut日期。

Not a legal OleAut date.


推荐答案

您可以将double

You could convert the double to a string and then call ParseExact specifying the format.

DateTime newDate = DateTime.ParseExact(currentDate.ToString(), "yyyyMMdd", null);

但实际上您应该避免这种情况,并将日期从一开始就存储在DateTime变量中。

but really you should try to avoid this and store dates in DateTime variables from the start.

这篇关于在C#中将双精度值转换为DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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