A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Date Object

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Posts
    996

    Date Object

    I have a situation where I have a date in this format.
    Code:
    var currentDate : String = "09/22/09"
    and I want to be able to change this dynamically to the next date.
    like you had currentDate +1
    The reason for this is the user selects this date but I want another field to display the next date after this.

    I thought about using the date object to take care of this for me but I'm not sure that will work.
    If I pass the date object this format it returns.
    newDate = Wed Sep 22 00:00:00 GMT-0500 1909

    I could start stripping out the currentDate and adding in lots of if statements for the month year and date and figuring out leap years but I thought the date object could solve all this.

    Anyone have any ideas?

    Thanks

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Use Date.parse to get a number of milliseconds representing that date. Add the number of milliseconds in a day to that, and use setTime on a new Date. That Date object will then represent your new Date. If you need it in a String, you can use toDateString. I'm not sure if there's a way to format that other than the default. You could always use getMonth, getDate and getYear to build your string.

  3. #3
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    Code:
    var currentDate:String = "04/30/09";
    function getNextDate(d:String):String {
    	var temp:Array = d.split("/");
    	var date:Date = new Date(int((temp[2].length==2?"20":"")+temp[2]), int(temp[0])+1, int(temp[1]));
    	date.setDate(date.getDate()+1);
    	//date.
    	var newMonth:Number = date.getMonth()-1;
    	var newDay:Number = date.getDate();
    	var newYear:Number = date.getFullYear() - 2000;
    	return ((newMonth>9?newMonth:"0"+newMonth)+"/"+(newDay>9?newDay:"0"+newDay)+"/"+(newYear>9?newYear:"0"+newYear));
    }
    trace(getNextDate(currentDate));
    New sig soon

  4. #4
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    Oops, little mistake, should probably test code in flash before posting!!!
    Code:
    var currentDate:String = "03/31/09";
    function getNextDate(d:String):String {
    	var temp:Array = d.split("/");
    	var date:Date = new Date(int((temp[2].length==2?"20":"")+temp[2]), int(temp[0])-1, int(temp[1]));
    	date.setDate(date.getDate()+1);
    	//date.
    	var newMonth:Number = date.getMonth()+1;
    	var newDay:Number = date.getDate();
    	var newYear:Number = date.getFullYear() - 2000;
    	return ((newMonth>9?newMonth:"0"+newMonth)+"/"+(newDay>9?newDay:"0"+newDay)+"/"+(newYear>9?newYear:"0"+newYear));
    }
    trace(getNextDate(currentDate));
    New sig soon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center