Reputation: 51
I need convert the date of string format 'MM/dd/yyyy' to datetime format 'MM/dd/yyyy' when the system date format is 'dd/MM/yyyy'.
Upvotes: 0
Views: 2298
Reputation: 326
DateTime dt = DateTime.ParseExact("05/02/2014","MM/dd/yyyy",CultureInfo.InvariantCulture);
Upvotes: 0
Reputation: 1620
try this
string dateString="05/02/2014";
DateTime txtmyDate =convert.toDateTime(dateString);
Upvotes: 0
Reputation: 3520
DateTime
stores a date as a numerical value, not as a string. So asking for a DateTime
type of format 'MM/dd/yyyy' doesn't make much sense. If you are displaying the date in a WPF control, it will default to displaying a string that conforms to the system date format. But that is just a display string that you can manipulate using the format strings alluded to in the links above. Using @Sajeetharan's code will create a DateTime
struct with the correct value internally. How you display it is up to you and your string formatting choices.
The same goes for dates stored in a database column of type 'datetime'. The value is stored numerically. Your query editor will display the value likely in the same format as your system settings. If the date is stored as a string, then again, @Sajeetharan's code is the correct way to convert the database string into a DateTime
struct.
Upvotes: 2
Reputation: 342
This may help:
string dateString="05/02/2014";
DateTime txtmyDate = DateTime.ParseExact(dateString, "MM/dd/yyyy",
CultureInfo.InvariantCulture);
Upvotes: 1
Reputation: 222582
DateTime.ParseExact("05/02/2014", "dd/MM/yyyy", CultureInfo.InvariantCulture)
.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
Upvotes: 1