如何在日期时间和整数之间进行减法? [英] how to make a substraction between a datetime and integer?

查看:167
本文介绍了如何在日期时间和整数之间进行减法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hallo Guy,我需要您的帮助.

我想像这样计算日期和持续时间之间的差.

Hallo Guy please i need your help.

I want to calculate a difference between a date and a duration like this.

DateTime date_Of_End = new DateTime(2012, 6, 02);
int duration  = 30;

DateTime date_Of_Bigin = date_Of_End - duration ;



请我失败了我该怎么办?



Please i get a failure how can i do? have somebody an idea??

推荐答案



你可以试试看.

要添加 :- date_Of_End .AddDays(30)

要减去 :- date_Of_End .Subtract(30)

快乐编码
:)
Hi,

You can try this.

To Add :- date_Of_End .AddDays(30)

To Subtract :- date_Of_End .Subtract(30)

Happy Coding
:)


如果需要从Date中扣除30 days,那么我认为DateTime 类的AddDays 方法具有负数可以用作<DateTime 的c4>方法接受一个DateTime 对象,而不是持续时间.
If it is required to deduct 30 days from the Date, then I think the AddDays method of DateTime class with negative number can be used as the Substract method of DateTime accepts a DateTime object and not a duration.
DateTime date_Of_End = new DateTime(2012, 6, 02);
DateTime date_Of_Bigin = date_Of_End.AddDays(-30);
Console.WriteLine (date_Of_Bigin.ToString("dd/MM/yyyy",
	System.Globalization.CultureInfo.InvariantCulture));
//Output
//03/05/2012


除了VJ Reddy解释的内容外,我通常更喜欢使用 TimeSpan [ ^ ].当您想进行更精确的计算时,它为您提供了灵活性.例如. 30天10小时45分钟:)

Adding to what VJ Reddy explained, I usually prefer to use TimeSpan[^]. It gives you flexibility when you want to have more accurate calculations. For eg. 30 days, 10 hours and 45 mins :)

DateTime date_Of_End = new DateTime(2012, 6, 02);

int duration  = 30;

TimeSpan tsDay = newTimeSpan(duration, 0, 0, 0);
 
DateTime date_Of_Bigin =  date_Of_End.Subtract(tsDay);


这篇关于如何在日期时间和整数之间进行减法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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