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

  • Home
  • SEARCH
  • 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 8561105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:25:39+00:00 2026-06-11T16:25:39+00:00

I read on this question and answer, where you must create a new DateTime

  • 0

I read on this question and answer, where you must create a new DateTime object if you need to modify (or shift) the time of the object, because DateTime is an immutable object type.

Why DateTime.AddHours doesn't seem to work?

That’s fine if the variable is being declared and a value is being set in the same lexical scope. Except I’m declaring and setting the value in different lexical scope. Normally I’d do “DateTime from_instant = null;” so it at least has a value, so I can use it after it’s set. However, with DateTime, once you set the value, it’s immutable. So how can I adjust the value of my object by doing “from_instant.AddHours(-10);” for example, outside the lexical scope it was declared in?

In my case below, whenever I reset “from_instant”, it doesn’t change. I’d like to be able to change it. How do you typically reset the value since it’s immutable?

    DateTime from_instant = DateTime.Now;

    bool set_scan_start_instant_to_last_scan_instant = Convert.ToBoolean(GetConfig("set_scan_start_instant_to_last_scan_instant"));

    if (set_scan_start_instant_to_last_scan_instant)
    {
        from_instant = GetScanTimeFromFile(@".\last_scan_instant.txt");
        from_instant = DateTime.Now;
    }
    else
    {
        if (!string.IsNullOrEmpty(scan_interval_minutes))
        {
            from_instant = DateTime.Now.AddMinutes(Convert.ToInt32(scan_interval_minutes));
        }
        else if (!string.IsNullOrEmpty(scan_interval_hours))
        {
            from_instant = DateTime.Now.AddHours(Convert.ToInt32(scan_interval_hours));
        }
        else if (!string.IsNullOrEmpty(scan_interval_days))
        {
            from_instant = DateTime.Now.AddDays(Convert.ToInt32(scan_interval_days));
        }
    }

    DateTime to_instant = DateTime.Now;
    WriteScanTimeToFile(@".\last_scan_instant.txt", to_instant.ToString());

    Console.WriteLine("from_instant: " + from_instant.ToString());  
    Console.WriteLine("to_instant: " + to_instant.ToString());        
  • 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-06-11T16:25:40+00:00Added an answer on June 11, 2026 at 4:25 pm

    The problem is you keep calling methods on DateTime and ignoring the return values. Don’t do that.

    Change this:

    from_instant.AddHours(Convert.ToInt32(scan_interval_hours));
    

    to

    from_instance = from_instant.AddHours(Convert.ToInt32(scan_interval_hours));
    

    Or:

    fromInstance += TimeSpan.FromHours(Convert.ToInt32(scan_interval_hours));
    

    (etc). It’s not clear why you appear to think you can’t do this. It’s also not clear what you expect this code to do:

    from_instant = GetScanTimeFromFile(@".\last_scan_instant.txt");
    from_instant = DateTime.Now;
    

    Why would you assign a value to from_instant and then immediately assign a different value?

    (Side-note: underscores are pretty unidiomatic in C# code.)

    In my case below, whenever I reset “from_instant”, it doesn’t change.

    Sounds like you’re probably not diagnosing things properly. Changing the value of the variable really will change it…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this question but the answer does not perform a refactoring of the
I read this question in stackoverflow. The excerpt answer provided by bbum is below:
I have read this question and the simple and clear answer but it's not
My question is in relation this this answer. https://stackoverflow.com/a/8773953/1297775 I have read at many
Having read this question Immutable or not immutable? and reading answers to my previous
I read the answers to this question and understand what the code var qq
In the answers to this question , we read that function f() {} defines
I had asked this question before, but I got no correct answer. So, this
Ok, I hope I've got everything listed up nicely before posting this question because
(I'm fairly certain the answer to this question is quite simply no but I

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.