This is not a blog. So sue me!


Crikey, things are looking up!

Thursday, November 15, 2007

getDateString: ActionScript 2.0 function to make a readable numeric date string

The getDateString returns the present date/time in the form of a readable (just) string down to milliseconds e.g. 20061231135901999. It uses the leftFill function below:

//get the current date/time and make a string
//in the form yyyymmddHHMMSSmmm
private function getDateString():String
{
var today:Date = new Date();
var dateString:String = today.getFullYear().toString()
+ leftFill((today.getMonth()+1).toString(),"0",2)
+ leftFill(today.getDate().toString(),"0",2)
+ leftFill(today.getHours().toString(),"0",2)
+ leftFill(today.getMinutes().toString(),"0",2)
+ leftFill(today.getSeconds().toString(),"0",2)
+ leftFill(today.getMilliseconds().toString(),"0",3);
return dateString;

}//getDateString

// This function will take a string (1-3 characters)
// and right justify it with fill characters padded to the left for the width required.
// Designed for use with Date/time fields 1, 2 or 3 in length
// if it is called on the wrong size inString it will return the correct size field of fill chars
private function leftFill(inString:String, fillChar:String, fieldWidth:Number):String
{
//make sure the fill character is only one character long!
var fill = fillChar.substr(0,1);
if (inString.length == fieldWidth)
{
return inString;
}
else if (inString.length == fieldWidth-1)
{
return fill + inString;
}
else if (inString.length == fieldWidth-2)
{
return fill + fill + inString;
}
else
{
return ((fill + fill + fill).substr(0,fieldWidth));
}
}//leftFill

No comments:

Contributors

St Lawrence Rowing

Test content from SLRC