如何使用datetime获取前一天的数据?

时间:2020-12-16 12:22:29

I want to set a DateTime property to previous day at 00:00:00. I don't know why DateTime.AddDays(-1) isn't working. Or why DateTime.AddTicks(-1) isn't working. First should this work?

我想在00:00设置一个DateTime属性到前一天。我不知道为什么日期时间。adddays(-1)不起作用。或者为什么DateTime.AddTicks(-1)不起作用。首先应该这个工作吗?

I have 2 objects. Each object have DateTime fields ValidFrom, ValidTo.

我有两个对象。每个对象都有DateTime字段ValidFrom、ValidTo。

EDIT: After coming home from work I tried to get the same behavior as my business objects behave. Below are the code I tried to replicate how it looks at work. Of course this is working at home but not at work. The good thing is I got good answers and +1 on all! =)

编辑:下班回家后,我试图得到和我的业务对象一样的行为。下面是我试图复制的代码。当然这是在家里工作,而不是在工作。好消息是我得到了很好的答案,总共+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

6 个解决方案

#1


52  

DateTime is an immutable struct. When you call AddDays() or AddTicks() it returns a new instance of a DateTime, it does NOT modify the instance you called it on. Make sure you assign the result to a variable or there is no visible change in your code:

DateTime是不可变结构。当您调用AddDays()或addtick()时,它会返回一个DateTime的新实例,它不会修改调用它的实例。请确保您将结果分配给一个变量,否则您的代码中没有可见的更改:

DateTime d1 = DateTime.Now;
d1 = d1.AddDays(-1); // assign back to see the new instance

If you need to reset the time portion of the date to midnight, you will need to use an explicit constructor call:

如果需要将日期的时间部分重置为午夜,则需要使用显式构造函数调用:

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...

#2


13  

the easiest way is this..

最简单的方法是…

DateTime yesterday = DateTime.Now.Date.AddDays(-1);

now if you are trying to use a variable that has already been created, you would do this...

如果你想使用一个已经创建好的变量,你可以这样做……

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

Possibly posting your code will show us what you are doing wrong.

可能发布代码会告诉我们您做错了什么。

#3


6  

Maybe your problem is AddDays doesn't modify the object, it returns an DateTime with the changed days. So it should be:

也许你的问题是AddDays不修改对象,它返回一个日期时间和改变的日期。所以它应该是:

DateTime Yesterday = CurrentDay.AddDays(-1);

#4


4  

Try:

试一试:

<DateTime>.Date.AddDays(-1);

This will strip off the time and give you midnight the previous day.

这将去掉时间,在前一天给你一个午夜。

EDIT: Yes sorry, I meant to put some sort of indication that "DateTime" meant the variable in question. I added brackets around it.

编辑:是的,对不起,我是想说,“DateTime”表示所讨论的变量。我在它周围加了括号。

#5


4  

Have you tried this:

你有试过这样的:

var yesterday = System.DateTime.Now.Date.Subtract(new TimeSpan(1, 0, 0, 0))

#6


0  

// get Lat Day Of Current Month

DateTime newDate= new DateTime();
var LastDay2 =  newDate.AddMonths(1);
var LastDay3 = LastDay2.Day * (-1);
var d5 = LastDay2.AddDays(LastDay3);

#1


52  

DateTime is an immutable struct. When you call AddDays() or AddTicks() it returns a new instance of a DateTime, it does NOT modify the instance you called it on. Make sure you assign the result to a variable or there is no visible change in your code:

DateTime是不可变结构。当您调用AddDays()或addtick()时,它会返回一个DateTime的新实例,它不会修改调用它的实例。请确保您将结果分配给一个变量,否则您的代码中没有可见的更改:

DateTime d1 = DateTime.Now;
d1 = d1.AddDays(-1); // assign back to see the new instance

If you need to reset the time portion of the date to midnight, you will need to use an explicit constructor call:

如果需要将日期的时间部分重置为午夜,则需要使用显式构造函数调用:

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...

#2


13  

the easiest way is this..

最简单的方法是…

DateTime yesterday = DateTime.Now.Date.AddDays(-1);

now if you are trying to use a variable that has already been created, you would do this...

如果你想使用一个已经创建好的变量,你可以这样做……

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

Possibly posting your code will show us what you are doing wrong.

可能发布代码会告诉我们您做错了什么。

#3


6  

Maybe your problem is AddDays doesn't modify the object, it returns an DateTime with the changed days. So it should be:

也许你的问题是AddDays不修改对象,它返回一个日期时间和改变的日期。所以它应该是:

DateTime Yesterday = CurrentDay.AddDays(-1);

#4


4  

Try:

试一试:

<DateTime>.Date.AddDays(-1);

This will strip off the time and give you midnight the previous day.

这将去掉时间,在前一天给你一个午夜。

EDIT: Yes sorry, I meant to put some sort of indication that "DateTime" meant the variable in question. I added brackets around it.

编辑:是的,对不起,我是想说,“DateTime”表示所讨论的变量。我在它周围加了括号。

#5


4  

Have you tried this:

你有试过这样的:

var yesterday = System.DateTime.Now.Date.Subtract(new TimeSpan(1, 0, 0, 0))

#6


0  

// get Lat Day Of Current Month

DateTime newDate= new DateTime();
var LastDay2 =  newDate.AddMonths(1);
var LastDay3 = LastDay2.Day * (-1);
var d5 = LastDay2.AddDays(LastDay3);