November 15th, 2005

Update to Date Formatter

UPDATE: I guess I didn't think this one through completely. If you are using a component such as the DateChooser component or other class that returns a Date Object, using the Date2 Class will be of no use. So using the DateFormatter static class makes far more sense. Thanks Darron for the heads up on this one.

The other day I updated Darron Schalls dateFormat.as into a static class. Not sure why I just didn't extend the Date class.

Anyhow.. I've made the quick change. You can download the Date2 Class here along with a sample FLA [LINK]

Sample code:

Actionscript:

  1. import Date2;
  2.  
  3. var now:Date2 = new Date2();
  4.  
  5. trace(now.formatTo("mm-dd-yyyy"));
  6. trace(now.formatTo("mmmm d, yyyy"));
  7. trace(now.formatTo("mm/dd/yyyy"));
  8. trace(now.formatTo("d-mmm-yyyy"));
  9. trace(now.formatTo("ddd, mmmm dd, yyyy"));
  10. trace(now.formatTo("short"));
  11. trace(now.formatTo("medium"));
  12. trace(now.formatTo("long"));
  13. trace(now.formatTo("full"));

4 Responses to “Update to Date Formatter”

  • You shouldn’t extend the Date class because you lose the ability to format regular Date instances.

    Imagine if you are using a thrid party library that returns a date:

    var dt:Date = Library.returnSomeCoolDate();

    How would you format it now? You’d have to convert the date to a “Date2″ instnace, which is extra work that isn’t necessary. Using a static utility method is really best practice here…

    And by the way, you should’ve just asked me for an AS2 version of my DateFormat code — I’ve been using it for over a year now:

    DateUtil.format( date, formatString )

    :-)

  • Agreed. I didn’t think that one through. If I was using something like the DateChooser component or other library as you mentioned which returns a Date Object, extending the Date Class would not be of much use.

    So the static Class makes more sense. Thanks Darron.

    Are you providing your AS2 version publically available?

  • Nah, I don’t have a download link to it on my website. My DateUtil class is part of a class library that I tend to re-use a lot in the apps I build. When people ask me for it I have no problem giving it to them though.

    As you’re aware, it’s pretty trivial to “port” the AS1 code to AS2. Essentially just make the method static and wrap it in a class and you’re good to go. I’ll have to post a comment in my original entry to point people to the AS2 version, though I would prefer if it was in the com.darronschall.util namespace.

  • If you update your post, let me know and I’ll modify my blog here to point to your post and your AS2 version.