get Ruby 1.9 Date.parse to assume American date format

By: Johnathon Wright on: March 29, 2011

Ruby 1.9 assumes that everyone has been broken of the bad habit of illogical date formats.

in the console:


Date.parse('2/15/2011') ArgumentError: invalid date

Date.parse('2/15/11') ArgumentError: invalid date

'2/15/2011'.to_date

ArgumentError: invalid date

While I'm driving everyone crazy by writing the date as YYYY-MM-DD, my clients aren't as OCD. They insist on using the American date format mm/dd/yyyy and it's lazy alternate, mm/dd/yy. Here's the code I wrote to help them with their bad habits.

For my rails apps, in config/initializers/americandateformat.rb:

---ruby def Date.parse(value = nil) if value =~ /^(\d{1,2})\/(\d{1,2})\/(\d{2})$/ ::Date.civil($3.toi + 2000, $1.toi, $2.toi) elsif value =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/ ::Date.civil($3.toi, $1.toi, $2.toi) else ::Date.new(*::Date.parse(value, false).valuesat(:year, :mon, :mday)) end

end

Now:


Date.parse('2/15/11') => Tue, 15 Feb 2011

Date.parse('2/15/2011') => Tue, 15 Feb 2011

Date.parse('2011-02-15') => Tue, 15 Feb 2011

'2011-02-15'.to_date => Tue, 15 Feb 2011

'2/4/11'.to_date => Fri, 04 Feb 2011





Comments:

