This may be one of those stupid, I missed something errors but here it goes.
I have a date time in string format (no trailing or leading whitespaces)
Sun 27 Apr 2013 7:30pm
I use this code to turn it into a DateTime variable but it always returns false
DateTime date;
bool dateParsed = false;
CultureInfo provider = CultureInfo.InvariantCulture;
dateParsed = DateTime.TryParseExact(when, "ddd d MMM yyyy h:mmtt", provider, DateTimeStyles.AssumeLocal, out date);
Hopefully someone can quickly tell me what I am doing wrong here.
Because April 27th, 2013 fell on a Saturday, not on a Sunday. If you try to parse
Sat 27 Apr 2013 7:30pm
It should work. You can see that by printing out the date using the same format, and comparing it with what you're trying to parse.
bool dateParsed = false;
DateTime date;
CultureInfo provider = CultureInfo.InvariantCulture;
string when = "Sat 27 Apr 2013 7:30pm";
dateParsed = DateTime.TryParseExact(when, "ddd d MMM yyyy h:mmtt", provider, DateTimeStyles.AssumeLocal, out date);
Console.WriteLine(date);
date = new DateTime(2013, 4, 27, 19, 30, 00, DateTimeKind.Local);
Console.WriteLine(date.ToString("ddd d MMM yyyy h:mmtt", provider));
Change your code to use ParseExact instead. It gives you the exact problem by crashing with an exception:
String was not recognized as a valid DateTime because the day of week was incorrect.
April 27 2013 is a Saturday, not a Sunday.
The problem is your date, which is invalid. Change it like
Sat 27 Apr 2013 7:30pm
Check this screenshot,
Here is your code:
DateTime date;
bool dateParsed = false;
CultureInfo provider = CultureInfo.InvariantCulture;
string when = "Sat 27 Apr 2013 7:30pm";
dateParsed = DateTime.TryParseExact(when, "ddd d MMM yyyy h:mmtt", provider, DateTimeStyles.AssumeLocal, out date);
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);
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
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);
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"));
How can I convert the following strings to a System.DateTime object?
Wednesday 13th January 2010
Thursday 21st January 2010
Wednesday 3rd February 2010
Normally something like the following would do it
DateTime dt;
DateTime.TryParseExact(value, "dddd d MMMM yyyy", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt);
but this doesn't work because of the 'th', 'st' or 'rd' in the string
Update
It appears that DateTime doesn't support formatting the 'th', 'st', 'rd' etc so they need to be stripped before parsing. Rubens Farias provides a nice regular expression below.
What about strip them?
string value = "Wednesday 13th January 2010";
DateTime dt;
DateTime.TryParseExact(
Regex.Replace(value, #"(\w+ \d+)\w+ (\w+ \d+)", "$1 $2"),
"dddd d MMMM yyyy",
DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.None, out dt);
Another approach.
string sDate = "Wednesday 13th January 2010";
string[] sFields = sDate.Split (' ');
string day = sFields[1].Substring (0, (sFields[1].Length - 2));
DateTime date = new DateTime (sFields[3], sFields[2], day);
Another alternative using escape characters for handling (st, nd, rd, and th) without stripping them before the call of DateTime.TryParseExact
string dtstr = "Saturday 23rd January 2016";
DateTime dt;
string[] formats = new string[] {
"dddd d\\s\\t MMMM yyyy", "dddd d\\n\\d MMMM yyyy",
"dddd d\\r\\d MMMM yyyy", "dddd d\\t\\h MMMM yyyy" };
bool result = DateTime.TryParseExact(dtstr, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
Where does "th", "st","nd" or "rd" appear below?
monday
tuesday
wednesday
thursday
friday
saturday
sunday
january
february
march
april
may
june
july
august
september
october
november
december
However you know those 4 will always be followed by a space. So unless I've missed something, a simple
value = value.Replace("August","Augus").Replace("nd ","").Replace("st ","").Replace("nd ","").Replace("rd ","").Replace("Augus","August");
DateTime dt;
DateTime.TryParseExact(value,"DDDD dd MMMM yyyy", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt);
No credit to me but this looks interesting for general DataTime parsing: http://www.codeproject.com/KB/datetime/date_time_parser_cs.aspx?msg=3299749
I remembered this post on using MGrammar to parse a lot of different ways to express dates and times. It doesn't exactly answer your question, but it might serve as a useful base depending on what your ultimate goal is.
Expanding on Kenny's approach, I added some code to pass integers to the DateTime variable...
string sDate = "Wednesday 13th January 2010";
string[] dateSplit = sDate.Split (' ');
string day = dateSplit[1].Substring(0, dateSplit[1].Length - 2);
int monthInDigit = DateTime.ParseExact(dateSplit[3], "MMMM", CultureInfo.InvariantCulture).Month;
DateTime date = new DateTime(Convert.ToInt16(year), monthInDigit, day);