Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 685301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:54:31+00:00 2026-05-14T01:54:31+00:00

I want to set a DateTime property to the previous day at time 00:00:00.

  • 0

I want to set a DateTime property to the previous day at time 00:00:00. I’ve tried using DateTime.AddDays(-1) and DateTime.AddTicks(-1) and they aren’t working. Is this the right way to do it?

I have 2 objects. Each object has two DateTime fields: ValidFrom and ValidTo.

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

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:54:31+00:00Added an answer on May 14, 2026 at 1:54 am

    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 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...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this datetime picker and want to set the minDate to today at
i want to set the default value in a sql server 2005 datetime field
I want set Listbox background to transparent but not working Is there any idea?
If I want to set the DataSource property on a BindingSource to an IList<>,
I have a DateTime property on one of my Model classes and want to
I am using Newtonsoft's Json when i serialze a date time property i get
Below is the Property in my viewModel of Datetime,I want to display only date
I have a property called Raised_Time, this property shows the time at which alarm
I need to set the day of year for a DateTime, I am getting
I want set interfaceOrientation in one UIView as LandscapeLeft and Portrait in other UIView.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.