      */
     public static function dateToString(value:Date, outputFormat:String):String
     {
-        if (!value)
-            return "";
-
+		var maskChar:String;
+		var maskParts:Array = [];
+		var part:int = 0;
+		var length:int = outputFormat.length;
+		
+		if (!value || isNaN(value.getTime()))
+			return "";
+		
+		length = outputFormat.length;
+		part = -1;
+
+		var lastChar:String;
+		
+		for (var i:int = 0; i < length; i++)
+		{
+			maskChar = outputFormat.charAt(i);
+			
+			if (maskChar == "Y" || maskChar == "M" || maskChar == "D")
+			{
+				if (maskChar != lastChar)
+				{
+					part++;
+					maskParts[part] = "";
+				}
+				maskParts[part] += maskChar;
+			}
+			else {
+				part++;
+				maskParts[part] = maskChar;
+			}
+			
+			lastChar = maskChar;
+		}
+		
         var date:String = String(value.getDate());
-        if (date.length < 2)
-            date = "0" + date;
-
         var month:String = String(value.getMonth() + 1);
-        if (month.length < 2)
-            month = "0" + month;
-
         var year:String = String(value.getFullYear());
 
-        var output:String = "";
         var mask:String;
-
-        // outputFormat will be null if there are no resources.
-        var n:int = outputFormat != null ? outputFormat.length : 0;
-        for (var i:int = 0; i < n; i++)
-        {
-            mask = outputFormat.charAt(i);
-
-            if (mask == "M")
-            {
-                if ( outputFormat.charAt(i+1) == "/" && value.getMonth() < 9 ) {
-                    output += month.substring(1) + "/";
-                } else {
-                    output += month;
-                }
-                i++;    
-            }
-            else if (mask == "D")
-            {
-                if ( outputFormat.charAt(i+1) == "/" && value.getDate() < 10 ) {
-                    output += date.substring(1) + "/";
-                } else {    
-                    output += date;                         
-                }
-                i++;
-            }
-            else if (mask == "Y")
-            {
-                if (outputFormat.charAt(i+2) == "Y")
-                {
-                    output += year;
-                    i += 3;
-                }
-                else
-                {
-                    output += year.substring(2,4);
-                    i++;
-                }
-            }
+		var maskLength:int
+		var output:String = "";
+
+        length = maskParts.length;
+        for (i = 0; i < length; i++)
+        {
+            mask = maskParts[i].charAt(0);
+			maskLength = maskParts[i].length;
+			
+			if (mask == "D")
+			{
+				if (maskLength > 1 && date.length == 1)
+					date = "0" + date;
+				
+				output += date;
+			}
+			else if (mask == "M")
+			{
+				if (maskLength > 1 && month.length == 1)
+					month = "0" + month;
+				
+				output += month;
+			}
+			else if (mask == "Y")
+			{
+				if (maskLength == 2)
+					output += year.substr(2,2);
+				else
+					output += year;
+			}
             else
             {
                 output += mask;

     */
    public static function stringToDate(valueString:String, inputFormat:String):Date
    {
        var mask:String
        var temp:String;
        var dateString:String = "";
        var monthString:String = "";
        var yearString:String = "";
        var j:int = 0;

        var n:int = inputFormat.length;
        for (var i:int = 0; i < n; i++,j++)
        {
            temp = "" + valueString.charAt(j);
            mask = "" + inputFormat.charAt(i);

            if (mask == "M")
            {
                if (isNaN(Number(temp)) || temp == " ")
                    j--;
                else
                    monthString += temp;
            }
            else if (mask == "D")
            {
                if (isNaN(Number(temp)) || temp == " ")
                    j--;
                else
                    dateString += temp;
            }
            else if (mask == "Y")
            {
                yearString += temp;
            }
            else if (!isNaN(Number(temp)) && temp != " ")
            {
                return null;
            }
        }

        temp = "" + valueString.charAt(inputFormat.length - i + j);
        if (!(temp == "") && (temp != " "))
            return null;

        var monthNum:Number = Number(monthString);
        var dayNum:Number = Number(dateString);
        var yearNum:Number = Number(yearString);

        if (isNaN(yearNum) || isNaN(monthNum) || isNaN(dayNum))
            return null;

        if (yearString.length == 2 && yearNum < 70)
            yearNum+=2000;

        var newDate:Date = new Date(yearNum, monthNum - 1, dayNum);


     *  object corresponding to the String.
     *  The <code>inputFormat</code> argument contains the pattern
     *  in which the <code>valueString</code> String is formatted.
     *  It can contain <code>"M"</code>,<code>"MM"</code>,<code>"D"</code>,
	 *  <code>"DD"</code>,<code>"YY"</code>,<code>"YYYY"</code>
     *  and delimiter and punctuation characters.
     *
     *  <p>The function does not check for the validity of the Date object.
