Flex Date Utils – isDate
2 short methods again today, but one I find is very useful (and the first one that got me started on my date utils library)
/** * Determines whether a value is actually a valid date * * @param value The date value * * @return <code>true</code> means this is a valid date, <code>false</code> means it is not a valid date */ public static function isDate( value:String ):Boolean { return Date.parse( value ) > 0; }
My isDate method will check whether the value that is passed to it is actually a date or not. I was surprised to find this wasn’t included somewhere in the Flex framework, but quite happy at how easy it was to create it. All that needs to be done is to check whether the date can be parsed by the Date class. If yes, it’s a date, if not, it is not a date.
To implement, first import the package:
import com.flexoop.utilities.dateutils.DateUtils;
then:
private var _validDate:Boolean = DateUtils.isDate( 'this is not a date' );
Straight forward and simple
/** * Converts the month to a Flex month * * @param date The human readable month * * @return The Flex converted month or 0 aka January */ public static function toFlexMonth( localMonth:Number ):Number { return ( localMonth > 0 && localMonth < 13 ) ? localMonth - 1 : 0; }
This method is another one that I created to try to make my life easier when thinking about months, so I didn’t get confused about using 0-11 instead of 1-12. I actually use another method I created instead of this one now, but I thought I should leave it in as it may be useful to someone, even if I don’t use it that much.
This accepts a month, 1-12, and returns the flex value for that month, 0-11.
Once again, to use, import the package:
import com.flexoop.utilities.dateutils.DateUtils;
and then:
private var _newMonth:Number = DateUtils.toFlexMonth( 6 );
And this would return 5. Nothing awe inspiring in that method, but perhaps useful to someone.
All methods in this series are describing code in the FlexDateUtils package I put on Google code.
Nice Util class mate, Would be cool if you could hook the resource files into the holidays so you could get en_us = US holidays etc
Thanks, and you read my mind
Right after I made the Holiday class, I realized that it was (mostly) only useful to a US audience.
I’m hoping that when I get a chance, I’ll be able to do exactly as you say and hook it to the resource files in some fashion. The only other thing I thought of was that perhaps someone would need access to the US holidays, but might not be accessing them from the US. I haven’t looked into it too much, but I wouldn’t want to limit who had access to the certain files, unless the classes were still available to developers if needed.
Anyway, food for thought.