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);
Related
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);
I have the following string which I want to bind to DateTime object for further processing:
Fri Dec 7 16:36:21 2012
I tried this:
string format = "ddd MMM dd hh:mm:ss yyyy";
DateTime.ParseExact(_srdfLag.CaptureTime, format,
CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces);
However, it throws an exception: String was not recognized as a valid DateTime
What is wrong with my code?
You have 24 hour date change format accordingly, you need HH instead of hh, also used instead of dd.
string date = "Fri Dec 7 16:36:21 2012";
string format = "ddd MMM d HH:mm:ss yyyy";
DateTime dt = DateTime.ParseExact(date, format, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces);
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"));
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"));
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