DateTime.TryParseExact does not process long format strings - asp.net

I have to try and parse out a date time from a string that is causing some trouble.
Most of the time the string will be as follows
'Sat Aug 10 08:01:37 2013'
^ note one space
My original date format for the TryParseExact was 'ddd MMM d HH:mm:ss yyyy'
However, on single digit days, what would normally be the digit in the ten's place is not deleted, but instead it is replaced by a space (see below)
'Wed Aug 7 08:01:37 2013'
^^ note the two spaces
My first idea was to simply remove all the spaces to achieve the format string:
'dddMMMdHH:mm:ssyyyy'
but my DateTime.TryParseExact fails and never accepts the newly formatted (spaceless) format string. Why?

Because with TryParseExact, the spacing of the date is important.
It's a bit hacky, but if no better solution is presented, I would do a TryParseExact using 'ddd MMM d HH:mm:ss yyyy' and if that fails, another TryParseExact using 'ddd MMM d HH:mm:ss yyyy' (with the two spaces)
Edit
I think I found a smarter way, pass DateTimeStyles.AllowInnerWhite to the TryParseExact method.
According to the docs:
Extra white-space characters in the middle of the string must be
ignored during parsing, except if they occur in the DateTimeFormatInfo
format patterns.
Not sure what it means by "except if they occur in the DateTimeFormatInfo format patterns." but it is worth a try.

Related

Apache Nifi Expression Language - toDate formatting

I am trying to format a date string using the Apache Nifi expression language and the Replace Text processor(regex). Given a date string
date_str : "2018-12-05T11:44:39.717+01:00",
I wish to convert this to:
correct_mod_date_str: "2018-12-05 10:44:39.717",
(notice how the date is converted to UTC, and character 'T' replaced by a space.)
To do this, I am currently using:
toDate("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"):format("yyyy-MM-dd HH:mm:ss.SSS", '+00:00')
and this works perfectly.
However, when the date string has 6 digits in ms, rather than 3, things break:
another_date_str: "2018-12-05T11:44:39.717456+01:00"
is converted to:
incorrect_mod_date_str: "2018-12-05 10:56:36.456"
It seems the first 3 digits in the ms precision interferes with the conversion.
Appreciate inputs to resolve this - what am I missing?
Regards
seems that's a limitation in java.
according to java documentation there is no support of more then 3 milliseconds digits.
https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
the simplest way is to remove extra digits like this:
attr:replaceAll('(\.\d{3})\d*','$1'):toDate("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"):format("yyyy-MM-dd HH:mm:ss.SSS", '+00:00')
I ran into a similar issue with date time encoded in ISO 8601. The problem is, that the digits after the second are defined as fragment of a second, not milliseconds.
See answer to related topic

Format hours, minutes and seconds with moment.js

I get this value from my backend service: 171054. It represents hh:mm:ss. But when I use the formatting options from the docs it gives me back 00:00:00.
Things I've tried:
moment('171054').format('hh-mm-ss')
moment('171054').format('HH-mm-ss')
moment('171054').format('HH-MM-SS')
You are confusing format option with parsing option. Since your input string is not in ISO 8601 format, you have to specify format when parsing.
Here a working example for your use case:
var mom = moment('171054', 'HHmmss');
console.log(mom.format());
console.log(mom.format('HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
I am not sure if momentjs can read that as date since there is no identifier. I would suggest changing the value for example you have 171054, get each 2 digits since you sure that this is represent as hh:mm:ss then add identifier between then like ":" or "-" then try us momentjs formatting again.

Understanding Escape characters in Unicode LDML Date Format for extended ISO 8601

While using the following control https://openui5.hana.ondemand.com/#docs/api/symbols/sap.m.DatePicker.html#setValueFormat
Supported format options are pattern-based on Unicode LDML Date Format
notation. http://unicode.org/reports/tr35/#Date_Field_Symbol_Table
<DatePicker id="date" value="{/bound/value}" valueFormat="yyyy-MM-dd'T'HH:mm:ss.SSS'Z" displayFormat="MMMM d, y"change="handleChange"/>
In the pattern, the T is escaped with ' on either side.
According to https://www.w3.org/TR/NOTE-datetime, "T" appears literally in the string, to indicate the beginning of the time element, as specified in ISO 8601.
I am confused about the usage pattern for escaping the Z at the end.
Regardless of how I escape the Z with '
on both sides valueFormat="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
or only in the beginning, valueFormat="yyyy-MM-dd'T'HH:mm:ss.SSS'Z",
I still get the correct value.
What is the correct usage here? I would think 'Z' would be more consistent with how 'T' is being escaped.
The Z is not a placeholder like the T. It is a designation of the timezone. If you look in the file you linked it refers to the pattern TZD to represent the designation of the timezone. This may be +1:00 or +11:00 or it may be just Z for Zulu or UTC.
Hopefully that clears it up.
valueFormat="yyyy-MM-dd'T'HH:mm:ss.SSSTZD" is what you are looking for.

ASP.NET "String was not recognized as a valid DateTime."

First off, I realize there's a million pages discussing this already. I have looked at least a hundred of them but cannot seem to make this work. My date and time is presented as a string, compiled from javascript to grab client's local time. It is formatted like this: 7/11/2015 8:34 PM.
I currently have:
Dim datetimeformated = DateTime.ParseExact(lblDateTime.Text, "MM/dd/yyyy HH:mm tt", CultureInfo.InvariantCulture)
I have tried many different variants, but this should be correct I think, yet it does not work. Any help is greatly appreciated. TIA
The correct format for your case is: M/dd/yyyy h:mm tt, and perhaps even, M/d/yyyy h:mm tt, if you can have the day of the month as a single digit.
Explanation: Why your format string didn't work.
MM: means that you must always have 2 digits for the month, clearly not the case in your example.
dd: again, means that you must always have 2 digits for the day of the month. Is that the case? Adjust the parameter if needed.
HH: This actually means that you are expecting the hour value as 2-digits using the 24-hour clock (00-23), which is clearly wrong on both accounts. You can have a single digit, and you are not using the 24-hour clock, because you are using the AM/PM designator.
Relevant documentation link: Custom Date and Time Format Strings.

DateTime format conversion to string ignores AM/PM

I am trying to convert DateTime object to string using formatting but due to some strange reason, it ignores AM/PM. It would just take the magnitude of the Hour in the object.
I am using the following format:
StartDate.ToString("yyyy-MM-dd hh:mm:ss.fff");
and
String.Format("0:yyyy-MM-dd hh:mm:ss.fff", StartDate);
I don't think that there is a difference between the two, but just wanted to give it a try. If I pass a value 4/28/2012 6:00:00 AM or 4/28/2012 6:00:00 PM, the result is the same "2012-04-27 06:00:00.000"
You've used hh which uses the 12-hour clock. You want HH which uses the 24-hour clock.
See MSDN for more details about custom format strings.
Note that you may wish to specify the invariant culture, unless you really want the time separator to depend on the current culture:
string formatted = StartDate.ToString("yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture);
(Note that you shouldn't have the braces if you're passing the format string to ToString. I'll assume this was just a typo in the question.)
If you want to use the 12-hour clock, use tt in the format string to produce the AM/PM designator.

Resources