将字符串值添加到日期时间 [英] Add string value into datetime
本文介绍了将字符串值添加到日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我的字符串是like("+ 5"或"-5").我想将其添加到日期时间"中.
即时间是01:20:20 PM和string ="+ 5",那么输出将是:06:20:20 PM
时间是01:20:20 PM和string =-5",那么输出将是:08:20:20 AM
建议我为此解决方案.
谢谢
Hi All,
I have string is like("+5" or "-5"). and i want to add it into Hours of Datetime.
i.e.Time is 01:20:20 PM and string="+5" then output will be : 06:20:20 PM
Time is 01:20:20 PM and string="-5" then output will be : 08:20:20 AM
Suggest me solution for this one.
Thanks
推荐答案
使用TimeSpan
类.
此处 [
Use theTimeSpan
class.
Here[^] is more information onTimeSpan.Add
method.
DateTime dt = DateTime.Now;
String s = "+5"; // or "-5" works with both + and -
dt = dt.AddHours(Convert.ToInt32(s));
尝试一下:
Try this:
DateTime clientTime = System.Date.Now;
string hrsToCompute = "+5";
int final = 0;
if(hrsToCompute.IndexOf("+"))
{
final = Convert.ToInt32(hrsToCompute.Substring(1));
clientTime.AddHours(hours);
}
//same logic here for negative.....
问候,
爱德华
Regards,
Eduard
这篇关于将字符串值添加到日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文