Reputation: 3944
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
Reputation: 79
This worked for me...
string dt = dateTimePicker1.Value.ToString("yyyy/MM/dd");
Upvotes: 0
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
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
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
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
Reputation: 9527
You can easily format the datetime into a string like the one you want:
the_date_time.ToString("yyyyMMdd");
Upvotes: 1