nikorio
nikorio

Reputation: 691

Set specific date with string to dateTimePicker

I'm trying to set specific date for dateTimePicker, but not sure how to do it properly. The goal is to load date and time of user previous load insert from text document, each time user loads applications, and then to save new date as string for next using to load into dateTimePicker.

This way it does not sets and I'm not sure how to load string like "25/03/2017" to:

   dateTimePicker.Value = new DateTime(1978, 05, 22);

to use it as string for further processing:

   string bd = dateTimePicker.Value.ToString("yyyy-M-d");  

Upvotes: 3

Views: 15784

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

You can use DateTime.ParseExact to parse date string in the format which you have:

dateTimePicker.Value = 
  DateTime.ParseExact("25/03/2017", "dd/MM/yyyy", CultureInfo.InvariantCulture);

Note that when you are getting date from DateTimePicker control you are converting same date to another format, and it will look like 2017-3-25.

Upvotes: 8

Related Questions