获取其他日期的C#值

获取其他日期的C#值

问题描述:

我有一个问题我想问一下





I have a Question i want to ask


string Date = System.DateTime.Now.ToString("yyyy-MM-dd");



返回今天的日期,现在非常正确如果我想让它返回其他日期的日期那么我可以在我的更新SQL语句中使用,我该怎么做?新手使用`System.DateTime()`因为我没有看到类似


Returns the Date for today, Very correct now how about if i want it to return dates for other days so i can use in My update SQL statement , How do i go about that? Novice to using `System.DateTime()` as i didn't See something like

`System.DateTime.Value.ToString("yyyy-MM-dd");`

如何使用相同的方法获取其他日期的值?



我尝试过:



How Can i get the Value for other dates Using the same Method?

What I have tried:

`System.DateTime.Value.ToString("yyyy-MM-dd");`

如果您将其他日期表示为值:

If you mean other dates as values just do :
DateTime dt = DateTime.Parse("2017-10-22"); // parse from a string
DateTime dt = datepicker.Value; // form a date picker

string date = dt.ToString("yyyy-MM-dd"); // same as before



但一般来说你应该使用 SqlParameter 而不是字符串连接并传递日期值:课程06:向命令添加参数 - C#站 [ ^ ]


But generally you should use SqlParameter instead of string concatenation and pass the date value : Lesson 06: Adding Parameters to Commands – C# Station[^]


首先生成一个DateTime对象并在构造函数中提供日期。然后在此对象上调用ToString。

First you generate a DateTime object and provide the date in the constructor. Then you call the ToString on this object.
// Construct a DateTime object for a specified date
DateTime specificDate = new DateTime(2017, 4, 1);   // April fool's day 2017
string Date = specificDate.ToString("yyyy-MM-dd");



这在Date变量中提供2017-04-01