Flex: adding 1 day to a specific date
17,781
Solution 1
Isn't Flex based on ECMA (basically javascript), if so, try just adding 86400000 milliseconds to the date object? Something like:
var mili = 1000;
var secs = 60;
var mins = 60;
var hours = 24;
var day = hours * mins * secs * mili;
var tomorrow = new Date();
var tomorrow.setTime(tomorrow.getTime() + day);
Solution 2
tommorow date arithmetic suggested by @Treby can be uses this way by using Date(year, month, day) constructor :
var now:Date = Date();
var currentDate = now.date;
var currentMonth = now.month;
var currentYear = now.fullYear;
var tomorrow:Date = new Date(currentYear, currentMonth, currentDate + 1);
var lastWeek:Date = new Date(currentYear, currentMonth, currentDate - 7);
var lastMonth:Date = new Date(currentYear, currentMonth-1, currentDate);
etc.
Solution 3
Use:
var date:Date = new Date();
date.setDate(date.getDate() + 1);
As this considers the summer timechange days, when days have 23h or 25h.
Cheers
Solution 4
I took this helper function from this blog post, but it's just a snippet.
The usage is simple:
dateAdd("date", +2); //now plus 2 days
then
static public 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;
break;
default:
/* Unknown date part, do nothing. */
break;
}
return returnDate;
}
Solution 5
or if you are after a fancy solution you can use the library Flex Date Utils http://flexdateutils.riaforge.org/, which has a lot of useful operations
best
![Treby](https://i.stack.imgur.com/VbdSS.jpg?s=256&g=1)
Comments
-
Treby about 2 years
how can i set a date adding 1 day in flex??
-
Treby over 14 yearsnice.. this works.. but this also work tommorow.date = tommorow.date +1 it automatically change the year and month if it reaches the end of each month
-
Ashith over 14 yearsThat is nice. Would adding 24 hours to a date not do that?