PDA

View Full Version : DateField component to Mysql


skalie
07-02-2004, 08:54 AM
In the interests of prosperity.....

I've had a hell of a time trying to work out how to format the info from a DateField component to throw into a MySql database (without binding it to another component).

There was nothing on the net to be found, so I wrote this code ( in this case to be attached to an instance of a DateField ).


on (change) {
var displayedDate:Date = new Date();
var displayedDate:Date = this.selectedDate;
var day:Number = displayedDate.getDate();
var month:Number = displayedDate.getMonth();
var year:Number = displayedDate.getUTCFullYear();
if (day<10) {
var ad1:Number = 0;
} else {
var ad1:String = "";
}
if (month<10) {
var ad2:Number = 0;
} else {
var ad2:String = "";
}
trace("day :"+day);
trace("month :"+month);
trace("year :"+year);
trace(year+"-"+ad2+month+"-"+ad1+day);
}


Comments or improvements welcomed, hope this helps someone out one day

starev1l
07-26-2004, 03:25 AM
:D Great! This is just what I need!

However, some notes to point out...
getMonth() returns an interger depicting the Months in a "sort-of array".
eg, 0 is actually January, 1 is actually February, etc.

So, it should be...
var month:Number = displayedDate.getMonth() + 1;

And I don't really understand the rationale behind the use of getUTCFullYear midway suddenly when all the while you're using local date/time, but if you're to try it on eg. 1 Jan of 2004, your current code will output year : 2003 instead of the 2004 in the positive (GMT +HH:MM) timezones. So, to play it safe, either use getUTCwhatever for everything or simply base it on your local timezone where flash & mySQL resides.

To anyone that finds this useful and wants to learn more about Date specific issues, try here (http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary153.html).

petefs
07-27-2004, 03:35 AM
and a handy way to go backwards...


String.prototype.mysqlDatetimeToDate = function ()
{
var dt= this.split(" ");
var ymd = dt[0].split("-");
var hms = dt[1].split(":");
return (new Date(ymd[0],Number(ymd[1])-1,ymd[2],hms[0],hms[1],hms[2]));
};

kaquna
04-03-2009, 09:50 AM
hello everyone.
(sorry, but I am spanish and my english may not be very good ... )

I found this post by chance and, given my meager knowledge of actionscript, could someone explain me how to use the prototype.mysqlDatetimeToDate?
thank you very much.