将当前时间添加到DateTime? [英] Add the current time to a DateTime?

查看:199
本文介绍了将当前时间添加到DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,表示一个日期,从DropDownList返回。例如,字符串是27.08.2010。现在我想添加当前的时间并将其解析为Datetime ...所以最终它应该是一个DateTime,像例如27.08.2010 15:12:45 。

我该怎么做?现在,我使用DateTime.Now.Hour等把一个字符串放在一起,并把它当作一个DateTime,但这似乎是错误的。



谢谢:

解决方案

  string s =27.08.2010; 
DateTime dt = DateTime.ParseExact(s,dd.MM.yyyy,CultureInfo.InvariantCulture);
DateTime result = dt.Add(DateTime.Now.TimeOfDay);


I have a string which represents a date, its given back from a DropDownList. The string is "27.08.2010" for example. Now I want to add the current time to this and parse it to Datetime ... so in the end it should be a DateTime like 27.08.2010 15:12:45 for example.

How could I do this? Right now, I am putting together a string using DateTime.Now.Hour etc. and make a DateTime out of it, but that seems to be the wrong way.

Thanks :)

解决方案

 string s = "27.08.2010";
 DateTime dt = DateTime.ParseExact(s, "dd.MM.yyyy", CultureInfo.InvariantCulture);
 DateTime result = dt.Add(DateTime.Now.TimeOfDay);

这篇关于将当前时间添加到DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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