Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string to date - in C# [ "Mon Jan 13 2014 00:00:00 GMT+0000 (GMT Standard Time)"]

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.

like image 422
user2739418 Avatar asked Jan 15 '14 13:01

user2739418


People also ask

How to convert string to datetime in C?

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

What is the use of datetime in C?

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.

How to convert string to date in R?

The easiest way to convert strings to dates in R is with the as.Date () function, which uses the following syntax:

What is the use of string format in DateTime object?

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.


1 Answers

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.

like image 193
Mike Perrenoud Avatar answered Oct 06 '22 15:10

Mike Perrenoud