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
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
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);
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
}
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;
}
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
Related
My app has a DateTime picker control that send selected date in this format: "Wed Mar 01 2017", In server side I'm doing Convert.ToDateTime("Wed Mar 01 2017").
With everything date until today this work fine, but with "Wed Mar 01 2017" its throw Invalid format exception.
Why is that?
You need to use ParseExact or TryParseExact and specify the format like:
DateTime dt;
if(DateTime.TryParseExact("Wed Mar 01 2017","ddd MMM dd yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
//invalid date
}
The reason it is failing on your machine is due to a culture that doesn't support the format, otherwise your code should work for en-US culture.
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
DateTime dt = Convert.ToDateTime("Wed Mar 01 2017");
Just make sure that the culture is not changed on your machine.
To be on the safe side, it is always a better idea to parse the date using the format with InvariantCulture so that you can support your applications across multiple cultures.
check DateTime.ParseExact
DateTime.ParseExcact("Wed Mar 01 2017","ddd MMM dd yyyy",CultureInfo.InvariantCulture);
Try this code
var date = "Wed Mar 01 2017";
Console.WriteLine(DateTime.ParseExact(date, "ddd MMM dd yyyy", CultureInfo.InvariantCulture));
I have to convert the below string in DateTime. I have used following code for that but it was not working.
DateTime created = DateTime.ParseExact("Sun Feb 23 2014 00:00:00 GMT+0550", "ddd mmm d yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
My string to which I have to convert in Date Time is--
Sun Feb 23 2014 00:00:00 GMT+0550
You need uppercase month and 'GMT'zzz
DateTime created = DateTime.ParseExact("Sun Feb 23 2014 00:00:00 GMT+0550"
, "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz"
, System.Globalization.CultureInfo.InvariantCulture);
I would parse this as a DateTimeOffset instead of a DateTime - after all, that's the data you've been given. Assuming it's always specified using GMT+... you can use a format string of "ddd MMM d yyyy HH:mm:ss 'GMT'zzz". In particular, note:
MMM is abbreviated month name, not mmm
'GMT' will always match the letters 'GMT'
zzz is a UTC offset including minutes. It would be formatted with a colon, but apparently it's permissive enough without the colon being specified.
Sample code:
using System;
using System.Globalization;
class Test
{
static void Main()
{
var dto = DateTimeOffset.ParseExact
("Sun Feb 23 2014 00:00:00 GMT+0550",
"ddd MMM d yyyy HH:mm:ss 'GMT'zzz",
CultureInfo.InvariantCulture);
Console.WriteLine(dto); // 23/02/2014 00:00:00 +05:50
}
}
Try This:
DateTime created = DateTime.ParseExact("Sun Feb 23 2014 00:00:00 GMT+0550",
"ddd MMM d yyyy HH:mm:ss 'GMT'zzz",
System.Globalization.CultureInfo.InvariantCulture);
This question already has answers here:
C# to Convert String to DateTime
(6 answers)
Closed 9 years ago.
I am extracting the data from an api and I am getting this value "Fri Aug 16 21:06:52 +0000 2013" I would like to know how I would be able to change this string value to type Date time
Use DateTime.ParseExact or (if the input may be invalid) DateTime.TryParseExact:
string input = "Aug 16 21:06:52 +0000 2013";
DateTime output;
if (DateTime.TryParseExact(input, "MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out output))
{
// date was parsable, here is it:
Console.WriteLine(output.ToLongDateString());
}
Custom Date and Time Format Strings, especially the "zzz" Custom Format Specifier
You can use DateTime.ParseExact
DateTime.ParseExact("Aug 16 21:06:52 +0000 2013", "MMM dd HH:mm:ss +ffff yyyy",
System.Globalization.CultureInfo.InvariantCulture);
You should read DateTime custom formats.
this should solve your problem thougf it
DateTime result = DateTime.ParseExact("Aug 16 21:06:52 +0000 2013", "MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
or to do it more appropriately and avoid exceptions. Do it like this
//zzz is Hours and minutes offset from UTC
string[] formats = { "MMM dd HH:mm:ss zzz yyyy" };
DateTime result;
string date = "Aug 16 21:06:52 +0000 2013";
if (DateTime.TryParseExact(date, formats, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
// i prefer this method though
}
Parsing string to datetime:
http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx
DateTime.TryParse() returns true or false for success / fail.
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
DateTime.Parse() throws an exception on failure.
There are a few ways...
DateTime.TryParse
DateTime dt;
if (DateTime.TryParse("Aug 16 21:06:52 +0000 2013", out dt))
{
//parsing was successfull
}
This won't throw an exception if the parse fails.
Then there is DateTime.Parse:
DateTime dt = DateTime.Parse("Aug 16 21:06:52 +0000 2013");
Unlike TryParse, this will throw an exception if parsing fails.
And there is also, Convert.ToDateTime:
DateTime dt = Convert.ToDateTime("Aug 16 21:06:52 +0000 2013", culture);
This also throws an error if the conversion fails.
Edited after you updated your question.
If you want it converted to your local time zone, use:
var dateTime = DateTime.ParseExact("Fri Aug 16 21:06:52 +0000 2013",
"ddd MMM dd HH:mm:ss zzz yyyy",
CultureInfo.InvariantCulture);
Otherwise, use:
var dateTime = DateTime.ParseExact("Fri Aug 16 21:06:52 +0000 2013",
"ddd MMM dd HH:mm:ss zzz yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal);
If the +0000 is always like that in the input, and you want to disregard that completely, use:
var dateTime = DateTime.ParseExact("Fri Aug 16 21:06:52 +0000 2013",
"ddd MMM dd HH:mm:ss +0000 yyyy",
CultureInfo.InvariantCulture);
I have a strange problem:
string format = #"ddd MMM dd hh:mm:ss \G\M\Tzzz yyyy";
__timestamp = "Fri Apr 09 17:02:00 GMT-0500 2010";
DateTime.ParseExact(__timestamp, format, new CultureInfo("en"));
returning FormatException = "String was not recognized as a valid DateTime."
but that code going without exceptions:
string format = #"ddd MMM dd hh:mm:ss \G\M\Tzzz yyyy";
__timestamp = "Sat Apr 10 01:27:00 GMT-0500 2010";
DateTime.ParseExact(__timestamp, format, new CultureInfo("en"));
From 30k of date parsing of that format, around 50% of that failed with that exception...
Anyone know why?
it should be HH not hh. You're in the 24-hr format.
ddd MMM dd HH:mm:ss \G\M\Tzzz yyyy
Valid: Sat Apr 10 01:27:00 GMT-0500 2010
Seems that DateTime is expecting AM/PM info for that "en" format provider. Try it with any hours less than 12 (inclusive), or add some AM/PM information
This is a quick one, i wanna parse a date that comes in this format "Sun May 23 22:00:00 UTC+0300 2010"
Is this a valid UTC DateTime? And how to parse it? I tried :
DateTime newStartTime = DateTime.ParseExact(hdnNewStartTime.Value, "ddd MM dd HH:mm:ss UTC+0300 yyyy", CultureInfo.CurrentCulture);
However, this didn't work, any help appreciated!
DateTime dt = DateTime.ParseExact(s,"ddd MMM dd HH:mm:ss UTCzzzz yyyy", System.Globalization.CultureInfo.InvariantCulture);
Its not a standard format, but you can still parse it.
string format = "ddd mmm dd HH:mm:ss zzzzz yyyy";
string temp = "Sun May 23 22:00:00 UTC+0300 2010";
DateTime time = DateTime.ParseExact(temp, format, CultureInfo.InvariantCulture);
This isn't in a standard .NET format, so you'll probably have to parse it by hand. The UTC+0300 bit indicates the timezone, everything else is part of the date and time.
I tried the solution presented by #johncatfish and it does what I expect. I would presume that you actually want to keep the timezone information.
[Test()]
public void TestCaseWorks ()
{
string format = "ddd MMM dd HH:mm:ss UTCzzzzz yyyy";
string temp = "Sun May 23 22:00:00 UTC+0300 2010";
DateTime time = DateTime.ParseExact(temp, format, CultureInfo.InvariantCulture);
Assert.AreEqual(DayOfWeek.Sunday, time.DayOfWeek);
Assert.AreEqual(5, time.Month);
Assert.AreEqual(23, time.Day);
Assert.AreEqual(0, time.Minute);
Assert.AreEqual(0, time.Second);
Assert.AreEqual(2010, time.Year);
// Below is the only actually useful assert -- making sure the
// timezone was parsed correctly.
// In my case, I am GMT-0700, the target time is GMT+0300 so
// 22 + (-7 - +3) = 12 is the expected answer. It is an exercise
// for the reader to make a robust test that will work in any
// timezone ;).
Assert.AreEqual(12, time.Hour);
}
Sorry for my previous answer which was quite simplistic.
Replace MM by MMM in your date format and it should be fine.
From the example given, it isn't possible to tell if the month should be in the 3 letter form (Jan, Feb, May etc.) or in the full form (January, February, May etc.).
If it should be in the short form, use:
ddd MMM dd HH:mm:ss UTCzzz yyyy
If it should be in the long form, use:
ddd MMMM dd HH:mm:ss UTCzzz yyyy
Details of the formatting specifiers available can be found at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx