flash on 2012-9-13
♥0 |
Line 138 |
Modified 2012-09-13 07:46:31 |
MIT License
archived:2017-03-20 15:36:39
ActionScript3 source code
/**
* Copyright majoraze ( http://wonderfl.net/user/majoraze )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/suO8
*/
package {
import flash.text.TextField;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private var txt:TextField = new TextField();
public function FlashTest() {
// write as3 code here..
txt = new TextField()
txt.text = 'oi';
addChild(txt);
var dateNow:Date = new Date();
var checkDate:String = "12/22/2000";
var dateBirthday:Date = dateStringToObject(checkDate);
trace("dateNow = "+dateNow);
trace("dateBirthday = "+dateBirthday);
calculateAge(dateBirthday)
txt.text = calculateAge(dateBirthday);
}
private function calculateAge(birthdate:Date):String {
var dtNow:Date = new Date();// gets current date
var currentMonth:Number = dtNow.getMonth() + 1 ;
var currentDay:Number = dtNow.getDate();
var currentYear:Number = dtNow.getFullYear();
var bdMonth:Number = birthdate.getMonth() + 1;
var bdDay:Number = birthdate.getDate();
var bdYear:Number = birthdate.getFullYear();
// get the difference in years
var years:Number = dtNow.getFullYear() - birthdate.getFullYear();
var months:Number
var weeks:Number
var days:Number
// subtract another year if we're before the
// birth day in the current year
if (currentMonth < bdMonth || (currentMonth == bdMonth && currentDay < bdMonth)){
months = currentMonth - bdMonth - 1
if(currentDay > bdDay){
months++
days = currentDay - bdDay
weeks = Math.floor(days / 7)
days %=7
}else if(currentDay == bdDay){
months++
days = 0;
weeks = 0
}else{// currentDay is smaller than bdDay
// this is not trivial
// here we calculate the days from the bdDay of the previous month + currentDay
days = getDays(currentMonth - 1, dtNow) - bdDay + currentDay
weeks = Math.floor(days / 7)
days %=7
}
}else if(currentMonth == bdMonth){
if(currentDay > bdDay){
months = 0
days = currentDay - bdDay
weeks = Math.floor(days / 7)
days %=7
}else if(currentDay == bdDay){
months = 0
days = 0
weeks = 0
}else{// currentDay is smaller than bdDay
months = 11
days = getDays(currentMonth - 1, dtNow) - bdDay + currentDay
weeks = Math.floor(days / 7)
days %=7
}
}else{ // this is when the currentMonth is smaller than bdMonth
months = (12 - bdMonth) + (currentMonth - 1)
if(currentDay > bdDay){
months++
days = currentDay - bdDay
weeks = Math.floor(days / 7)
days %=7
}else if(currentDay == bdDay){
months++
days = 0
weeks = 0
}else{// currentDay is smaller than bdDay
days = getDays(currentMonth - 1, dtNow) - bdDay + currentDay
weeks = Math.floor(days / 7)
days %=7
}
}
return years + " years, " + months + " months, " + weeks + " weeks, " + days + " days old" ;
trace ("age:" + years)
trace ("months:" + months)
trace ("weeks:" + weeks)
trace ("days:" + days)
//var time:String = years + " years, " + months + " months, " + weeks + " weeks, " + days + " days old" ;
//time_txt.text = time;
}
private function getDays(previousMonth:Number, nowDate:Date):Number{
var residual:Number = nowDate.getFullYear()%4
trace("Is leap =" + residual)
trace("previousMonth=" + previousMonth )
switch(previousMonth){
case 0://December
return 31
break
case 1://Jaunary
return 31
break
case 2://February
if (residual == 0){
return 29
}else{
return 28
}
break
case 3://March
return 31
break
case 4://April
return 30
break
case 5://May
return 31
break
case 6://June
return 30
break
case 7://Jul
return 31
break
case 8://Aug
return 31
break
case 9://Sep
return 30
break
case 10://Oct
return 31
break
case 11://Nov
return 30
break
}
return 0
}
private function dateStringToObject(dateString:String):Date {
var date_ar:Array = dateString.split("/");
return new Date(date_ar[2],date_ar[0] - 1,date_ar[1]);
}
}
}