As many of you are aware, I have been complaining about the skill level of the other programmers that I work with. I present this post as evidence supporting my incessant complaints. My apologies to those of you that find this post mind-numbingly boring but it had to be written.
Still here?
If you are … you are a complete nerd.
I’m just sayin’.
Problem:
Write a method that accepts a Date (d) and a Number (n) as parameters. The method should add n hours to Date d and return a string representation of the resulting date. The method must allow for crossing over into new days, years, leap years, etc. (All the normal date mathematics issues). The method must not alter the input Date object (Assumption: All objects are passed by reference).
My Solution:
public static function dateAddHours(date:Date, hours:Number):String
{
var d:Date = clone(date);
d.setHours(d.getHours() + hours);
return toISO8601(d);
}
Their Solution:
public static function dateAddHours(myDate:Date, myHours:Number):String
{
var myYear:Number= myDate.getFullYear();
var myMonth:Number= myDate.getMonth()+1;
var myDay:Number= myDate.getDate();
var myHH:Number= myDate.getHours();
var myMM:Number= myDate.getMinutes();
myHH= myHH+myHours;
trace("hour = " + myHH);
if(myHH==24){
//turn the clock past 12am
myHH=0;
myDay=myDay+1;
//its now the next day, did we got to a new month????
if(myMonth==1 || myMonth == 3 || myMonth == 5 ||myMonth == 7 ||
myMonth == 9||myMonth == 10|| myMonth==12 ){
if (myDay>31){
trace("weve got month");
myDay=1
myMonth=myMonth+1;
if (myMonth>12){
myMonth=1;
//it is now the next year
myYear=myYear+1
}
}
}else{
//CHECK FOR February in A LEAP YEAR
if (myMonth=2){
if (myYear==2008||myYear==2012||
myYear==2016||myYear==2020){
if(myDay>29){
myDay=1;
myMonth=myMonth+1;
}
}else{
if(myDay>28){
myDay=1;
myMonth=myMonth+1;
}
}
}
//then it is just 30 days in the month
else{
if(myDay>30){
myDay=1;
myMonth=myMonth+1;
}
}
}
}
if(myMM>0 &&myMM<30){myMM=0};
if(myMM>30 &&myMM<60){
myMM=30;
}
var myMonthF = format(String(myMonth));
var myDayF = format(String(myDay));
var myHF = format(String(myHH));
var myMF = format(String(myMM));
var theDate:String=""+myYear+"-"+myMonthF+"-"+myDayF+" "+myHF+":"+myMF+":00";
return theDate;
}
Need I say more?