I am trying to parse the following 09/04/2015 17:22:29.183 PM
The code is as follows:
string s = "09/04/2015 17:22:29.183 PM";
DateTime.ParseExact(s,Constants.DateTimeFormat,System.Globalization.CultureInfo.InvariantCulture);
The DateTimeFormat is of the form dd/MM/yyyy hh:mm:ss.fff tt
However the compiler throws up the error. Where am I going wrong?
hh specifier is for 12-hour clock format which takes 01 to 12, you need to use HH specifier which is for 24-hour clock format which takes 00 to 23.
string s = "09/04/2015 17:22:29.183 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yyyy HH:mm:ss.fff tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 09.04.2015 17:22:29
}
But on the other hand, AM or PM designators are for 12-hour clock format. That's why there is no such a thing as 17 PM or 17 AM.
You need to use HH instead of hh.
Of course, it's a bit weird to use HH and tt at the same time. Are you sure that's what you want to do?
Related
This question already has answers here:
Parse DateTime with time zone of form PST/CEST/UTC/etc
(6 answers)
Closed 6 years ago.
var dateValue = "Mon, 02 May 2016 12:00 PM EDT";
var date = DateTime.ParseExact(
dateValue,
"ddd, dd MMM yyyy hh:mm tt K",
System.Globalization.CultureInfo.InvariantCulture);
As near as I can tell, from the official format string documentation, this should work. Instead, it raises System.FormatException with the rather unhelpful message: String was not recognized as a valid DateTime.
Is there any way to figure out what's going wrong?
The K Custom Format Specifier does not accept time zone strings.
If you can supply the hour offset instead of a string, then you can use "z".
var dateValue = "Mon, 02 May 2016 12:00 PM -4";
var date = DateTime.ParseExact(
dateValue,
"ddd, dd MMM yyyy hh:mm tt z",
System.Globalization.CultureInfo.InvariantCulture);
https://msdn.microsoft.com/en-us/library/shx7s921%28v=vs.110%29.aspx specifies that the DateTime.Kind enumeration has 3 members. So perhaps it does not like you to specify "EDT" as a kind.
Member name Description
Local The time represented is local time.
Unspecified The time represented is not specified as either local time or Coordinated Universal Time (UTC).
Utc The time represented is UTC.
This should work
var dateValue = "Mon, 02 May 2016 12:00 PM EDT".Replace("EDT", "-4");
var date = DateTime.ParseExact(
dateValue,
"ddd, dd MMM yyyy hh:mm tt z",
System.Globalization.CultureInfo.InvariantCulture);
I get this date string from an RSS:
Wed, 16 Dec 2015 17:57:15 +0100
I need to parse into a DateTime. Ive googled and searched stack overflow and gotten to the following answer (ive tried with only one, two and four 'z' instead of three)
string parseFormat = "ddd, dd MMM yyyy HH:mm:ss zzz";
DateTime date = DateTime.ParseExact(dateString, parseFormat,
DateTimeFormatInfo.CurrentInfo,
DateTimeStyles.None);
But I get this error:
System.FormatException: String was not recognized as a valid
DateTime.
Change your code to
string parseFormat = "ddd, dd MMM yyyy HH:mm:ss zzz";
DateTime date = DateTime.ParseExact(dateString, parseFormat,
CultureInfo.InvariantCulture);
Hope it helps!
As commented, your format and string matches unless if your CurrentCulture is english-based one. If it is not, it can't parse these Wed and Dec parts successfully.
On the other hand, zzz format specifier does not recommended for DateTime parsing.
From documentation;
With DateTime values, the "zzz" custom format specifier represents the
signed offset of the local operating system's time zone from UTC,
measured in hours and minutes. It does not reflect the value of an
instance's DateTime.Kind property. For this reason, the "zzz" format
specifier is not recommended for use with DateTime values.
However, I would parse it to DateTimeOffset instead of DateTime since you have an UTC Offset in your string like;
var dateString = "Wed, 16 Dec 2015 17:57:15 +0100";
string parseFormat = "ddd, dd MMM yyyy HH:mm:ss zzz";
DateTimeOffset dto = DateTimeOffset.ParseExact(dateString, parseFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None);
Now, you have a DateTimeOffset as {16.12.2015 17:57:15 +01:00} which as +01:00 Offset part.
I am trying to convert to time from a string
My string are like this "11:45 AM" or "03:19 PM" and i am using
dateTime = DateTime.ParseExact("11:45 AM", "H:mm tt",
System.Globalization.CultureInfo.InvariantCulture);
Then it is getting converted but when i am passing
DateTime.ParseExact("3:19 PM", "H:mm tt",
System.Globalization.CultureInfo.InvariantCulture).ToString();
Getting error as
String was not recognized as a valid DateTime.
I cant understand why it is happening any one have idea then please help me
I would use h instead of H. H is for the 24hr fromat, h for the 12hr format.
DateTime.ParseExact("9:45 PM", "h:mm tt", System.Globalization.CultureInfo.InvariantCulture)
See the full list of format options here.
As you want to parse the 12 hr format and convert it to the 24 hr format then you can just use this
string dt = DateTime.ParseExact("3:19 PM", "h:mm tt",CultureInfo.InvariantCulture).ToString("HH:mm");;
Unfortunately, none of the answers are completely correct.
Ante meridiem and post meridiem belong to the 12-hour clock format. That's why you should never use 24-hour clock format specifiers if your string contains one of them.
That's why you need to use h or hh specifiers, not H or HH. Since your hour part can be with leading zeros, using hh specifier is the best option for both of your string types.
Using the hh:mm tt format will parse your strings successfully.
string s = "03:19 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "hh:mm tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 29.05.2015 15:19:00
}
and
string s = "11:45 AM";
DateTime dt;
if(DateTime.TryParseExact(s, "hh:mm tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 29.05.2015 11:45:00
}
Also, you mentioned the 3:19 PM string in your code example. Since the hour part is single digit, you need to use the h:mm tt format in that case.
Invariant culture requires two-digit hours.
I'm trying to remove hidden characters from a string that represents a date time. I'm using .Net Fiddle and you can see the line that tries to ParseExact fails.
Here is a snippet. Please refer to the fiddle link for working code.
var dateTime = "2015-04-14 07:30:00 PM"; //<= this throws an error from some hidden char
dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", "");
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd HH:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");
The HH in the format string refers to the 24-hour clock hours, which doesn't work when using AM/PM in the format string for PM times.
Change HH to hh.
It's not an invisible character. Your use of HH conflicts with your use of tt. HH is 24 hour time, but you are using tt to interpret PM (12 hour time). Change it to hh and it works.
var dateTime = "2015-04-14 07:30:00 PM";
//dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", ""); <= not needed
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");
You need to change HH to hh.
yyyy-MM-dd hh:mm:ss tt
This is a quick one, i wanna parse a date that comes in this format "Sun May 23 22:00:00 UTC+0300 2010"
Is this a valid UTC DateTime? And how to parse it? I tried :
DateTime newStartTime = DateTime.ParseExact(hdnNewStartTime.Value, "ddd MM dd HH:mm:ss UTC+0300 yyyy", CultureInfo.CurrentCulture);
However, this didn't work, any help appreciated!
DateTime dt = DateTime.ParseExact(s,"ddd MMM dd HH:mm:ss UTCzzzz yyyy", System.Globalization.CultureInfo.InvariantCulture);
Its not a standard format, but you can still parse it.
string format = "ddd mmm dd HH:mm:ss zzzzz yyyy";
string temp = "Sun May 23 22:00:00 UTC+0300 2010";
DateTime time = DateTime.ParseExact(temp, format, CultureInfo.InvariantCulture);
This isn't in a standard .NET format, so you'll probably have to parse it by hand. The UTC+0300 bit indicates the timezone, everything else is part of the date and time.
I tried the solution presented by #johncatfish and it does what I expect. I would presume that you actually want to keep the timezone information.
[Test()]
public void TestCaseWorks ()
{
string format = "ddd MMM dd HH:mm:ss UTCzzzzz yyyy";
string temp = "Sun May 23 22:00:00 UTC+0300 2010";
DateTime time = DateTime.ParseExact(temp, format, CultureInfo.InvariantCulture);
Assert.AreEqual(DayOfWeek.Sunday, time.DayOfWeek);
Assert.AreEqual(5, time.Month);
Assert.AreEqual(23, time.Day);
Assert.AreEqual(0, time.Minute);
Assert.AreEqual(0, time.Second);
Assert.AreEqual(2010, time.Year);
// Below is the only actually useful assert -- making sure the
// timezone was parsed correctly.
// In my case, I am GMT-0700, the target time is GMT+0300 so
// 22 + (-7 - +3) = 12 is the expected answer. It is an exercise
// for the reader to make a robust test that will work in any
// timezone ;).
Assert.AreEqual(12, time.Hour);
}
Sorry for my previous answer which was quite simplistic.
Replace MM by MMM in your date format and it should be fine.
From the example given, it isn't possible to tell if the month should be in the 3 letter form (Jan, Feb, May etc.) or in the full form (January, February, May etc.).
If it should be in the short form, use:
ddd MMM dd HH:mm:ss UTCzzz yyyy
If it should be in the long form, use:
ddd MMMM dd HH:mm:ss UTCzzz yyyy
Details of the formatting specifiers available can be found at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx