I have a situation in which I am receiving date as a string in following format.
"Mon Jan 13 2014 00:00:00 GMT+0000 (GMT Standard Time)"
I need to convert it to following format in c# (Either date/string) for further processing
YYYY-MM-DD (2014-01-13)
Convert.ToDateTime(SelectedData)
Above code thorws following error:
'Convert.ToDateTime(SelectedData)' threw an exception
of type 'System.FormatException' System.DateTime {System.FormatException}
Any suggestions?
I can't change the format in which I am receiving the date Best Regards.
String To DateTime Conversion In C#. 1 C# DateTime Conversion. C# DateTime is a struct type, which is mostly used in applications to manage date, date-time, time data types. Most of time, ... 2 Convert.ToDateTime () 3 DateTime.Parse () 4 DateTime.ParseExact () 5 DateTime.TryParse () More items
C# DateTime Conversion C# DateTime is a struct type, which is mostly used in applications to manage date, date-time, time data types. Most of time, we get a date in form of a string and we usually need to parse to a DateTime object to perform some operations like date difference, weekday, month name, formatting and so on.
The easiest way to convert strings to dates in R is with the as.Date () function, which uses the following syntax:
It is a string array which contains a list of formats and at least one format must match with string (value) to convert DateTime object. Provider: It is an object which specify culture info. Style: It defines the formatting options that customize string parsing for some date and time parsing methods.
You're going to need to use DateTime.ParseExact
:
var date = DateTime.ParseExact(
"Mon Jan 13 2014 00:00:00 GMT+0000 (GMT Standard Time)",
"ddd MMM dd yyyy HH:mm:ss 'GMT'K '(GMT Standard Time)'",
CultureInfo.InvariantCulture);
after parsing the date you can then send it out your way:
date.ToString("yyyy-MM-dd");
Here's an Ideone to prove it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With