Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Datetimepicker to a date from a string

Tags:

c#

I got a file which has dates stored as the following format (yyyy|dMM|dd). I now need to retrieve the values from this file and set the datetimepicker to the correct year month and day matching the ones in the stored string. Im able to retrieve all data correctly only i dont know how to set the calander correctly matching the strings. This is what i got so far although it doesnt work:

string[] editdate = clean[3].Split('|');
EditDatepickCalendar.Value = DateTime.Parse("{0},{1},{2}", editdate[0], editdate[1], editdate[2]);
like image 424
DW24da Avatar asked Mar 15 '26 11:03

DW24da


1 Answers

If sounds like you should just be using DateTime.ParseExact:

DateTime date = DateTime.ParseExact(clean[3], "yyyy'|'MM'|'dd",
                                    CultureInfo.InvariantCulture);

Quoting the | isn't strictly necessary here, but it makes it clear that you're just intending them to be taken as literal text, rather than having any date/time-related meaning.

(Note that the problem really isn't anything to do with the DateTimePicker here - it's parsing the value. When you run into an issue, you should perform appropriate diagnostics to work out where it is - in the UI, data tranformation, storage etc.)

like image 94
Jon Skeet Avatar answered Mar 18 '26 00:03

Jon Skeet