I am getting issue on single line code ;(
TBCurrent.Text = DateTime.Now.Subtract(Current).ToString("HH:mm:ss");
This triggers on timer tick. I've tried already following formats:
"HH\:mm\:ss"
"HH':'mm':'ss"
"HH\":\"mm\":\"ss"
It works if I put just "mm" or "ss", but not even "HH mm ss".
Did you face such error before? Or this is .NET version (4.0) related?
Thank you,
Michael
That is TimeSpan formatting (see link), not DateTime. Try "hh':'mm':'ss".
It is a usual mistake to think TimeSpan formatting is the same as DateTime formatting. Before .NET 4.0, you could not format TimeSpan.
For DateTime there are two ways to write times of day, 20:50 or 8:50 PM. There "H" gives 20, and "h" gives 8. With TimeSpan, that difference is absent and you must always use lowercase h.
This format seems to get the output that you want
TBCurrent.Text = DateTime.Now.Subtract(Current).ToString(#"hh\:mm\:ss");
You need to put the backslashes before the colons because the Subtract method returns a TimeSpan not a DateTime and a TimeSpan has different rules for Custom Format String. And do not forget the # verbatim to escape the backslashes
Related
I was looking in the Microsoft doc's and I can't find any explanation why ParseExact doesn't understand my date.
Could somebody explain why this code throws an exception?
DateTime.ParseExact("6092019", "dMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None)
From the docs: DateTime.ParseExact Method
If format is a custom format pattern that does not include date or
time separators (such as "yyyyMMddHHmm"), use the invariant culture
for the provider parameter and the widest form of each custom format
specifier. For example, if you want to specify hours in the format
pattern, specify the wider form, "HH", instead of the narrower form,
"H".
So in your case you probably should use approach suggested in John's answer - add "missing" zero and parse with wider date format "dd"
The problem here seems to be that d can be a one-digit or two-digit date, so the parser struggles to determine if "2102019" refers to the 2nd of November 2019, or the 21st of... and then it breaks. With delimiters, the parser is able to act more intelligently. It will happily parse "2-10-2019" using "d-MM-yyyy".
My suggested solution to your problem is to pad the string, and change your format string:
string dateToParse = "6092019";
string paddedDateToParse = dateToParse?.PadLeft(8, '0'); // 06092019
DateTime parsedDate = DateTime.ParseExact(paddedDateToParse, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
Try it online
I'm getting a DateTimeOffset string as "2018-10-16T193850+0200", but I think it's none of the standard formats. Mainly, the "+0200" part is not standard, because it lacks the colon.
What format do I have to specify to parse DateTimeOffsets like this? thank you!
You can use ParseExact:
DateTimeOffset offsetDate = DateTimeOffset.ParseExact(
"2018-10-16T193850+0200",
"yyyy-MM-dd'T'HHmmsszzzz",
DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.None);
Read: Custom Date and Time Format Strings
Unfortunately setting DateTimeInfo.TimeSeparator to empty string won't help.
But you can use DateTimeOffset.ParseExact as follows:
DateTimeOffset date = DateTimeOffset.ParseExact("2018-10-16T193850+0200", "yyyy'-'MM'-'dd'T'HH''mm''ss''K", CultureInfo.InvariantCulture.DateTimeFormat);
The trick is to specify 'K' format specifier in order to accept all kinds of offsets. Additionally to the above date string the following will be parsed correctly as well:
"2018-10-16T193850Z" - UTC
"2018-10-16T193850" - local time
EDIT
My answer is similar to Tim Schmelter's one, except by the 'K' part.
I’m trying to parse a time. I’ve seen this question asked/answered here many times but not for this specific scenario. Here’s my code:
var time1 = DateTime.ParseExact("919", "Hmm", CultureInfo.InvariantCulture);
also
var time2 = DateTime.ParseExact("919", "Hmm", null);
both of these throw the same
"String was not recognized as a valid DateTime"
What I want is 9:19 AM.
For further info I also need to parse “1305” as 1:05 PM, this is working fine.
It seems to me I’m using the correct format. What am I overlooking?
I'm not sure there is any format that can handle this. The problem is that "H" can be either one digit or two, so if there are two digits available, it will grab both - in this case parsing it as hour 91, which is clearly invalid.
Ideally, you'd change the format to HHmm - zero-padding the value where appropriate - so "0919" would parse fine. Alternatively, use a colon in the format, to distinguish between the hours and the minutes. I don't believe there's any way of making DateTime parse a value of "919" as you want it to... so you'll need to adjust the string somehow before parsing it. (We don't have enough context to recommend a particular way of doing that.)
Yes, your format is right but since H specifier might be 2 character, ParseExact method try to parse 91 as an hour, which is an invalid hour, that's why you get FormatException in both case.
I connected to microsoft team about this situation 4 months ago. Take a look;
DateTime conversion from string C#
They suggest to use 2 digit form in your string or insert a date separator between them.
var time1 = DateTime.ParseExact("0919", "Hmm", CultureInfo.InvariantCulture);
or
var time1 = DateTime.ParseExact("9:19", "H:mm", CultureInfo.InvariantCulture);
You cant exclude the 0 prefix to the hour. This works
var time1 = DateTime.ParseExact("0919", "Hmm", CultureInfo.InvariantCulture);
Perhaps you want to just prefix 3-character times with a leading zero before parsing.
Much appreciated for all the answers. I don’t have control of the text being created so the simplest solution for me seemed to be prefixing a zero as opposed to adding a colon in the middle.
var text = "919";
var time = DateTime.ParseExact(text.PadLeft(4, '0'), "Hmm", null);
You can specify a custom format for a DateTime object like this:
DateTime.Now.ToString("HH:mm:ss"); // 19:55:23
But when I try to use the same format for a TimeSpan object like this:
DateTime.Now.TimeOfDay.ToString("HH:mm:ss");
I get the "Input string was not in a correct format." exception.
It turns out, the solution is that you need to escape the ':' characters like in "HH\\:mm\\:ss". Note that there is a double backslash because if you specify only one, it will break the string so you need to escape that one too.
The question is, why .NET Framework developers made it this way? There must be a reason for sure. Why can't we use custom format specifiers without escaping them like we can with a DateTime object?
Looking for .NET gurus to shed light on this subject.
As stated in documentation, one of the differences between DateTime.ToString and TimeSpan.ToString format specifiers is the following: the custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals.
In contrast with TimeSpan (see table of format specifiers in docs), DateTime format specifiers include predefined symbols for Date separator /, and for Time separator :. It means that for example for Italian culture semicolon will be recognized as time separator (not the literal) and will be replaced with . symbol:
// outputs 09.57.18 instead of 09:57:18 because of Italian culture.
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss", CultureInfo.GetCultureInfo("it-IT")));
I think .NET designers made such difference between DateTime and TimeSpan string formatters intentionally, and it is quite reasonable. This is because historically Date/Time were formatted differently for different cultures. And .NET tried to provide globalization means for that matter along with DateTime type. But TimeSpan did not get such 'globalization' duties, it is just a type representing period of time, and formatting of it is not tied to any culture specifics (if they are ever existed), but instead formatting of it is constant in different culture settings.
why does this not work?
DateTime.TryParseExact(text, "H", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out value);
I want to parse an Time value only providing the hour part, but it throws a FormatException.
On the other hand, this works:
DateTime.TryParseExact(text, "HH", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out value)
Anybody knows the cause?
Thanks.
Okay, I had to look this one up - it seems like it should be working, but it does not because the custom format string is not valid. A custom format string needs to be at least two characters wide - see:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#UsingSingleSpecifiers
So, according to the documentation, you can fix this by using this code:
DateTime.TryParseExact(text, "%H", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out value);
I guess this means that TryParseExact does not manage to fit the hour part into a single char, and that is understandable enough to me since hour will either be 12 or 24 hour based.
Without more specific information, the DatTime you're constructing can't determine AM / PM given the input. H would only allow a value of 1 - 12, leaving ambiguity. The HH provides the extra info.
The format specifier you pass to DateTime.TryParseExact needs to exactly match the string you are parsing.
E.g. passing "15:20" with format of "H" will fail, because there is other content in the string.
Either parse the whole string and use DateTime.Hour to just get the hour, or create a string with just the hour part and use Int32.Parse.