减去'DateTime'

时间:2010-05-19 16:27:51

标签: c# datetime

我想从DateTime中减去。例如:

date1 = 13/01/2004 12:20:00
result = Subtract(date1-15);

预期产出:

13/01/2004 12:05:00

我该怎么做?

4 个答案:

答案 0 :(得分:45)

您可以查看AddMinutes方法:

var result = date1.AddMinutes(-15);

答案 1 :(得分:4)

System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

// diff1 gets 185 days, 14 hours, and 47 minutes.
System.TimeSpan diff1 = date2.Subtract(date1);

// date4 gets 4/9/1996 5:55:00 PM.
System.DateTime date4 = date3.Subtract(diff1);

// diff2 gets 55 days 4 hours and 20 minutes.
System.TimeSpan diff2 = date2 - date3;

// date5 gets 4/9/1996 5:55:00 PM.
System.DateTime date5 = date1 - diff2;

答案 2 :(得分:3)

使用带有负值的DateTime.Add(TimeSpan)

或使用DateTime.AddDays()DateTime.AddMinutes()

答案 3 :(得分:3)

你可以试试这个:

TimeSpan t1 = new TimeSpan(16,0,0);
DateTime d2= datetime.Subtract(t1);