all groups > flash actionscript > april 2005 >
You're in the

flash actionscript

group:

Setting the Date in Datefield


Setting the Date in Datefield DaveHCYJ
4/28/2005 12:00:00 AM
flash actionscript: Is there a way to pre-populate a DateField with the current date?

for example something like:
on(load){
this.selectedDate = ???;
}

Secondly, I want my users to be able to select a range of dates. So if there
is a way to pre-populate the datefield with the current date, is there also a
way to pre populate a second DateField with the current date minus 7 days?
Re: Setting the Date in Datefield TimSymons
4/28/2005 12:00:00 AM
When you place the DateField componet onto the stage, in the Properties box
under the Parameters tab this is a Parameter that is labeled "showToday". Set
it to "true" to display the current date.

To set another DateField with the current date minus 7 days, you will need to
create a new instance of Date class. Look in the Help files (under the
ActionScript Language Reference section) to find all of the methods available
for controlling the date.

Tim
Re: Setting the Date in Datefield DaveHCYJ
4/29/2005 12:00:00 AM
Thanks for the responce, but let me clarify my question.

The showToday property simply highlights the current date when the user goes
to enter a date. What I want to do is pre load a value into the DateField so
that the user doesn't even have to click it.

For example with a TextInput I could do:

on(load){
this.text = "default value";
}

Is there a way to get at the value of a DateField??
Re: Setting the Date in Datefield Kaare
4/29/2005 12:00:00 AM
To autopopulate the DateField, you could go with following simple solution:

var oDate = new Date();
var iDate = oDate.getDate()
var iMonth = oDate.getMonth()
var iYear = oDate.getFullYear()
myDF.selectedDate = new Date(iYear, iMonth, iDate);

Where myDF is the instancename of your DateField.
Re: Setting the Date in Datefield Kaare
4/29/2005 12:00:00 AM
I quickly tried out an idea for your other DateField showing 7 days before...

It doesn't look that good, but should be working code. I just can't seem to
find out how to retract amount of days in any month, so that's hard-coded...

var aDaysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
if(iDate-7>0){
myDF2.selectedDate = new Date(iYear, iMonth, (iDate-7));
} else {
if(iMonth-1>=0){
myDF2.selectedDate = new Date(iYear, (iMonth-1),
(aDaysInMonth[(iMonth-1)]+(iDate-7)));
} else {
myDF2.selectedDate = new Date((iYear-1), 11,
(aDaysInMonth[(aDaysInMonth.length-1)]));
}
}

I hope you can use this...
Re: Setting the Date in Datefield DaveHCYJ
4/29/2005 12:00:00 AM
Yes that did work, thanks.

Maybe they will include more static functions for dates at some point like
other languages have.

such as:
var dateInstance = Date.now();
dateInstance = dateInstance - (Date.day * 7);
Re: Setting the Date in Datefield dkid
11/17/2005 12:00:00 AM
sorry, but did this actually work for anyone? it doesn't work for me!

i have tried 100 variations and the only way i can successfully set the value
of selectedDate is by manually inserting a number for the month OR the day of
the month in my new Date statement. i have no idea why this would be the case.

ideas anyone?

s
Re: Setting the Date in Datefield richie l
1/10/2006 5:44:23 AM
Originally posted by: Kaare
I quickly tried out an idea for your other DateField showing 7 days before...

It doesn't look that good, but should be working code. I just can't seem to
find out how to retract amount of days in any month, so that's hard-coded...

var aDaysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
if(iDate-7>0){
myDF2.selectedDate = new Date(iYear, iMonth, (iDate-7));
} else {
if(iMonth-1>=0){
myDF2.selectedDate = new Date(iYear, (iMonth-1),
(aDaysInMonth[(iMonth-1)]+(iDate-7)));
} else {
myDF2.selectedDate = new Date((iYear-1), 11,
(aDaysInMonth[(aDaysInMonth.length-1)]));
}
}

I hope you can use this...

its probably easier just to use the functions that the date object provides,
i.e:

var weekAgo:Date = new Date(); // creates a date object with the current
date
weekAgo.setDate(weekAgo.getDate() -7); // takes 7 days away from it.
myDF.selectedDate = new Date(weekAgo.getYear(), weekAgo.getMonth(),
weekAgo.getDate());

note that the methods setTime(), setDate(), setYear() and setMonth() deal with
the wrapping of months and years and so on...
AddThis Social Bookmark Button