Converting this into a date time format C#

大城市里の小女人 提交于 2021-02-18 08:30:49

问题


Okay, so i am trying to read the date/time of the Twitter feed XML, it is currently in this format: Fri May 03 15:22:09 +0000 2013 However my C# is not reading it as a Date/Time type.

This is what i got:

  ArticleDate = DateTime.Parse(d.Element("created_at").Value)

created_at contains the: Fri May 03 15:22:09 +0000 2013 Format


回答1:


Be careful. The times you are given back are in UTC. You may end up unintentionally letting your local time zone influence the result.

For example, one of the other answers suggested this code:

DateTime dt = DateTime.ParseExact("Fri May 03 15:22:09 +0000 2013",
                                  "ddd MMM dd HH:mm:ss zzz yyyy",
                                  CultureInfo.InvariantCulture);

The result of this on my computer, which is in Arizona (UTC-7), is:

5/3/2013 8:22:09 AM   (dt.Kind == DateTimeKinds.Local)

While this is the correct moment in my local time, it is not what was given to me, and it probably not what you are expecting unless paying close attention to the .Kind property.

You can instead do the following:

DateTime dt = DateTime.ParseExact("Fri May 03 15:22:09 +0000 2013",
                                  "ddd MMM dd HH:mm:ss zzz yyyy",
                                  CultureInfo.InvariantCulture,
                                  DateTimeStyles.AdjustToUniversal);

This returns:

5/3/2013 3:22:09 PM   (dt.Kind == DateTimeKinds.Utc)

Which better matches what you started with.

Now, this assumes that the values coming back from Twitter will always be UTC. That seems to be the case, according to their FAQ. But one could argue that since we are given an offset, it might be more correct to use that offset as provided. If the offset ever changes, we don't want our code to break. Therefore, it is more appropriate to use the DateTimeOffset class.

DateTimeOffset dto = DateTimeOffset.ParseExact("Fri May 03 15:22:09 +0000 2013",
                                               "ddd MMM dd HH:mm:ss zzz yyyy",
                                               CultureInfo.InvariantCulture);

The result of which is:

5/3/2013 3:22:09 PM +00:00



回答2:


You should be using the ParseExact of DateTime to get your value

DateTime.ParseExact("Fri May 03 15:22:09 +0000 2013","ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);



回答3:


Use DateTime.TryParseExact like:

if (DateTime.TryParseExact("Fri May 03 15:22:09 +0000 2013",
                                              "ddd MMMM dd HH:mm:ss zzz yyyy",
                                              CultureInfo.InvariantCulture,
                                              DateTimeStyles.None,
                                              out ArticleDate))
            {
                //date is fine
            }



回答4:


The actual code fix for this specific date format from Twitter APi XML is below:

using System.Globalization;

CultureInfo enUS = new CultureInfo("en-US");                
DateTime dateValueOut;
string userCreated = "Fri May 03 15:22:09 +0000 2013"; 

bool isDateFormatted = DateTime.TryParseExact(userCreated,"ddd MMM dd HH:mm:ss zzzz yyyy",enUS,DateTimeStyles.None, out dateValueOut);

if (isDateFormatted == true)
{
    DateTime formattedDateTime = dateValueOut;                    
}



回答5:


You can use

var date = DateTime.TryParseExcact(d.Element("created_at").Value, new String[]{"pattern" });

http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact.aspx



来源:https://stackoverflow.com/questions/16521777/converting-this-into-a-date-time-format-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!