Parsing a strangely formatted DateTime. Anyone fancy stepping up? - c#

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"));

Related

How to parse CET/CEST datetime?

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);

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);

Error : String was not recognized as a valid DateTime while converting to date format in c#

Here is the date time format i'm trying to format.I'm getting this date format from twitter apis
string date = "Thu Jul 18 17:39:53 +0000 2013"
i tried
Convert.ToDateTime(date).ToString("dd/MM/yyyy")
But it says String was not recognized as a valid DateTime.
This works:
DateTime.ParseExact(dtStr, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture)
ParseExact and TryParseExact allows to use a custom format string. ddd is the abbreviated day name, MMM the abbreviated month name, dd the day number, HH hours in 24h clock format, mm minutes, ss seconds, zzzz the time-zone and yyyy the years.
I have used CultureInfo.InvariantCulture to specify that the current culture is not used but InvariantCulture which is similar to "en-US".
Demo
works but after getting date from your line of code i tried to do
date.ToString("dd/mm/yyyy") but get the string as 12-12-2013, no
slashes
/ is a replacement character for your current culture's date-separator which is obviously -. So also use CultureInfo.InvariantCulture to specify that the separator should be used without using your current culture:
string result = dateTime.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);
See: The "/" Custom Format Specifier
Try this
DateTime.ParseExact(YourDate, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture)
Its better to use Invariant culture than Current culture
You are trying to convert a non-standard format, so use this:
string dateStr = "Thu Jul 18 17:39:53 +0000 2013";
DateTime date = DateTime.ParseExact(dateStr, "ddd MMM dd h:mm:ss KKKK yyyy", System.Globalization.CultureInfo.InvariantCulture);
Or build the correct format for your input.
How about like;
string date = "Thu Jul 18 17:39:53 +0000 2013";
DateTime dt = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
Output will be;
18.07.2013 20:39:53
K for time zone information in here.
Check out for more information;
Custom Date and Time Format Strings
Your date string needs to be this:
Thu Jul 18 2013 17:39:53 +0000
Whatever is producing your string needs to have the year value after the month and day and before the time, like above.
string date = "Thu Jul 18 2013 17:39:53 +0000";
var theDate = Convert.ToDateTime(date);
Note: This will produce a valid .NET DateTime object.
UPDATE:
If you cannot change the string produced, then use the ParseExact method with a custom format, like this:
string date = "Thu Jul 18 17:39:53 +0000 2013";
var theDate = DateTime.ParseExact(date, "ddd MMM dd H:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
Try using DateTime.ParseExact.
string date = "Thu Jul 18 17:39:53 +0000 2013"
DateTime date = DateTime.ParseExact(date, "dd/MM/yyyy", null);
this.Text="22/11/2009";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);

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"));

Categories

Resources