我想在00:00:00将DateTime属性设置为前一天。我不知道为什么DateTime.AddDays(-1)不起作用。或者为什么DateTime.AddTicks(-1)不起作用。 首先应该这样做吗?
我有2个物体。每个对象都有DateTime字段ValidFrom,ValidTo。
编辑:下班回家后,我试图获得与业务对象行为相同的行为。下面是我试图复制它看起来如何工作的代码。当然这是在家工作但不工作。好消息是我得到了很好的答案,并且+1! =)
public class RuleValue
{
public DateTime ValidFrom, ValidTo;
public RuleValue(DateTime _validFrom, DateTime _validTo)
{
ValidFrom = _validFrom;
ValidTo = _validTo;
}
// oldObject.ValidFrom = 1900-01-01
// oldObject.ValidTo = 9999-12-31
// newObject.ValidFrom = 2010-03-22
// newObject.ValidTo = 9999-12-31
public void ChangeOldDate(RuleValue oldObject, RuleValue newObject)
{
/*
* 1: When first object (oldObject) have ValidTo set to SQL-server maxdate (9999-12-12 23:59:59 etc)
* I want to allow for a new object to be created
* 2: oldObject timespan ValidFrom-ValidTo should not be overlapping with newObjects timespan(i have checks for that)
* 3: oldObject.ValidTo should be newObject.ValidFrom - one day/or one tick
*/
if (oldObject.ValidTo == DateTime.MaxValue)
{
oldObject.ValidTo = newObject.ValidFrom.AddTicks(-1); //now works
}
}
}
class Program
{
static void Main(string[] args)
{
RuleValue rv1 = new RuleValue(DateTime.Parse("1900-01-01"), DateTime.MaxValue);
RuleValue rv2 = new RuleValue(DateTime.Parse("2010-03-22"), DateTime.MaxValue);
Console.WriteLine("First: ");
Console.WriteLine("Old = " + rv1.ValidFrom + " - " + rv1.ValidTo);
Console.WriteLine("New = " + rv2.ValidFrom + " - " + rv2.ValidTo);
rv1.ChangeOldDate(rv1, rv2);
Console.WriteLine("After: ");
Console.WriteLine("Old = " + rv1.ValidFrom + " - " + rv1.ValidTo);
Console.WriteLine("New = " + rv2.ValidFrom + " - " + rv2.ValidTo);
Console.ReadKey();
}
}
//Output:
//First:
//Old = 1900-01-01 00:00:00 - 9999-12-31 23:59:59
//New = 2010-03-22 00:00:00 - 9999-12-31 23:59:59
//After:
//Old = 1900-01-01 00:00:00 - 2010-03-21 23:59:59
//New = 2010-03-22 00:00:00 - 9999-12-31 23:59:59
// ALL CORRECT! :D
答案 0 :(得分:55)
DateTime是一个不可变的结构。当你调用AddDays()
或AddTicks()
时,它会返回一个新的DateTime实例,它不会修改你调用它的实例。确保将结果分配给变量,或者代码中没有明显的变化:
DateTime d1 = DateTime.Now;
d1 = d1.AddDays(-1); // assign back to see the new instance
如果您需要将日期的时间部分重置为午夜,则需要使用显式构造函数调用:
DateTime d1 = DateTime.Now;
DateTime d2 = new DateTime( d1.Year, d1.Month, d1.Day, 0, 0, 0 );
DateTime d3 = d1.Date; // a simpler alternative to the above...
答案 1 :(得分:13)
最简单的方法就是这个..
DateTime yesterday = DateTime.Now.Date.AddDays(-1);
现在,如果您尝试使用已创建的变量,则可以执行此操作...
DateTime yesterday = DateTime.Now; // will give you today's date
yesterday = yesterday.Date.AddDays(-1); // will give you yesterday's date at 12:00 AM
可能发布您的代码会告诉我们您做错了什么。
答案 2 :(得分:6)
也许您的问题是AddDays
不修改对象,它会返回更改日期的DateTime。所以它应该是:
DateTime Yesterday = CurrentDay.AddDays(-1);
答案 3 :(得分:4)
尝试:
<DateTime>.Date.AddDays(-1);
这将剥夺时间并让你在前一天午夜。
编辑: 对不起,我的意思是说“DateTime”意味着有问题的变量。我在它周围添加了括号。
答案 4 :(得分:4)
你试过这个:
var yesterday = System.DateTime.Now.Date.Subtract(new TimeSpan(1, 0, 0, 0))
答案 5 :(得分:0)
DateTime newDate= new DateTime();
var LastDay2 = newDate.AddMonths(1);
var LastDay3 = LastDay2.Day * (-1);
var d5 = LastDay2.AddDays(LastDay3);