PeteTheGreek
PeteTheGreek

Reputation: 729

c# convert string to date type

I've got this string:

string date = "Sun, 17 Mar 2013 12:40:23 +0000";

And trying to convert to a date type but I keep getting a not in correct format error when I try the convert.

DateTime dt = Convert.ToDateTime(date);

And then trying to get it into these formats:

dt.ToString("dd")
dt.ToString("MMMM")
dt.ToString("yyyy")

Upvotes: 4

Views: 14464

Answers (8)

Rahul K A
Rahul K A

Reputation: 332

public string dateConvertion(string da) { string sDate = da; sDate = sDate.Replace("-", "/"); sDate = sDate.Replace(".", "/");

    string format = "dd/MM/yyyy";
    DateTime dDate;
    if (DateTime.TryParse(sDate, out dDate))
    {

        //if (DateTime.TryParseExact(sDate, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate))
        //{

        sDate = dDate.ToString("MM/dd/yyyy");
        sDate = sDate.Replace("-", "/");
        sDate = sDate.Replace(".", "/");

    }
    return sDate;
}

Upvotes: 0

Rohit Kiran
Rohit Kiran

Reputation: 436

Well..If you need the result in numeric format,try as shown below

 string date = "Sun, 17 Mar 2013 12:40:23 +0000";
 DateTime dt = Convert.ToDateTime(date);
 var day = dt.Day;
 var month = dt.Month;
 var year = dt.Year;
 var time = dt.ToShortTimeString();
 var hour = dt.Hour;
 var minute = dt.Minute;
 var second = dt.Second;

The Variables will return the exact numerical form. NB: Hour will be depicted as 24 hour format

Upvotes: 0

eandersson
eandersson

Reputation: 26342

Try using DateTime.Parse instead.

var dt = DateTime.Parse(date);

I would also recommend that you Parse the date using DateTime.TryParse to make sure that the date is always in a valid format.

DateTime result;
if (DateTime.TryParse(date, out result))
{
    Console.WriteLine(result.ToString("dd"));
    Console.WriteLine(result.ToString("MMMM"));
    Console.WriteLine(result.ToString("yyyy"));
}
else
{
    Console.WriteLine("Error parsing date.");
}

If you are still experiencing issues you may need to provide DateTime with a CultureInfo. This allows you to specify the exact Culture used by the parser, to ensure that the computer region settings doesn't cause any issues.

DateTime.Parse(date, new CultureInfo("en-US"));     // Specific culture
DateTime.Parse(date, CultureInfo.InvariantCulture); // Culture-insensitive

// Culture-insensitive TryParse
if (DateTime.TryParse(date, out result, CultureInfo.InvariantCulture)) 
{...} 

The normal DateTime uses the culture set by your Operating System.

Upvotes: 3

Soner Gönül
Soner Gönül

Reputation: 98740

You can use DateTime.Parse() method like;

string date = "Sun, 17 Mar 2013 12:40:23 +0000";
DateTime dt = DateTime.Parse(date, CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("dd"));
Console.WriteLine(dt.ToString("MMMM"));
Console.WriteLine(dt.ToString("yyyy"));

Output will be;

17
March
2013

Here is a DEMO.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460038

You can use DateTime.Parse with CultureInfo.InvariantCulture which ignores your current culture, hence avoids possible localization issues.

DateTime dt = DateTime.Parse(date, CultureInfo.InvariantCulture);

CultureInfo.InvariantCulture is similar to the english culture and works with your string.

Demo

Upvotes: 2

M. Mennan Kara
M. Mennan Kara

Reputation: 10222

You can use DateTime.ParseExact for the conversion.

Try the following code:

var date = "Sun, 17 Mar 2013 12:40:23 +0000";
var dt = DateTime.ParseExact(date, "ddd, dd MMM yyyy hh:mm:ss zzz", CultureInfo.InvariantCulture);

Console.WriteLine(dt.ToString("dd"));
Console.WriteLine(dt.ToString("MMMM"));
Console.WriteLine(dt.ToString("yyyy"));

Output:

17
March
2013

Upvotes: 7

Toni Petrina
Toni Petrina

Reputation: 7112

Have you checked DateTime.TryParse method? If you scroll down, you will notice that the last sample actually is "Fri, 15 May 2009 20:10:57 GMT", similar to your request.

Upvotes: 1

Nefarion
Nefarion

Reputation: 881

Try DateTime.TryParse() or DateTime.Parse()

Upvotes: 3

Related Questions