Wednesday, January 12, 2011

Adding date in Flex using Flex datepart

By using Datepart in flex we can increase or decrease the particular part of the date in flex  , Gareth suggested a dateAdd method to do this date addition in simple steps, using datepart we can add or subtract particular part of the date easily.
By using datepart, adding 1 to datepart["date"] we get the next day date,datepart["week"] we get the next week date.

Source Code :

public static function dateAdd(datepart:String = "", number:Number = 0, date:Date = null):Date
        if (date == null) {
            date = new Date();

        var returnDate:Date = new Date(date.time);;

        switch (datepart.toLowerCase()) {
            case "fullyear":
            case "month":
            case "date":
            case "hours":
            case "minutes":
            case "seconds":
            case "milliseconds":
                returnDate[datepart] += number;
                /* Unknown date part, do nothing. */
        return returnDate;

with this method
dateAdd("date",-1) return the yesterday's date,
dateAdd("week",1) will return next week date
dateAdd( DateUtils.YEAT, 2) will add two years to the current date.

Bookmark and Share


Post a Comment

  © Blogger templates by updated with tips

Back to TOP