如何将对象转换为日期时间. [英] How to convert object into datetime.

查看:103
本文介绍了如何将对象转换为日期时间.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




将对象转换为DateTime时遇到问题.我遇到以下问题:

getStartDateOfEvent是我的存储过程.

业务逻辑:

对象存在= events.getStartDateOfEvent(EventID,OrganizerID);
DateTime dt1 =(DateTime)存在;

我在进行调试时得到的值是:7/9/2012 12:34:45 PM
并在dt1中获得值1/1/0001 12:00:00 AM

那么确切的解决方案是什么.我已经尝试了很多方法,但没有得到任何解决方案.

Hi,


I am getting an issue while converting an object into DateTime. I am getting the following problem :

The getStartDateOfEvent is my stored procedure.

Business Logic :

Object exist = events.getStartDateOfEvent(EventID, OrganizerID);
DateTime dt1 = (DateTime)exist;

The value i get while debugging in exist is : 7/9/2012 12:34:45 PM
and in dt1 i get the value 1/1/0001 12:00:00 AM

So what will be the exact solution. I have tried in many ways but not getting any solution.

推荐答案

做到这一点的方法是

The way to do it would be

string MyString = exist.tostring();
DateTime dt1 = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",
                                  null);



您可能还会发现此有用

轻松将字符串转换为DateTime,将DateTime转换为字符串和格式 [ ^ ]



you may also find this useful

Easy String to DateTime, DateTime to String and Formatting[^]


如果内部接收到的对象的数据类型为datetime,则将通过显式分配将其转换为Datetime.

看起来,在内部它不是日期时间类型.您需要对其进行显式转换.
此处:
If the object recieved was of datatype datetime internally then it will be converted into Datetime by explicit assignment.

Looks like, internally it is not of datetime type. You need to convert it explicitly.
Here:
Object exist = events.getStartDateOfEvent(EventID, OrganizerID);
DateTime dt1 = Convert.ToDateTime(exist);


这篇关于如何将对象转换为日期时间.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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