Unable to parse abbreviated day name from string - c#

I'm trying to parse a date in my string using DateTime.TryParseExact. The string looks like this:
Wed, 21 Apr 2019 07:28:45 GMT
However, the parse function always fails when I use the ddd format specifier. If I remove 'Wed' from the string and the 'ddd' part from the format string, it parses just fine.
Here's a bit of code that reproduces the problem
var ci = CultureInfo.CreateSpecificCulture("en-GB");
var datesToParse = new[] { "Wed, 21 Apr 2019 07:28:45 GMT", "21 Apr 2019 07:28:45 GMT" };
var formats = new[] { "ddd, dd MMM yyyy HH:mm:ss 'GMT'", "dd MMM yyyy HH:mm:ss 'GMT'" };
foreach (var dateToParse in datesToParse)
{
var result = DateTime.TryParseExact(dateToParse, formats, ci, DateTimeStyles.AllowWhiteSpaces, out DateTime parsedDate)
? parsedDate.ToLongDateString()
: $"Unable to parse date: {dateToParse}";
Console.WriteLine(result);
}
Console.ReadLine();
And the output:
Unable to parse date: Wed, 21 Apr 2019 07:28:45 GMT
21 April 2019
I'm not really sure what I'm doing wrong here. Any help would be appreciated.
edit: improved the output and program syntax

Your issue is that 21 April 2019 was a Sunday and not a Wednesday. This works fine "Sun, 21 Apr 2019 07:28:45 GMT"

Related

convert string with utc-datetime to datetime-Ojbect

