0

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?

Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
user3430861
  • 172
  • 1
  • 3
  • 13
  • Possible duplicate of [How to get only the date value from a Windows Forms DateTimePicker control?](http://stackoverflow.com/questions/1138195/how-to-get-only-the-date-value-from-a-windows-forms-datetimepicker-control) – turbopipp Oct 20 '15 at 07:18

3 Answers3

4

Simple formating can be achieved by using the following code.

DateTimePicker.Value.ToString("yyyyMMdd");
sujith karivelil
  • 26,861
  • 6
  • 46
  • 76
2

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.

Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
0

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/

User2012384
  • 4,382
  • 15
  • 60
  • 97