How to parse CET/CEST datetime? - c#

I got a JSON on the server, and I try to parse it. The problem is that some dates looks like:
Tue, 03 Sep 2013 12:18:45 CEST
some as:
Sat, 17 Nov 2012 15:39:43 CET
so if I do somethins like:
var processedData = DateTime.ParseExact((string)item.pubDate, "ddd, dd MMM yyyy HH:mm:ss CEST", CultureInfo.InvariantCulture);
only on CEST dates it works, else it raises an exception.
How can I solve it? I hope .Replace() it is not the answer...

You can use Multiple Formats like:
string[] formats = new[]
{
"ddd, dd MMM yyyy HH:mm:ss CEST",
"ddd, dd MMM yyyy HH:mm:ss CET"
};
var processedData = DateTime.ParseExact((string)item.pubDate,
formats,
CultureInfo.InvariantCulture,
DateTimeStyles.None);

Related

Convert a GMT date string to DateTime

How to convert a string date value of such format:
Wed Oct 02 2013 00:00:00 GMT+0100 (GMT Daylight Time)
To a date of format of 02/10/2013.
I have already tried the DateTime.ParseExact but it didn't work at all:
DateTime.ParseExact(dateToConvert, "ddd MMM d yyyy HH:mm:ss GMTzzzzz", CultureInfo.InvariantCulture);
var dateToConvert = "Wed Oct 02 2013 00:00:00 GMT+0100 (GMT Daylight Time)";
var format = "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(GMT Daylight Time)'";
var date = DateTime.ParseExact(dateToConvert, format, CultureInfo.InvariantCulture);
Console.WriteLine(date); //prints 10/2/2013 2:00:00 AM for my locale
You need to specify ending with 'GMT'zzz '(GMT Daylight Time)'
You can use single d in format instead of dd, works fine
You can check demo here
Thank you. This is exactly what I have done to resolve the question in the last comment above.
int gmtIndex = dateToConvert.IndexOf("G");
string newDate = dateToConvert.Substring(0, gmtIndex).Trim();
value = DateTime.ParseExact(newDate, "ddd MMM dd yyyy HH:mm:ss", CultureInfo.InvariantCulture);

C# to Convert String to DateTime

How to convert the below string to DateTime in C#?
Mon Apr 22 07:56:21 +0000 2013
When i tried the code with
Convert.ToDateTime("Mon Apr 22 07:56:21 +0000 2013")
it is throwing error as
String was not considered as valid DateTime
Try DateTime.ParseExact instead.
Example:
CultureInfo provider = CultureInfo.InvariantCulture;
dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
format = "ddd dd MMM yyyy h:mm tt zzz";
result = DateTime.ParseExact(dateString, format, provider);
More examples are available at http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
You have to specify that your input string is in a particular format. Please refer this link and this one too.
Use DateTime.ParseExact like:
string str = "Mon Apr 22 07:56:21 +0000 2013";
DateTime dt = DateTime.ParseExact(str,
"ddd MMM d HH:mm:ss +0000 yyyy",
CultureInfo.InvariantCulture);
You have basically two options for this.
DateTime.Parse() and DateTime.ParseExact(). like
DateTime parseexactdt = DateTime.ParseExact("Mon Apr 22 07:56:21 +0000 2013",
"ddd MMM d HH:mm:ss +0000 yyyy",
CultureInfo.InvariantCulture);
string input = "Mon Apr 22 07:56:21 +0000 2013";
string format = "ddd MMM dd HH:mm:ss +ffff yyyy";
DateTime dt;
if(DateTime.TryParseExact(input,format, CultureInfo.InvariantCulture,
DateTimeStyles.None,out dt))
{
// do something with dt
}
You can use this:
using System;
using System.Globalization;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
CultureInfo cult = CultureInfo.InvariantCulture;
string txt = "Mon Apr 22 07:56:21 +0000 2013";
string format = "ddd MMM dd hh:mm:ss zzz yyyy";
DateTime dt = DateTime.ParseExact(txt, format, cult);
}
}
}
If you run program from country with +06:00, you get time 13:56:21 with same date

How to convert string date : "Mon Nov 12 08:00:00 ICT 2012" to "dd/MM/yyyy HH:mm:ss" format in C#