First of all I am new in C#.
I have to convert a string like
"Fri, 30 Jul 2021 11:57:58 (UTC)"
into a DateTime. I've tried serveral format strings (like "ddd, dd MMM yyyy HH:mm:ss", "r", "R"). But I always get the error message
String was not recognized as a valid DateTime.
Here is my last code for this:
CultureInfo enUS = new CultureInfo("en-US");
string timeStampFormat = "ddd, dd MMM yyyy HH:mm:ss";
DateTime myDateTime;
myDateTime = DateTime.ParseExact(
stringToFormat,
timeStampFormat,
enUS,
DateTimeStyles.AssumeUniversal);
Thanks for your support.
Best regards
Andreas## Heading ##
Assuming that you can have not only (UTC), but (UTC+4), (UTC-5) and alike suffixes, I suggest escaping (UTC and ):
string stringToFormat = "Fri, 30 Jul 2021 11:57:58 (UTC)";
...
DateTime myDateTime = DateTime.ParseExact(
stringToFormat,
new string[] {
"ddd, d MMM yyyy H:m:s '(UTC)'",
"ddd, d MMM yyyy H:m:s '(UTC'z')'",
},
CultureInfo.GetCultureInfo("en-US"),
DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
Demo:
DateTime demo(string text) => DateTime.ParseExact(
text,
new string[] {
"ddd, d MMM yyyy H:m:s '(UTC)'",
"ddd, d MMM yyyy H:m:s '(UTC'z')'",
},
CultureInfo.GetCultureInfo("en-US"),
DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
string[] tests = new string[] {
"Fri, 30 Jul 2021 11:57:58 (UTC)",
"Fri, 30 Jul 2021 11:57:58 (UTC-1)",
"Fri, 30 Jul 2021 11:57:58 (UTC+1)",
"Fri, 30 Jul 2021 11:57:58 (UTC-14)",
};
string report = string.Join(Environment.NewLine, tests
.Select(test => $"{test,-40} => {demo(test):dd.MM.yyyy HH:mm:ss}"));
Console.Write(report);
Outcome:
Fri, 30 Jul 2021 11:57:58 (UTC) => 30.07.2021 11:57:58
Fri, 30 Jul 2021 11:57:58 (UTC-1) => 30.07.2021 12:57:58
Fri, 30 Jul 2021 11:57:58 (UTC+1) => 30.07.2021 10:57:58
Fri, 30 Jul 2021 11:57:58 (UTC-14) => 31.07.2021 01:57:58
It is the (UTC) which is causing the string to not be recognized as a timestamp. You could account for the (UTC) by removing it, I have provided one method to accomplish this here.
string stringToFormat = "Fri, 30 Jul 2021 11:57:58 (UTC)";
string[] SubString = stringToFormat.Split('(');
stringToFormat = SubString[0].Trim();
string timeStampFormat = "ddd, dd MMM yyyy HH:mm:ss";
DateTime mydateTime = DateTime.ParseExact(stringToFormat, timeStampFormat, CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.AssumeUniversal);
Or replace the (UTC) with the offset from UTC, which in this case would be 0, and add (z) to timeStampFormat.
string stringToFormat = "Fri, 30 Jul 2021 11:57:58 (UTC)";
stringToFormat = stringToFormat.Replace("(UTC)", "(-0)");
string timeStampFormat = "ddd, dd MMM yyyy HH:mm:ss (z)";
DateTime mydateTime = DateTime.ParseExact(stringToFormat, timeStampFormat, CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.AssumeUniversal);
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

Convert string to string(datetime) in EST

I have string in "Mon, 20 Mar 2021 14:04:48 +0000"
and I want to convert it as "20 Mar 2021 | 14:04 PM"
I want to convert the string as it is but it was appearing differently in my local and server.
First of all 20 Mar 2021 is Saturday, not Monday, let's correct it. Then you can ParseExact to get DateTime and finally represent it in the required format with a help of ToString():
string source = "Sat, 20 Mar 2021 14:04:48 +0000";
string result = DateTime
.ParseExact(source, "ddd, dd MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture)
.ToUniversalTime()
.ToString("dd MMM yyyy' | 'HH:mm tt", CultureInfo.InvariantCulture);
Notes:
It seems that you want to obtain Universal (not Local) time, that's why I've added ToUniversalTime()
14:04 PM looks strange for me (14:04 and 02:04 PM are much more frequent formats); put hh instead of HH to have 02:04 PM
If you actually want to be manipulating the timezone information, then use Noda Time.
If that's your exact text format, and the dates and times are what you want, you can convert it manually:
var input = "Mon, 20 Mar 2021 14:04:48 +0000";
var dateParts = input.Split(' ');
var timeParts = dateParts[4].Split(':');
var amPm = int.Parse(timeParts[0]) < 12 ? "AM" : "PM";
var output = $"{dateParts[1]} {dateParts[2]} {dateParts[3]} | {timeParts[0]}:{timeParts[1]} {amPm}";
System.Console.WriteLine(output); // "20 Mar 2021 | 14:04 PM"
Or, if you are feel adventurous, use a regular expression.

Data of the converter and time gtm c#

I get a schedule in this format Fri, 07 Apr 2017 09:03:03 GTM. How can I convert to this format 04/07/2017 09:03:03?
I tried to do with the date conversion method
DateTime.Parse(string date);
But that returns 07/04/2017 06:03:03
DateTime.Parse("Fri, 07 Apr 2017 09:03:03 GMT").ToUniversalTime().ToString("MM/dd/yyyy HH:mm:ss");
DateTime dd = Convert.ToDateTime( DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss"));
You just need to specify the format of the date when converting

On converting a string to a DateTime object

Am unable to convert a string which represents date and time ex: "Tue Mar 18 14:37:34 PDT 2014" to a DateTime object. From the format I can figure it out to be in the RFC 1123 format. What is the best way to parse date strings as above?
Timezone literals are not supported by DateTime.Parse/ParseExact. Here is a workaround:
string inputDate = "Tue Mar 18 14:37:34 PDT 2014";
inputDate = inputDate.Replace("PDT", "-7");
DateTime d = DateTime.ParseExact(inputDate, "ddd MMM dd HH:mm:ss z yyyy", culture);
Console.WriteLine(d);
If you can make the format of the string like this (you are pretty close):
Sat, 01 Nov 2008 19:35:00 GMT
You can use DateTime.Parse(dateString);
Find more information here http://msdn.microsoft.com/en-us/library/vstudio/1k1skd40(v=vs.100).aspx

Casting String to DateTime

I have a string in format
Jul 13 2011 1:07PM
I want to cast it as
dd/MM/yyyy HH:mm tt
e.g: 13/7/2011 11:49:00 AM //string=Jul 13 2011 1:07PM
I am using following code to cast it to date.
DateTime date = Convert.ToDateTime(Convert.ToDateTime(myDateString).ToString("dd/MM/yyyy HH:mm:ss"));
This works fine if my day in my string is less than 13
Jul 12 2011 1:07PM //this will cast to desire format fine!
Jul 13 2011 1:07PM //gives error String was not recognized as a valid DateTime.
I understand that it is taking day as month but I can not found a way to cast it to desire format.
See DateTime.ParseExact :
DateTime date = DateTime.ParseExact(myDateString, "MMM dd YYYY H:mmtt", CultureInfo.InvariantCulture);
See also Time Format Strings
You should use DateTime.TryParse
DateTime dt ;
if (DateTime.TryParse("Jul 13 2011 1:07PM",out dt))
MessageBox.Show("Converted to Date object");
Post that you use the ToString() method to get the desired output
dt.ToString("dd/MM/yyyy HH:mm")
First, convert the string Jul 13 2011 1:07PM to a date:
var date = Convert.ToDateTime("Jul 13 2011 1:07PM");
Then, convert it to a string in the format you like:
var dateText = date.ToString("dd/MM/yyyy HH:mm:ss");
I believe you're searching for this:
Date.ParseExact("Jul 13 2011 1:07PM", "MMM d yyyy h:mmtt", Globalization.CultureInfo.InvariantCulture)

Categories

Resources