Reputation: 182
I am getting date value correctly from DateTimePicker using DateTimePicker.Value
.
However I wish to format my date to be YYYYMMDD
(ex: "20151020"). Is this possible please?
Upvotes: 0
Views: 9504
Reputation: 4927
Seems you want to have your result in string format:
string dateFormat = "yyyyMMdd";
string result = DateTimePicker.Value.ToString(dateFormat);
For other formats, here's the reference:
http://www.csharp-examples.net/string-format-datetime/
Upvotes: 0
Reputation: 98868
DateTimePicker.Value
returns DateTime
, not a string
. A DateTime
does not have any implicit format. It just have date and time values. It's textual representation can have a format which is usually done with ToString()
method like;
string myFormat = DateTimePicker.Value.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
Remember, there is no YYYY
and DD
as a custom date and time format strings. They should be as yyyy
and dd
since they are case sensitive.
Upvotes: 2
Reputation: 29036
Simple formating can be achieved by using the following code.
DateTimePicker.Value.ToString("yyyyMMdd");
Upvotes: 5