- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Sometimes I wonder why date conversion/formatting seems to be such a special case for special cases
Admin
Of course TRWTF is M/D/Y convention, even worse is MM-DD-YY. Obligatory xkcd-reference: http://www.xkcd.com/1179/
Admin
And just after I implemented ConvertCatInscribedDate() :(
Admin
soo much easier to first collect all DateFormats and then loop over them
for(DateFormat format: dateFormats){ try{ return format.parse(dateString); } catch(ParseException e) { } } System.err.println("couldn't parse date: "+dateString); return null;
Admin
This code is so broken and senseless, if it was better documented I would have guessed it's from the original date handling API in Java.
Admin
date handling really shouldn't be all that difficult. the only time you should ever need to process a date without knowing how it's represented is when a user initially types it in. in that case you tell him what date formats are acceptable and immediately feed back to him the date you've interpreted from what he typed in. in all other cases, you should know how the date is represented and throw an exception if it's not in the correct format.
Admin
It's like the cherry on the top, or in this case on the bottom, of the sundae.
Admin
Admin
In the end, it's because the year is not evenly divisible by the day, the month is not evenly divisible by the day, and the year is not evenly divisible by the month.
And don't get me started on calculating Easter.
Admin
ratchet freak almost got it right in post 2, but he actually needs dateFormatsAndSuffixes
Admin
That code should have used a regex. Then it would only have two problems!
Admin
This java needs moar xml, it's not enterprisey enough!
Admin
Admin
1st of Febuary 2014,
is written 01/02/2014 in most of the world, your code is broken already!
Admin
try this, it´s another ideia, with new java date style
Admin
Admin
You must not have ever needed to deal with it. There are just so many wonderful formats available, it's always fun and games even when you use the builtin functions properly.
Admin
Other places in Asia use more traditional date systems (like Buddhist Era years) or were influenced strongly by European colonisation and trade, so they tend to have a DD/MM/YYYY format.
Admin
Ha! I lived in rural Japan and most people not only used the Imperial calendar on official documents, they actually thought in terms of imperial years and converted to A.D. when needed.
Drove me nuts.
Admin
Admin
Just another place where a good set of unit tests would have revealed the problems inherent in this code; and would have enabled [someone else] to refactor it properly.
And totally ignoring the actual logic, who thinks that lots of nested try/catch is good? The overhead of all these try/catch must really slow down the program.
And I love the code -- just keep tacking on date parts to the original date until something works. Wow.
And how would you ever localize this -- as other posters have said, the display string for dates varies from locale to locale; so the parse method better be able to understand what locale the date string is in.
Admin
Not sure if "01 02 03" is Jan 2nd, 2003; Feb 1st 2003; March 1st, 2002; or a sequence of zero padded numbers.
Let's go with the first type it parses as.
Admin
This function is really WTF. It doesn't support actual date format used for centuries. DD.MM.YYYY, I mean.
Admin
Not a problem. You tell the user that the accepted format is M/D/YYYY, and they can figure out what they should put in.
Admin
Even this awful function is fine, compared to the über-WTF that is MDY dates!
Screw you yankees! Choose either little or big endian, not bittle endian!
Admin
The trouble is with the format 2013-02-27 is that when you build such a string in Javascript it evaluates it as a mathematical expression and you get (for the above instance) 1984.
Similar problem with the format 2013/02/27.
ISO notwithwstanding, I prefer (and recommend to my peers and mentees) 20130227 precisely because of this.
Admin
TRWTF, of course, is the Java calendar / date system, in which the months go from 0 to 11. Catches me out every time, that does.
Admin
"bittle endian" -- love it. Sounds like a Lisa Simpsonism.
Admin
You mean Middle-endian used on the PDP-11 (making it commonly known as PDP-endian). It's essentially little endian, but because the PDP-11 operates on 16-bit words not 8-bit bytes, it makes for some rather confusing endian was when you are first learning about endianness.
CAPTCHA: incassum; just incassum thing were to come up where you'd need to know about this.
Admin
it's technically Native Endian
Admin
CAPTCHA: valetudo... absolutely NOT! Not this!
Admin
Admin
Admin
Admin
Why is date functionality so difficult? Seriously three scenarios:
Pick a date format and enforce only that format. MM/DD/YYYY for example.
Support multiple formats with proper functions to convert to each of them. I like the idea of converting to a universal format behind the scenes and redisplaying as the requested format as needed.
Allow users to pick a format and use it only for that user. Behind the scenes is a universal format that can be dynamically loaded into each persons style. But allow for users to define their own formats, or request a format be defined. Eventually you'll run out of ways to display a date and have all your bases covered. Now you have a new date manager program you can leverage for business synergy with top agencies.
The easiest solution for date conversion is to have a decoding function that removes all formats and brings the date to a common universal format internally. Then a recoding format that takes that universal date and can easily redisplay it.
Admin
Simply because Dates follows very few set rules.
The calendar has changed over the centuries and different ways to write dates, which in same cases are syntactically identical but where the different numbers represent different date parts depending on in which locale you run the code, makes date handling a chore.
I remember 10 years ago I designed a simple cargo tracking application for an import company.
The shipping office filled in shipping info and the local office verified it on reception.
Despite being within the same company we found out they wrote the dates in 5 different ways :P.
We ended up adding a date picker to prevent them from entering the wrong format.
Admin
All applications I write that require datetime input with a date picker or calendar widget. I do not understand why anyone would attempt to interact with dates in any other way.
Admin
Admin
software didn't exist before 1999
Admin
This. And then be aware that the same is valid for countries. You must not identify a certain country with one format, there can occur multiple formats in use in the same country. So always let the user choose a date format, don't rely on his country setting/choice.
Admin
What are you doing with dates to make that happen in JavaScript? Are you running them through an eval or something?
Admin
I was wondering the same thing.
Message from webpage 2013-02-27
Admin
Oh really? And what about legacy applications that contain date information that was originally entered by users? One guy 10 years ago typed in "01/29/1985" and another guy typed in "01-29-1985" while a third did "01-29-85" and a fourth did "Jan 29, 1985". At the time, it didn't matter cause it was just a field on a table somewhere, but now the manual process that used that field is being automated so the system needs to read and parse those dates. You gonna throw up a UI element for that?
Admin
You get an unpaid intern to manually update these entries, after building a quick form that only accepts valid values.
Admin
Request: "Please enter the date". Answer: "Today"
Not so hard if you ask me. In all the other cases, you get a bunch of selection boxen for year, month, date, and I find scrolling back to my birthday tedious, but it DOES work. Hopefully they don't enter April 31 or February 30, but flinging an error will solve that.
Once it is in the program, convert it to something nice, string, numerics, whatever, and you should be good to go. Thankfully most languages have some sort of internal <-> external conversions. Use them!
Admin
You must listen to Brian Regan.
Admin
Each time I see people doing Date processing not in "yyyy/MM/dd" (or "yyyy/MM' in case you don't need the day part) format for input, and then stuck in some problem caused by it, I can't help but laugh at them.
Admin
Just want to add that yes, using hyphen is also acceptable. the idea is year should go before month and month should go before day.
Admin
Admin
Hmm... something's going wrong then, and it's non-trivial. Let me go and check my codebase again ... we could have implemented a WTF.