Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Question i want to ask


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

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
C#
`System.DateTime.Value.ToString("yyyy-MM-dd");`
How Can i get the Value for other dates Using the same Method?

What I have tried:

C#
`System.DateTime.Value.ToString("yyyy-MM-dd");`
Posted
Updated 21-Oct-17 21:25pm

If you mean other dates as values just do :
C#
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

But generally you should use SqlParameter instead of string concatenation and pass the date value : Lesson 06: Adding Parameters to Commands – C# Station[^]
 
Share this answer
 
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");

This provides "2017-04-01" in the Date variable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900