I’m using the above function to parse a date from string. The format has both the numeric date and also the 3 letter Day of the week. This function seems to ignore the 3 letter day of the week and will only parse from the numeric date. My problem is that I’m extracting this datestamp using vision kit so sometimes it decodes improperly where the 3 letter date is sort of acting as a checksum but if the function just ignores it I’m ultimately parsing incorrect dates. I just wonder if a strictness option can be added?
DateFormatter.date(from: String) strictness
I just wonder if a strictness option can be added?
I’m not sure what you mean by this:
-
Are you asking if Apple can add a ‘loose parsing’ option to the
DateFormatter
API? -
Or are you asking if that option already exists?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
I guess I’m asking both. And maybe even option 3 being some advice on why it might be unnecessary and i can just do it myself? If the option exists and I’m missing it then please point that out. If not I guess I’m asking if it’s possible for apple to add such an option. As I’m thinking about it now I guess I could potentially take the resulting date and run it back through dateformatter.string(from: date) and then see if the 2 strings match.
just to be clear what I start with is a string like “2022-11-17 Mon 00:00:00” (Monday not being the corresponding day of the week for that date) and when I use the date(from: String) I’ll end up with a date of 11/17/2022 (Thursday) so perhaps my solution is just using the same dateformatter to convert back to string in the same format and I’ll get “2022-11-17 Thu 00:00:00” and that won’t match so I guess I can implement my own strictness check after the fact. Sometimes it takes talking through an issue to discover another solution. Still just found it strange that the function ignored a part of the input though. Maybe intentionally because the day of the week isn’t required to determine the actual date.
There are two kinds of date strings in the world:
-
Fixed-format dates. These use Latin digits, the Gregorian calendar, and English for the textual components.
-
Localised dates. These may use non-Latin digits, other calendars, and other languages.
DateFormatter
is primarily focus on the latter, but the example you posted kinda looks like the former.
So, what kind of date string are you working with?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
i believe I’m working with fixed format ones but the formats vary widely. Basically my app is using vision kit to detect a date/time in a video frame found in all kinds of surveillance systems. I’m attempting to support every variation seen in the wild.
the timestamp format that sparked this forum post was the one mentioned in my previous response where visionkit makes a mistake on the ocr of a digit in the numeric date where maybe a 23 is read as 28 instead to the point where the date and the redundant day of the week also found in the timestamp no longer coordinate.
Oh, interesting.
In this situation I’d avoid DateFormatter
entirely. Rather, write your own parser to extract the Gregorian date components from the string returned to you by your VisionKit code, and then pass those date components to the date(from:)
method to create your Date
value.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"