71

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#

9 Answers 9

53

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.

4
  • 5
    For instance in the watch window; a DateTime will show as {03/12/2009 00:00:00}, but if I change e.g. the date to 04 instead of 03, I get an error from VS: Invalid expression term '{'
    – Rabbit
    Commented Nov 25, 2009 at 9:50
  • 3
    Thanks (also you, astander), that's what I needed. Although I think it sucks :-) The debugger should support that.
    – Rabbit
    Commented Nov 25, 2009 at 9:56
  • 1
    slugster's answer has an example of how to use this answer: Just type the following in the Immediate window myDate = new DateTime(2009, 12, 25) Commented Oct 17, 2019 at 10:20
  • immediate windows doesn´t work, when you want to set an datetime in an object, that is declared by an interface, but the interfaces doesn´t have that property.
    – Simon
    Commented Jun 28, 2023 at 13:40
46

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
2
  • 1
    If date is not readonly ... but he is asking for changing the value of a DateTime ... your solution is to change the reference. The answer should be - you can't, because it is immutable, isn't it?
    – tanascius
    Commented Nov 25, 2009 at 9:52
  • 1
    Date is immutable, but this creates a new date object which you can assign to the variable you want to change.
    – kristianp
    Commented Apr 12, 2013 at 1:50
19

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.

2
  • 3
    You can do this hovering the mouse directly over the variable without having to use the Watch Window. It works in Visual Studio 2012. I don't know about previous versions.
    – DJH
    Commented Jun 5, 2013 at 12:00
  • Thank you, using new DateTime(...) did the trick! This was a member of class, and "token.expiry = new Datetime(...)" in the immediate window failed with "object reference not set to an instance of an object". Commented Jul 26, 2017 at 19:16
15

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

(new DateTime(2017, 09, 27))
1
  • 1
    Your answer is totally right, direct, and avoiding to waste time and taking the question to another fields, you get my vote
    – Windgate
    Commented Feb 21, 2023 at 15:32
13

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)
4

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

0

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

1
  • Signatures are considered bad form. Please read the FAQ for details. Commented Dec 19, 2012 at 19:33
0

The most easy option (non generic) =>

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

Et voilà ! :)

-1

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

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jan 5, 2022 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.