Add days to Date in ActionScript

22,478

Solution 1

While the other answers will work im sure, it is as easy as doing:

var dte:Date = new Date();
dte.date += 30; 
//the date property is the day of the month, so on Sept. 15 2009 it will be 15

This will even increment the month if necessary and year as well. You can do this with the month and year properties as well.

Solution 2

I suggest that you look here: How can you save time by using the built in Date class?.

It should be something like this:

var date:Date = new Date();
date.setDate(date.date + 30);

Solution 3

My TimeSpan class might prove useful here (it's a port of the .NET System.TimeSpan):

var now : Date = new Date();
var threeDaysTime : Date = TimeSpan.fromDays(3).add(now);

Solution 4

@Zerata

Adding milliseconds directly will not work if dates are across day light saving change...

However, you can add seconds directly:

var date: Date = new Date(); date.seconds += 86400; => this works even if dates are across DLS change.

Maurice

Solution 5

I'm writing the code from the top of my head, without compiling it, but I'd use getTime(). Something like:

var today : Date = new Date();
var futureDate : Date = new Date();
futureDate.setTime(today.getTime() + (1000 * 60 * 60 * 24 * 30));

1000 * 60 * 60 * 24 * 30 = milliseconds * seconds * minutes * hours * days

Makes sense?

Share:
22,478

Related videos on Youtube

Lea Cohen
Author by

Lea Cohen

Web developer, both server-side and client-side. Started my web career with ASP.NET and now also write in PHP (mainly Wordpress, some Moodle). SOreadytohelp

Updated on March 28, 2020

Comments

  • Lea Cohen
    Lea Cohen about 4 years

    We have an application in which the user has to enter a date who's value is no more than 30 days after the the current date (the date on which the user uses the application). This is a Flash application, therefore I need a way to add 30 days to the current date, and get the right date. Something like in JavaScript:

    myDate.setDate(myDate.getDate()+30);
    

    Or in C#:

    DateTime.Now.Add(30);
    

    Is there such a thing in ActionScript?

  • Virusescu
    Virusescu over 14 years
    date.date - Properties are case sensitive.
  • Ryan Guill
    Ryan Guill over 14 years
    That is true. Also remember that the month property is 0 based. So January is month 0 and December is month 11.
  • Mark
    Mark about 10 years
    Thanks, other examples just added days but didn't increase the month.

Related