Converting string into a numeric date value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kronus
    New Member
    • May 2008
    • 16

    Converting string into a numeric date value

    I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm trying to sort them by the date modified field.

    So what's the problem, right?

    Well, obviously Sep 14, 2008 is before Nov 14, 2008, but when doing a sort process, ActionScript will come back with Nov coming before Sep, because N comes before S in any sort.

    So I figured that I would convert the string to a number and then later specify that this is a numeric sort.

    Here's the code that traverses through the XML and changes the string to numbers:

    Code:
    			public function createDate(s:String):String
    			{
    				var retStr:String = '';
    				var str2xml:XML = new XML(s);
    				var xml2list:XMLList = new XMLList(str2xml);
    				var list2col:XMLListCollection = new XMLListCollection(xml2list);
    
    				var mods:XMLList = list2col.children().child("modified");
    
    				var mod:String = list2col.children().child("modified").text();
    				for each(var h:XML in mods)
    				{
    					var xml2str:String = h;
    					trace("list2col"+list2col.children().child("modified").text());
    					switch(xml2str.substr(0,4))
    					{
    						case "Jan ":
    						xml2str=xml2str.replace("Jan ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Feb ":
    						xml2str=xml2str.replace("Feb ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Mar ":
    						xml2str=xml2str.replace("Mar ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Apr ":
    						xml2str=xml2str.replace("Apr ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "May ":
    						xml2str=xml2str.replace("May ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Jun ":
    						xml2str=xml2str.replace("Jun ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Jul ":
    						xml2str=xml2str.replace("Jul ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Aug ":
    						xml2str=xml2str.replace("Aug ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Sep ":
    						xml2str=xml2str.replace("Sep ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Oct ":
    						xml2str=xml2str.replace("Oct ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Nov ":
    						xml2str=xml2str.replace("Nov ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						case "Dec ":
    						xml2str=xml2str.replace("Dec ","01");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.replace(",","");
    						xml2str=xml2str.replace(" ","");
    						xml2str=xml2str.substr(0,8);
    						retStr = xml2str;
    						break;
    						default:
    						// do nothing
    					}
    					trace("retStr"+retStr);
    				}
    				return retStr;
    			}
    No problem, right? This definitely works, but that is not where my issue lies.

    How do I re-insert the converted values in the exact same place as where it was located when I received the xml?

    Or do I have to grab every object and reassemble it?

    I'm hoping there is an easier way and, as usual, your help is appreciated and thanx n advance
  • joedeene
    Contributor
    • Jul 2008
    • 579

    #2
    I'm not an expert with integrating XML and Flash, but could you like create a new instance of the XML file and copy all of the data from the XML file you opened into that instance. Therefore, whenever you change anything, you'd change the second instance, leaving the originally XML file, unchanged?

    If worse comes to worse, you might have to "reassemble " the XML file...

    joedeene

    Comment

    • kronus
      New Member
      • May 2008
      • 16

      #3
      Thanx 4 d idea of treating it as one object/instance.

      After making my replaces, then it was time to return the whole xml object:
      Code:
      			public function createDateModified(s:String):XML
      			{
      				var i:int = 0;
      				var retStr:String = '';
      				var str2xml:XML = new XML(s);
      				var xml2list:XMLList = new XMLList(str2xml);
      				var list2col:XMLListCollection = new XMLListCollection(xml2list);
      
      				var mods:XMLList = list2col.children().child("modified");
      
      				var mod:String = list2col.children().child("modified").text();
      				for each(var h:XML in str2xml.children().child("modified"))
      				{
      					var someStr:String = str2xml.children().modified[i].text();
      					switch(someStr.substr(0,4))
      					{
      						case "Jan ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Jan ","010");							
      						}
      						else
      						{
      							someStr=someStr.replace("Jan ","01");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Feb ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Feb ","020");							
      						}
      						else
      						{
      							someStr=someStr.replace("Feb ","02");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Mar ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Mar ","030");							
      						}
      						else
      						{
      							someStr=someStr.replace("Mar ","03");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Apr ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Apr ","040");							
      						}
      						else
      						{
      							someStr=someStr.replace("Apr ","04");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "May ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("May ","050");							
      						}
      						else
      						{
      							someStr=someStr.replace("May ","05");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Jun ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Jun ","060");							
      						}
      						else
      						{
      							someStr=someStr.replace("Jun ","06");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Jul ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Jul ","070");							
      						}
      						else
      						{
      							someStr=someStr.replace("Jul ","07");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Aug ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Aug ","080");							
      						}
      						else
      						{
      							someStr=someStr.replace("Aug ","08");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Sep ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Sep ","090");							
      						}
      						else
      						{
      							someStr=someStr.replace("Sep ","09");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Oct ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Oct ","100");							
      						}
      						else
      						{
      							someStr=someStr.replace("Oct ","10");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Nov ":
      						trace("comma"+someStr.substr(6,1));
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Nov ","110");							
      						}
      						else
      						{
      							someStr=someStr.replace("Nov ","11");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						case "Dec ":
      						if(someStr.substr(6,1) != ",")
      						{
      							someStr=someStr.replace("Dec ","120");							
      						}
      						else
      						{
      							someStr=someStr.replace("Dec ","12");
      						}
      						someStr=someStr.replace(" ","");
      						someStr=someStr.replace(",","");
      						someStr=someStr.replace(" ","");
      						someStr=someStr.substr(0,8);
      						str2xml.children().modified[i] = someStr;
      						i++;
      						break;
      						default:
      						// do nothing
      					}
      					//trace("retStr"+retStr);
      				}
      				return str2xml;
      			}

      Comment

      • joedeene
        Contributor
        • Jul 2008
        • 579

        #4
        So, you fixed the problem? Because that was a lot of code, haha. I just skimmed it.

        joedeene

        Comment

        Working...