This is my way to convert but it's not work:
string date = "Mon Nov 12 08:00:00 ICT 2012";
DateTime dateConvert =
DateTime.ParseExact(date,
"dd/MM/yyyy HH:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dateConvert);
so, how to convert it? Thankyou!
Your format string for the DateTime.ParseExact should be ddd MMM dd HH:mm:ss 'ICT' yyyy
See http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx on the details on DateTime format strings.
PARSE EXACT means you're telling it to expect:
dd/MM/yyyy HH:mm:ss
But your input string is not in that format!!!
You need something like:
ddd MMM dd HH:mm:ss \I\C\T yyyy
string date = "Mon Nov 12 08:00:00 ICT 2012";
DateTime d = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss ICT yyyy", null);
Console.WriteLine(d.ToString("dd/MM/yyyy HH:mm:ss"));
What you've got wrong is that the format you provide to DateTime.ParseExact is supposed to be the format of the date string coming in, not what you want coming out.
Try something like:
string date = "Mon Nov 12 08:00:00 ICT 2012";
DateTime dateConvert = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss 'ICT' yyyy", null);
Console.WriteLine(dateConvert.ToString("dd/MM/yyyy HH:mm:ss"));
Thanks for all care! This is my solution and it's work cool!
DateTime dateTime = DateTime.ParseExact("Mon Nov 12 08:00:00 ICT 2012", "ddd MMM dd HH:mm:ss ICT yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dateTime.ToString("MM/dd/yyyy"));

Parsing a strangely formatted DateTime. Anyone fancy stepping up?

I'm trying to parse a datestamp (that I got from Twitter) but am receiving errors. here's the datestamp:
Fri, 27 Aug 2010 22:00:07 +0000
Here's my code:
DateTime.ParseExact(MyDateValue, "ddd, dd MMM YYYY HH:mm:ss +ffff", new CultureInfo("en-US"))
and here's my error:
System.FormatException was unhandled
Message=String was not recognized as a valid DateTime.
Anyone fancy taking that on? To make it easy I've provided the code below for a console app that exhibits the problem.
Thanks
Jamie
using System;
using System.Globalization;
class Program
{
static void Main(string[] args)
{
string MyDateValue = "Fri, 27 Aug 2010 22:00:07 +0000";
var dt = DateTime.ParseExact(MyDateValue, "ddd, dd MMM YYYY HH:mm:ss +ffff", new CultureInfo("en-US"));
}
}
The year specifier is yyyy, not YYYY:
string MyDateValue = "Fri, 27 Aug 2010 22:00:07 +0000";
var dt = DateTime.ParseExact(MyDateValue, "ddd, dd MMM yyyy HH:mm:ss +ffff", new CultureInfo("en-US"));
The above works fine, as far as that it will not throw an exception.
I am assuming that the +0000 at the end of the string is supposed to be a timezone specifier. If so, the ffff is incorrect, as it stands for The hundred thousandths of a second, not the timezone specifier, which is K. If it is indeed supposed to be the timezone specifier, then this would be the correct code:
string MyDateValue = "Fri, 27 Aug 2010 22:00:07 +0000";
var dt = DateTime.ParseExact(MyDateValue, "ddd, dd MMM yyyy HH:mm:ss K", new CultureInfo("en-US"));
See Custom Date and Time Format Strings.
Should your YYYY be yyyy?
My help file for the custom formatting information for DateTime only has lowercase y's, no uppercase.
Notice the change in year: YYYY->yyyy
DateTime.ParseExact(MyDateValue, "ddd, dd MMM yyyy HH:mm:ss +ffff", new CultureInfo("en-US"))
System.DateTime.ParseExact(MyDateValue, "ddd, dd MMM yyyy HH:mm:ss zzz", new System.Globalization.CultureInfo("en-US"));
The year part needs to be lower-case: ddd, dd MMM yyyy HH:mm:ss +ffff
I dropped the "+0000" and just used DateTime.Parse()
To get to my actual timezone (since +0000 is probably an offset from GMT) I set that as well.
string myDateValue = "Fri, 27 Aug 2010 22:00:07"; //get this using substring
int gmtOffset = -6; //I'm in the Central TimeZone
DateTime dt = DateTime.Parse(myDateValue);
dt.AddHours(gmtOffset);
Console.WriteLine(dt.ToString("ddd, dd MMM yyyy hh:mm:ss"));

C# : How to convert string to DateTime, where the string can have any of the standard datetime format

I had posted a question on DateTime to String conversion, I got many satisfying answers for that .. so I thank StackOverflow very much .. Here is one more problem of String manupulation, I am stuck with .. I have to convert a string (from some external source) using C# code .. the string can have these expected format of DateTime ..
02/31/2009 01:59:59 24 hours format
02/31/2009 01:59:59 AM 12 hours format
2/31/2009 1:59:59
2/31/2009 1:59:59 AM
02/01/2009 01:59:59 AM
2/1/2009 1:59:59
and so on .......
I tried using DateTime(Convert.ToInt32(string_date.Substring(6,4)),Int,Int,Int,Int,Int,Int) ie, By extracting the values of month, Day etcBut it doesn't work .. because I can't extract the values with substring perfectly .. as the length of string is Varying I also have tried to extract the values referring the occurance of "/", "space" and ":" but it becomes bottle neck to derive with (non-)Occurrence of AM/PM Only the length of Day, Month and Hours can vary ..
You can use the DateTime.ParseExact overload that takes a list of formats:
private static string[] formats = new string[]
{
"MM/dd/yyyy HH:mm:ss tt",
"MM/dd/yyyy HH:mm:ss",
"M/dd/yyyy H:mm:ss tt",
"M/dd/yyyy H:mm:ss"
};
private static DateTime ParseDate(string input)
{
return DateTime.ParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
}
This will throw a FormatException if the passed string does not match any of the given formats. Notice that the formats expecting AM/PM should appear before identical formats without AM/PM ("MM/dd/yyyy HH:mm:ss tt" comes before "MM/dd/yyyy HH:mm:ss").
Update
As Henk points out in the comments, the same functionality is available when using TryParseExact which removes exception situation. Also, paired with nullable types this can be made a bit cleaner:
private static DateTime? ParseDate(string input)
{
DateTime result;
if (DateTime.TryParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
return result;
}
return null;
}
Now it will simply return a null reference if it fails to parse the input.
Take a look at the TryParseExact method. Here's an example with the first case:
DateTime date;
// I changed 02/31/2009 to 01/31/2009 because the first is not a valid date
if (DateTime.TryParseExact("01/31/2009 01:59:59", "MM/dd/yyyy HH:mm:ss", null, DateTimeStyles.None, out date))
{
// string successfully parsed => do something with the date
}
You could then keep a list of different formats and try to parse the string with all of them until you succeed.
Here are all the possible formats ..
MM/dd/yyyy 08/22/2006
dddd, dd MMMM yyyy Tuesday, 22
August 2006
dddd, dd MMMM yyyy HH:mm Tuesday,
22 August 2006 06:30
dddd, dd MMMM yyyy hh:mm tt
Tuesday, 22 August 2006 06:30 AM
dddd, dd MMMM yyyy H:mm Tuesday, 22
August 2006 6:30
dddd, dd MMMM yyyy h:mm tt Tuesday,
22 August 2006 6:30 AM
dddd, dd MMMM yyyy HH:mm:ss
Tuesday, 22 August 2006 06:30:07
MM/dd/yyyy HH:mm 08/22/2006 06:30
MM/dd/yyyy hh:mm tt 08/22/2006
06:30 AM
MM/dd/yyyy H:mm 08/22/2006 6:30
MM/dd/yyyy HH:mm:ss 08/22/2006
06:30:07
MMMM dd August 22
yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK
2006-08-22T06:30:07.7199222-04:00
ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
Tue, 22 Aug 2006 06:30:07 GMT
yyyy'-'MM'-'dd'T'HH':'mm':'ss
2006-08-22T06:30:07
HH:mm 06:30
hh:mm tt 06:30 AM
H:mm 6:30
h:mm tt 6:30 AM
HH:mm:ss 06:30:07
yyyy'-'MM'-'dd HH':'mm':'ss'Z'
2006-08-22 06:30:07Z
dddd, dd MMMM yyyy HH:mm:ss
Tuesday, 22 August 2006 06:30:07
yyyy MMMM 2006 August
DateTime dt1 = DateTime.ParseExact("2007/01/01 04:23:12", "yyyy/MM/dd hh:mm:ss",
System.Globalization.CultureInfo.CurrentCulture);
DateTime dt = Convert.ToDateTime("2007/01/01 04:23:12", System.Globalization.CultureInfo.CurrentCulture);
System.Globalization.CultureInfo.CurrentCulture
format param

Categories

Resources