Analytic Lunatic
Analytic Lunatic

Reputation: 3944

Set DateTimePicker Control to format YYYYMMDD?

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?

Upvotes: 5

Views: 25528

Answers (6)

Didu
Didu

Reputation: 79

This worked for me...

string dt = dateTimePicker1.Value.ToString("yyyy/MM/dd");

Upvotes: 0

unrealsoul007
unrealsoul007

Reputation: 4077

@Analytic Lunatic please look here for the error you are getting.. I think this will solve the issue you are having.

Upvotes: 0

DROP TABLE users
DROP TABLE users

Reputation: 1955

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");

Upvotes: 1

InContext
InContext

Reputation: 2501

Post format your date:

string formattedDate = MyDateTime.ToString("yyyyMMdd")

if directly from the DateTimePicker control use:

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

Upvotes: 1

Yannick Blondeau
Yannick Blondeau

Reputation: 9621

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");

Upvotes: 10

Tobberoth
Tobberoth

Reputation: 9527

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

 the_date_time.ToString("yyyyMMdd");

Upvotes: 1

Related Questions