C# 在 Microsoft Visual Studio 调试器中更改日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1795767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:54:29  来源:igfitidea点击:

Change DateTime in the Microsoft Visual Studio debugger

c#visual-studiodebuggingdatetime

提问by Rabbit

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.

什么.... 如何在调试器中更改 DateTime 的值?我可以更改它,但是在离开编辑字段时出现错误;它无法解析它。

Edit: VS 2008, C#

编辑:VS 2008,C#

采纳答案by lc.

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.Parseor new DateTime()instead.

如果不查看您拥有的内容,我不确定您指的是哪个编辑字段。但是,您可以尝试使用即时窗口和/DateTime.Parsenew DateTime()代替。

回答by Adriaan Stander

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

回答by slugster

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)

回答by user218447

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

如果您的意思是在 Visual Studio 调试器上尝试这样:
- 设置断点
- 例如在 QuickWatch 中打开您的 DateTime 变量(右键单击)
- 在表达式文本框中输入新值,如果您的变量名称为“dt”,则这是示例:
dt = dt.AddDays(3)
- 按回车继续执行项目

回答by Anthony D. Green

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

“dt =”部分不是绝对必要的。只需键入“dt.AddDays(3)”或“new DateTime(...)”或 DateTime.Parse("") 等,调试器将尝试将您键入的表达式的结果分配给变量受监视;恰巧因为 C# 有赋值表达式(例如 y = (x = 1); //sets y = 1),所以将表达式赋值给变量名是可行的。:)

回答by tcbrazil

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.

我们可以直接在 Watch Window 中更改 DateTime 值。诀窍很简单:我们总是必须使用“new DateTime()”方法,提供适当的参数。

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.

监视窗口不允许您直接键入新值,因此诸如“2010-07-13 9:15”甚至 {13/07/2010 09:00:00} 之类的内容都不起作用。

回答by Jp_

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

如果您在括号之间定义 DateTime,则可以执行此操作。

(new DateTime(2017, 09, 27))

回答by rdhainaut

The most easy option (non generic) =>

最简单的选项(非通用)=>

In the Valueinput inside watch windowfor example Type DateTime.Nowthen press enter

监视窗口内的输入中,例如键入然后按回车DateTime.Now

Et voilà ! :)

等等!:)