off white hoodie said: I just wanted to construct a small message to be able to express gratitude to you for some of the precious guidelines you are giving out on this site. My time intensive internet lookup has now been compensated with incredibly good tips to talk about with my family members. I would assert that most of us readers actually are really fortunate to live in a wonderful network with many awesome people with very beneficial secrets. I feel rather fortunate to have seen your website page and look forward to some more awesome moments reading here. Thanks once again for a lot of things. off white hoodie [url=http://www.offwhitehoodie.us.com]off white hoodie[/url]

moncler jackets said: My husband and i got glad when Emmanuel could conclude his investigations out of the ideas he discovered out of the blog. It's not at all simplistic just to choose to be freely giving procedures which often the rest might have been trying to sell. And we grasp we now have the writer to appreciate for this. The specific explanations you've made, the easy blog menu, the friendships you aid to promote - it's got all fantastic, and it's leading our son and us reason why that idea is thrilling, and that is pretty pressing. Thank you for the whole lot! moncler jackets

yeezy boost said: I would like to express thanks to the writer just for rescuing me from this type of circumstance. Right after browsing throughout the the net and seeing concepts which were not powerful, I assumed my entire life was over. Existing devoid of the strategies to the problems you've sorted out as a result of your write-up is a critical case, and the kind that could have in a negative way damaged my entire career if I hadn't encountered your web blog. Your personal capability and kindness in dealing with a lot of things was valuable. I'm not sure what I would have done if I had not come across such a point like this. It's possible to at this point look ahead to my future. Thank you very much for your specialized and amazing guide. I won't hesitate to recommend your blog post to anyone who ought to have guide on this problem. yeezy boost

supreme hoodie said: I would like to show my affection for your generosity in support of people that really want help with your study. Your special commitment to getting the message across ended up being amazingly valuable and have in every case empowered regular people just like me to arrive at their pursuits. Your personal informative guidelines means much to me and especially to my peers. Thanks a lot; from each one of us. supreme hoodie

yeezy boost 350 said: I definitely wanted to send a comment so as to say thanks to you for the pleasant steps you are sharing here. My rather long internet look up has now been rewarded with reliable information to talk about with my co-workers. I 'd assume that most of us readers are definitely blessed to dwell in a wonderful place with many marvellous people with very beneficial advice. I feel very grateful to have seen your web site and look forward to really more fun times reading here. Thank you once more for a lot of things. yeezy boost 350

retro jordans said: Thanks so much for giving everyone an extremely memorable chance to read articles and blog posts from this blog. It's usually very excellent and full of a great time for me personally and my office friends to search your website at the least three times in one week to see the new things you will have. And indeed, I am certainly impressed with your tremendous solutions you serve. Some two areas in this article are undeniably the most suitable we have ever had. retro jordans

jordan 13 said: My wife and i have been very thankful Michael could round up his investigation through the entire ideas he got from your web pages. It's not at all simplistic to just possibly be giving for free techniques that other folks have been selling. And we realize we need the website owner to be grateful to because of that. Those explanations you've made, the straightforward site menu, the friendships you can aid to foster - it's everything terrific, and it's really making our son and us reason why the content is cool, which is certainly especially indispensable. Thank you for the whole thing! jordan 13

hermes handbags said: I wish to get across my admiration for your kindness for those people who actually need assistance with in this concept. Your very own commitment to passing the message all over turned out to be incredibly productive and has all the time enabled somebody like me to attain their objectives. Your entire insightful instruction signifies so much a person like me and a whole lot more to my peers. Thank you; from everyone of us. hermes handbags

balenciaga shoes said: I must express my love for your kind-heartedness for those who need assistance with in this issue. Your special dedication to passing the solution all over became surprisingly advantageous and have truly encouraged ladies just like me to realize their pursuits. Your useful publication can mean this much a person like me and substantially more to my peers. Thank you; from everyone of us. balenciaga shoes

moncler said: My wife and i have been now ecstatic Louis managed to deal with his investigation because of the precious recommendations he received when using the web pages. It's not at all simplistic to just possibly be giving out points which people might have been selling. We consider we need the writer to thank for this. The type of illustrations you made, the simple site menu, the friendships your site aid to foster - it's got many fantastic, and it is making our son in addition to the family know that this situation is satisfying, which is certainly wonderfully mandatory. Thanks for the whole thing! moncler

jordan shoes said: I not to mention my pals were found to be reviewing the great tips and hints located on the blog while quickly I had an awful suspicion I never expressed respect to the site owner for those techniques. My boys were definitely so happy to read through all of them and now have extremely been taking pleasure in these things. Many thanks for getting really considerate and for getting varieties of beneficial subjects millions of individuals are really wanting to learn about. Our sincere apologies for not expressing appreciation to you sooner. jordan shoes

curry shoes said: I wish to voice my love for your generosity for men who absolutely need guidance on your topic. Your special dedication to getting the message all through became extremely important and have all the time made somebody just like me to achieve their dreams. Your entire valuable suggestions means a great deal to me and still more to my peers. Thanks a lot; from all of us. curry shoes

nike lebron 15 said: I wish to express my appreciation to the writer for bailing me out of this type of instance. After surfing around through the the net and obtaining solutions which were not powerful, I thought my entire life was over. Existing without the presence of approaches to the problems you have resolved as a result of your main write-up is a serious case, as well as the ones which may have badly damaged my career if I had not discovered your blog. Your main natural talent and kindness in maneuvering every aspect was valuable. I'm not sure what I would've done if I hadn't discovered such a step like this. I am able to at this time look forward to my future. Thanks a lot very much for your expert and sensible guide. I won't hesitate to recommend your blog to anyone who desires care on this matter. nike lebron 15

lebron 16 said: Thanks a lot for giving everyone such a special possiblity to read articles and blog posts from this blog. It's always very enjoyable and as well , packed with fun for me personally and my office peers to visit the blog minimum thrice in one week to read the fresh guidance you have. And of course, I'm at all times amazed concerning the fabulous secrets you serve. Some two ideas in this article are unequivocally the most efficient I have had. lebron 16

jordan retro said: I must express my passion for your generosity in support of persons that absolutely need help with this important question. Your special commitment to getting the message across had become quite advantageous and have surely allowed individuals like me to realize their objectives. This insightful tutorial signifies a great deal to me and extremely more to my peers. With thanks; from all of us. jordan retro

golden goose outlet said: My spouse and i have been very fortunate that Edward could complete his homework from your precious recommendations he grabbed while using the web pages. It's not at all simplistic to just always be giving out guides which often some people may have been selling. And we all keep in mind we have got the writer to thank for this. Most of the illustrations you have made, the straightforward website menu, the friendships you will give support to foster - it's got everything fabulous, and it's letting our son in addition to our family understand this content is excellent, and that is wonderfully fundamental. Thanks for all! golden goose outlet

supreme said: A lot of thanks for each of your hard work on this website. Betty takes pleasure in managing investigation and it's really simple to grasp why. My partner and i hear all relating to the dynamic way you offer vital tips on the blog and even strongly encourage contribution from others on this content so our favorite girl is without question starting to learn a great deal. Take pleasure in the remaining portion of the year. You are always performing a really good job. supreme

kyrie 5 spongebob said: Needed to post you that bit of word to be able to give many thanks once again for your unique suggestions you have discussed on this page. It's quite surprisingly open-handed with people like you to convey unhampered precisely what a few people would have sold as an ebook to help make some bucks for their own end, most importantly considering the fact that you might well have done it if you ever desired. Those strategies in addition served like a easy way to recognize that other people online have the identical interest like mine to understand a great deal more related to this issue. I'm certain there are some more pleasurable occasions up front for those who browse through your blog post. kyrie 5 spongebob

lebron shoes said: I am only writing to let you understand of the magnificent experience our girl went through reading through yuor web blog. She discovered a lot of things, with the inclusion of what it's like to have a great coaching spirit to let other folks just know just exactly chosen impossible matters. You actually exceeded readers' desires. Thanks for delivering the good, trustworthy, revealing as well as unique tips about your topic to Lizeth. lebron shoes

off white hoodie said: I have to show appreciation to the writer for bailing me out of this particular instance. Just after looking out throughout the internet and getting things which were not helpful, I was thinking my life was done. Being alive minus the solutions to the issues you've solved through your entire write-up is a crucial case, and the kind which may have adversely affected my career if I had not come across the blog. Your own talents and kindness in dealing with the whole thing was precious. I am not sure what I would've done if I hadn't discovered such a solution like this. I am able to at this point look ahead to my future. Thanks for your time so much for this skilled and amazing guide. I won't think twice to recommend your blog to any person who desires tips about this issue. off white hoodie

yeezy boost 380 said: I wish to express my appreciation to this writer just for bailing me out of this particular instance. Because of researching through the search engines and obtaining views which were not pleasant, I believed my life was over. Living without the answers to the difficulties you've sorted out by way of this blog post is a serious case, as well as the ones that might have in a negative way affected my career if I hadn't encountered your web blog. That mastery and kindness in taking care of a lot of stuff was crucial. I'm not sure what I would've done if I had not come upon such a stuff like this. I am able to at this time relish my future. Thank you so much for this impressive and results-oriented guide. I won't think twice to endorse your web site to any person who ought to have support about this topic. yeezy boost 380

supreme hoodie said: I intended to create you the little bit of note to finally thank you yet again on the awesome views you have documented on this page. This is simply surprisingly generous with you to convey unhampered just what a few individuals could possibly have distributed as an e-book to make some profit on their own, specifically considering that you could have tried it in the event you wanted. Those tricks additionally worked to provide a great way to realize that other people online have a similar keenness much like mine to find out many more concerning this condition. I am sure there are lots of more pleasant opportunities ahead for individuals who check out your blog. supreme hoodie

supreme shirt said: I'm just commenting to make you be aware of of the fantastic encounter my friend's daughter experienced viewing yuor web blog. She noticed a good number of pieces, with the inclusion of what it's like to possess an amazing coaching heart to get a number of people with no trouble grasp some problematic subject matter. You actually did more than my expected results. Thank you for showing those informative, safe, revealing and as well as fun guidance on your topic to Mary. supreme shirt

kyrie irving shoes said: I enjoy you because of your entire efforts on this web site. Betty really likes setting aside time for investigations and it is obvious why. Most people learn all relating to the compelling mode you provide priceless secrets via the web site and as well boost contribution from other individuals on that concern and our favorite simple princess is actually being taught a whole lot. Have fun with the remaining portion of the year. Your doing a tremendous job. kyrie irving shoes

nike react said: I must show some appreciation to the writer just for bailing me out of such a instance. Because of looking out throughout the world wide web and getting basics that were not beneficial, I assumed my life was gone. Existing minus the solutions to the issues you have sorted out by means of your entire website is a serious case, and the kind which may have adversely damaged my career if I hadn't discovered your web blog. That ability and kindness in handling the whole lot was tremendous. I am not sure what I would've done if I had not discovered such a point like this. I can also at this point look ahead to my future. Thank you very much for your professional and result oriented help. I will not hesitate to suggest your web page to anybody who wants and needs guide about this area. nike react

off white outlet said: Thanks a lot for giving everyone such a breathtaking possiblity to read in detail from this website. It's usually so fantastic and as well , jam-packed with a good time for me personally and my office co-workers to visit your web site no less than 3 times weekly to find out the new guidance you have got. And of course, we're actually fascinated with the beautiful advice you serve. Some 4 tips in this article are particularly the simplest we have ever had. off white outlet

westbrook shoes said: I and also my pals ended up reviewing the great key points located on the website and so instantly developed an awful suspicion I had not expressed respect to the web site owner for those secrets. The people happened to be as a consequence excited to see all of them and have in effect undoubtedly been taking advantage of them. Thanks for getting quite accommodating and also for having some amazing subject matter most people are really desirous to learn about. My personal sincere regret for not expressing appreciation to you earlier. westbrook shoes

hermes online said: Thanks a lot for giving everyone an exceptionally memorable chance to read critical reviews from this site. It is always so good and stuffed with a good time for me and my office friends to visit your website on the least thrice per week to see the fresh items you will have. And definitely, we are usually fulfilled considering the striking solutions you give. Some 3 points in this article are indeed the very best we have had. hermes online

kd 12 shoes said: I wanted to write you the very small observation to finally say thanks a lot once again considering the superb tips you've contributed above. It has been so seriously open-handed of people like you to grant unhampered what exactly a few individuals could possibly have offered for sale for an electronic book to help with making some bucks for their own end, most importantly considering the fact that you could possibly have tried it if you decided. These ideas likewise acted as the great way to comprehend someone else have the same fervor like my own to know whole lot more on the topic of this issue. I believe there are a lot more fun occasions in the future for individuals that looked at your blog. kd 12 shoes

kyrie 4 said: I precisely needed to appreciate you again. I'm not certain what I would've undertaken in the absence of these ways contributed by you on my problem. Entirely was a real intimidating situation for me, however , encountering the very specialised strategy you handled it made me to leap for joy. I'm grateful for your advice and as well , hope you find out what a great job you have been doing training many people through the use of a blog. I am certain you haven't met any of us. kyrie 4

supreme said: Thank you so much for giving everyone an extraordinarily remarkable opportunity to read articles and blog posts from this web site. It's usually so lovely and as well , full of a lot of fun for me personally and my office friends to search your site at the least 3 times per week to find out the new guides you have got. And definitely, I'm so at all times satisfied for the exceptional creative ideas you serve. Selected two facts on this page are rather the simplest I have had. supreme

longchamp said: I am commenting to let you be aware of of the outstanding discovery my princess found viewing your webblog. She mastered a wide variety of things, most notably how it is like to have a very effective teaching spirit to let the others just learn about a number of hard to do topics. You really surpassed our expected results. Thanks for rendering these powerful, dependable, revealing as well as cool thoughts on this topic to Sandra. longchamp
Back