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?
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?
Simple formating can be achieved by using the following code.
DateTimePicker.Value.ToString("yyyyMMdd");
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.
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: