Change DateTime in the Microsoft Visual Studio debugger

C#Visual StudioDebuggingDatetime

C# Problem Overview


What the.... How do I change the value of a DateTime in the debugger? I can change it, but I get an error when leaving the edit field; it cannot parse it.

Edit: VS 2008, C#

C# Solutions


Solution 1 - C#

Without looking at what you have, I'm not really sure what edit field you're referring to. However, you could try using the immediate window and DateTime.Parse or new DateTime() instead.

Solution 2 - C#

You can change the date in the Immediate Window.

date = new DateTime(2009, 10, 05)
{05/Oct/2009 12:00:00 AM}
    Date: {05/Oct/2009 12:00:00 AM}
    Day: 5
    DayOfWeek: Monday
    DayOfYear: 278
    Hour: 0
    Kind: Unspecified
    Millisecond: 0
    Minute: 0
    Month: 10
    Second: 0
    Ticks: 633902976000000000
    TimeOfDay: {00:00:00}
    Year: 2009

Solution 3 - C#

We do can change a DateTime value directly in the Watch Window. The trick is simple: we always have to use a "new DateTime()" method, providing the appropriate parameters.

The Watch Window do not allow you to to type a new value directly, so things like "2010-07-13 9:15" or even {13/07/2010 09:00:00} don't work.

Solution 4 - C#

You can type things like this in to the Immediate window, the Watch window, or the QuickWatch window and they will get evaluated:

myDate = DateTime.Today.AddDays(2)
myDate = new DateTime(2009, 12, 25)

Solution 5 - C#

You can do it if you define your DateTime between parenthesis.

(new DateTime(2017, 09, 27))

Solution 6 - C#

If you mean on Visual Studio debugger try like this:

  • set the breakpoint
  • open your DateTime variable in QuickWatch for example (right click)
  • in Expression text box enter new value, this is example if your variable name is "dt":
    dt = dt.AddDays(3)
  • press enter and continue executing project

Solution 7 - C#

The "dt = " portion isn't strictly necessary. Just type in "dt.AddDays(3)" or "new DateTime(...)" or DateTime.Parse(""), etc, and the debugger will try to assign whatever value results from the expression you type to the variable under watch; it just so happens that because C# has assignment expressions (e.g. y = (x = 1); //sets y = 1) that assigning the expression to the variable name works. :)

Solution 8 - C#

The most easy option (non generic) =>

In the Value input inside watch window for example Type DateTime.Now then press enter

Et voilà ! :)

Solution 9 - C#

You can change the non-public data-value in miliseconds in debugger mode, to change it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRabbitView Question on Stackoverflow
Solution 1 - C#lc.View Answer on Stackoverflow
Solution 2 - C#Adriaan StanderView Answer on Stackoverflow
Solution 3 - C#tcbrazilView Answer on Stackoverflow
Solution 4 - C#slugsterView Answer on Stackoverflow
Solution 5 - C#Jp_View Answer on Stackoverflow
Solution 6 - C#user218447View Answer on Stackoverflow
Solution 7 - C#Anthony D. GreenView Answer on Stackoverflow
Solution 8 - C#rdhainautView Answer on Stackoverflow
Solution 9 - C#Guillermo ArriagaView Answer on Stackoverflow