Set DateTimePicker Control to format YYYYMMDD?

19,550

Solution 1

Actually, if your control is named dtpDate, you should use something like this (using the Value property of the control):

string selectDateAsString = dtpDate.Value.ToString("yyyyMMdd");

Solution 2

You can easily format the datetime into a string like the one you want:

 the_date_time.ToString("yyyyMMdd");

Solution 3

you need to get the DateTime value from the date time picker then do tostring() on that with the format.

string formateddate = dtpDate.Value.ToString("yyyyMMdd");

Solution 4

Post format your date:

string formattedDate = MyDateTime.ToString("yyyyMMdd")

if directly from the DateTimePicker control use:

string formattedDate = MyDateTime.Value.ToString("yyyyMMdd")

Share:
19,550
Analytic Lunatic
Author by

Analytic Lunatic

Software Developer by Day, Web Designer by Night.

Updated on June 10, 2022

Comments

  • Analytic Lunatic
    Analytic Lunatic almost 2 years

    I am putting together a simple C# Windows Forms Application. I am setting my DateTimePicker control on load to the current DateTime, example "11/12/2013 9:49:49 AM". I then use this value in a query to my 400 system, but I am getting an error because the field I query against the DateTimePicker Controls value is in format 'YYYYMMDD'.

    How do I format the value of my DateTimePicker Control to 'YYYYMMDD' to use it in my query?

  • Analytic Lunatic
    Analytic Lunatic over 10 years
    When I try string formattedDate = dtpDate.ToString("yyyyMMdd"); I receive: "No overload for method 'ToSTring' takes 1 arguments".
  • DROP TABLE users
    DROP TABLE users over 10 years
    @AnalyticLunatic you need to get the DateTime value from the date time picker then do tostring() on that.
  • Analytic Lunatic
    Analytic Lunatic over 10 years
    When I try string formattedDate = dtpDate.ToString("yyyyMMdd"); I receive: "No overload for method 'ToSTring' takes 1 arguments".
  • Analytic Lunatic
    Analytic Lunatic over 10 years
    When I try string formattedDate = dtpDate.ToString("yyyyMMdd"); I receive: "No overload for method 'ToSTring' takes 1 arguments".
  • Analytic Lunatic
    Analytic Lunatic over 10 years
    @DROPtableusers, string formattedDate = dtpDate.Value.ToString("yyyyMMdd"); did it! Thanks!
  • DROP TABLE users
    DROP TABLE users over 10 years
    @AnalyticLunatic glad you got it. I edited my answer to reflect below.