473,804 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting string into a numeric date value

16 New Member
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:

Expand|Select|Wrap|Line Numbers
  1.             public function createDate(s:String):String
  2.             {
  3.                 var retStr:String = '';
  4.                 var str2xml:XML = new XML(s);
  5.                 var xml2list:XMLList = new XMLList(str2xml);
  6.                 var list2col:XMLListCollection = new XMLListCollection(xml2list);
  7.  
  8.                 var mods:XMLList = list2col.children().child("modified");
  9.  
  10.                 var mod:String = list2col.children().child("modified").text();
  11.                 for each(var h:XML in mods)
  12.                 {
  13.                     var xml2str:String = h;
  14.                     trace("list2col"+list2col.children().child("modified").text());
  15.                     switch(xml2str.substr(0,4))
  16.                     {
  17.                         case "Jan ":
  18.                         xml2str=xml2str.replace("Jan ","01");
  19.                         xml2str=xml2str.replace(" ","");
  20.                         xml2str=xml2str.replace(",","");
  21.                         xml2str=xml2str.replace(" ","");
  22.                         xml2str=xml2str.substr(0,8);
  23.                         retStr = xml2str;
  24.                         break;
  25.                         case "Feb ":
  26.                         xml2str=xml2str.replace("Feb ","01");
  27.                         xml2str=xml2str.replace(" ","");
  28.                         xml2str=xml2str.replace(",","");
  29.                         xml2str=xml2str.replace(" ","");
  30.                         xml2str=xml2str.substr(0,8);
  31.                         retStr = xml2str;
  32.                         break;
  33.                         case "Mar ":
  34.                         xml2str=xml2str.replace("Mar ","01");
  35.                         xml2str=xml2str.replace(" ","");
  36.                         xml2str=xml2str.replace(",","");
  37.                         xml2str=xml2str.replace(" ","");
  38.                         xml2str=xml2str.substr(0,8);
  39.                         retStr = xml2str;
  40.                         break;
  41.                         case "Apr ":
  42.                         xml2str=xml2str.replace("Apr ","01");
  43.                         xml2str=xml2str.replace(" ","");
  44.                         xml2str=xml2str.replace(",","");
  45.                         xml2str=xml2str.replace(" ","");
  46.                         xml2str=xml2str.substr(0,8);
  47.                         retStr = xml2str;
  48.                         break;
  49.                         case "May ":
  50.                         xml2str=xml2str.replace("May ","01");
  51.                         xml2str=xml2str.replace(" ","");
  52.                         xml2str=xml2str.replace(",","");
  53.                         xml2str=xml2str.replace(" ","");
  54.                         xml2str=xml2str.substr(0,8);
  55.                         retStr = xml2str;
  56.                         break;
  57.                         case "Jun ":
  58.                         xml2str=xml2str.replace("Jun ","01");
  59.                         xml2str=xml2str.replace(" ","");
  60.                         xml2str=xml2str.replace(",","");
  61.                         xml2str=xml2str.replace(" ","");
  62.                         xml2str=xml2str.substr(0,8);
  63.                         retStr = xml2str;
  64.                         break;
  65.                         case "Jul ":
  66.                         xml2str=xml2str.replace("Jul ","01");
  67.                         xml2str=xml2str.replace(" ","");
  68.                         xml2str=xml2str.replace(",","");
  69.                         xml2str=xml2str.replace(" ","");
  70.                         xml2str=xml2str.substr(0,8);
  71.                         retStr = xml2str;
  72.                         break;
  73.                         case "Aug ":
  74.                         xml2str=xml2str.replace("Aug ","01");
  75.                         xml2str=xml2str.replace(" ","");
  76.                         xml2str=xml2str.replace(",","");
  77.                         xml2str=xml2str.replace(" ","");
  78.                         xml2str=xml2str.substr(0,8);
  79.                         retStr = xml2str;
  80.                         break;
  81.                         case "Sep ":
  82.                         xml2str=xml2str.replace("Sep ","01");
  83.                         xml2str=xml2str.replace(" ","");
  84.                         xml2str=xml2str.replace(",","");
  85.                         xml2str=xml2str.replace(" ","");
  86.                         xml2str=xml2str.substr(0,8);
  87.                         retStr = xml2str;
  88.                         break;
  89.                         case "Oct ":
  90.                         xml2str=xml2str.replace("Oct ","01");
  91.                         xml2str=xml2str.replace(" ","");
  92.                         xml2str=xml2str.replace(",","");
  93.                         xml2str=xml2str.replace(" ","");
  94.                         xml2str=xml2str.substr(0,8);
  95.                         retStr = xml2str;
  96.                         break;
  97.                         case "Nov ":
  98.                         xml2str=xml2str.replace("Nov ","01");
  99.                         xml2str=xml2str.replace(" ","");
  100.                         xml2str=xml2str.replace(",","");
  101.                         xml2str=xml2str.replace(" ","");
  102.                         xml2str=xml2str.substr(0,8);
  103.                         retStr = xml2str;
  104.                         break;
  105.                         case "Dec ":
  106.                         xml2str=xml2str.replace("Dec ","01");
  107.                         xml2str=xml2str.replace(" ","");
  108.                         xml2str=xml2str.replace(",","");
  109.                         xml2str=xml2str.replace(" ","");
  110.                         xml2str=xml2str.substr(0,8);
  111.                         retStr = xml2str;
  112.                         break;
  113.                         default:
  114.                         // do nothing
  115.                     }
  116.                     trace("retStr"+retStr);
  117.                 }
  118.                 return retStr;
  119.             }
  120.  
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
Nov 15 '08 #1
3 3283
joedeene
583 Contributor
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
Nov 15 '08 #2
kronus
16 New Member
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:
Expand|Select|Wrap|Line Numbers
  1.             public function createDateModified(s:String):XML
  2.             {
  3.                 var i:int = 0;
  4.                 var retStr:String = '';
  5.                 var str2xml:XML = new XML(s);
  6.                 var xml2list:XMLList = new XMLList(str2xml);
  7.                 var list2col:XMLListCollection = new XMLListCollection(xml2list);
  8.  
  9.                 var mods:XMLList = list2col.children().child("modified");
  10.  
  11.                 var mod:String = list2col.children().child("modified").text();
  12.                 for each(var h:XML in str2xml.children().child("modified"))
  13.                 {
  14.                     var someStr:String = str2xml.children().modified[i].text();
  15.                     switch(someStr.substr(0,4))
  16.                     {
  17.                         case "Jan ":
  18.                         if(someStr.substr(6,1) != ",")
  19.                         {
  20.                             someStr=someStr.replace("Jan ","010");                            
  21.                         }
  22.                         else
  23.                         {
  24.                             someStr=someStr.replace("Jan ","01");
  25.                         }
  26.                         someStr=someStr.replace(" ","");
  27.                         someStr=someStr.replace(",","");
  28.                         someStr=someStr.replace(" ","");
  29.                         someStr=someStr.substr(0,8);
  30.                         str2xml.children().modified[i] = someStr;
  31.                         i++;
  32.                         break;
  33.                         case "Feb ":
  34.                         if(someStr.substr(6,1) != ",")
  35.                         {
  36.                             someStr=someStr.replace("Feb ","020");                            
  37.                         }
  38.                         else
  39.                         {
  40.                             someStr=someStr.replace("Feb ","02");
  41.                         }
  42.                         someStr=someStr.replace(" ","");
  43.                         someStr=someStr.replace(",","");
  44.                         someStr=someStr.replace(" ","");
  45.                         someStr=someStr.substr(0,8);
  46.                         str2xml.children().modified[i] = someStr;
  47.                         i++;
  48.                         break;
  49.                         case "Mar ":
  50.                         if(someStr.substr(6,1) != ",")
  51.                         {
  52.                             someStr=someStr.replace("Mar ","030");                            
  53.                         }
  54.                         else
  55.                         {
  56.                             someStr=someStr.replace("Mar ","03");
  57.                         }
  58.                         someStr=someStr.replace(" ","");
  59.                         someStr=someStr.replace(",","");
  60.                         someStr=someStr.replace(" ","");
  61.                         someStr=someStr.substr(0,8);
  62.                         str2xml.children().modified[i] = someStr;
  63.                         i++;
  64.                         break;
  65.                         case "Apr ":
  66.                         if(someStr.substr(6,1) != ",")
  67.                         {
  68.                             someStr=someStr.replace("Apr ","040");                            
  69.                         }
  70.                         else
  71.                         {
  72.                             someStr=someStr.replace("Apr ","04");
  73.                         }
  74.                         someStr=someStr.replace(" ","");
  75.                         someStr=someStr.replace(",","");
  76.                         someStr=someStr.replace(" ","");
  77.                         someStr=someStr.substr(0,8);
  78.                         str2xml.children().modified[i] = someStr;
  79.                         i++;
  80.                         break;
  81.                         case "May ":
  82.                         if(someStr.substr(6,1) != ",")
  83.                         {
  84.                             someStr=someStr.replace("May ","050");                            
  85.                         }
  86.                         else
  87.                         {
  88.                             someStr=someStr.replace("May ","05");
  89.                         }
  90.                         someStr=someStr.replace(" ","");
  91.                         someStr=someStr.replace(",","");
  92.                         someStr=someStr.replace(" ","");
  93.                         someStr=someStr.substr(0,8);
  94.                         str2xml.children().modified[i] = someStr;
  95.                         i++;
  96.                         break;
  97.                         case "Jun ":
  98.                         if(someStr.substr(6,1) != ",")
  99.                         {
  100.                             someStr=someStr.replace("Jun ","060");                            
  101.                         }
  102.                         else
  103.                         {
  104.                             someStr=someStr.replace("Jun ","06");
  105.                         }
  106.                         someStr=someStr.replace(" ","");
  107.                         someStr=someStr.replace(",","");
  108.                         someStr=someStr.replace(" ","");
  109.                         someStr=someStr.substr(0,8);
  110.                         str2xml.children().modified[i] = someStr;
  111.                         i++;
  112.                         break;
  113.                         case "Jul ":
  114.                         if(someStr.substr(6,1) != ",")
  115.                         {
  116.                             someStr=someStr.replace("Jul ","070");                            
  117.                         }
  118.                         else
  119.                         {
  120.                             someStr=someStr.replace("Jul ","07");
  121.                         }
  122.                         someStr=someStr.replace(" ","");
  123.                         someStr=someStr.replace(",","");
  124.                         someStr=someStr.replace(" ","");
  125.                         someStr=someStr.substr(0,8);
  126.                         str2xml.children().modified[i] = someStr;
  127.                         i++;
  128.                         break;
  129.                         case "Aug ":
  130.                         if(someStr.substr(6,1) != ",")
  131.                         {
  132.                             someStr=someStr.replace("Aug ","080");                            
  133.                         }
  134.                         else
  135.                         {
  136.                             someStr=someStr.replace("Aug ","08");
  137.                         }
  138.                         someStr=someStr.replace(" ","");
  139.                         someStr=someStr.replace(",","");
  140.                         someStr=someStr.replace(" ","");
  141.                         someStr=someStr.substr(0,8);
  142.                         str2xml.children().modified[i] = someStr;
  143.                         i++;
  144.                         break;
  145.                         case "Sep ":
  146.                         if(someStr.substr(6,1) != ",")
  147.                         {
  148.                             someStr=someStr.replace("Sep ","090");                            
  149.                         }
  150.                         else
  151.                         {
  152.                             someStr=someStr.replace("Sep ","09");
  153.                         }
  154.                         someStr=someStr.replace(" ","");
  155.                         someStr=someStr.replace(",","");
  156.                         someStr=someStr.replace(" ","");
  157.                         someStr=someStr.substr(0,8);
  158.                         str2xml.children().modified[i] = someStr;
  159.                         i++;
  160.                         break;
  161.                         case "Oct ":
  162.                         if(someStr.substr(6,1) != ",")
  163.                         {
  164.                             someStr=someStr.replace("Oct ","100");                            
  165.                         }
  166.                         else
  167.                         {
  168.                             someStr=someStr.replace("Oct ","10");
  169.                         }
  170.                         someStr=someStr.replace(" ","");
  171.                         someStr=someStr.replace(",","");
  172.                         someStr=someStr.replace(" ","");
  173.                         someStr=someStr.substr(0,8);
  174.                         str2xml.children().modified[i] = someStr;
  175.                         i++;
  176.                         break;
  177.                         case "Nov ":
  178.                         trace("comma"+someStr.substr(6,1));
  179.                         if(someStr.substr(6,1) != ",")
  180.                         {
  181.                             someStr=someStr.replace("Nov ","110");                            
  182.                         }
  183.                         else
  184.                         {
  185.                             someStr=someStr.replace("Nov ","11");
  186.                         }
  187.                         someStr=someStr.replace(" ","");
  188.                         someStr=someStr.replace(",","");
  189.                         someStr=someStr.replace(" ","");
  190.                         someStr=someStr.substr(0,8);
  191.                         str2xml.children().modified[i] = someStr;
  192.                         i++;
  193.                         break;
  194.                         case "Dec ":
  195.                         if(someStr.substr(6,1) != ",")
  196.                         {
  197.                             someStr=someStr.replace("Dec ","120");                            
  198.                         }
  199.                         else
  200.                         {
  201.                             someStr=someStr.replace("Dec ","12");
  202.                         }
  203.                         someStr=someStr.replace(" ","");
  204.                         someStr=someStr.replace(",","");
  205.                         someStr=someStr.replace(" ","");
  206.                         someStr=someStr.substr(0,8);
  207.                         str2xml.children().modified[i] = someStr;
  208.                         i++;
  209.                         break;
  210.                         default:
  211.                         // do nothing
  212.                     }
  213.                     //trace("retStr"+retStr);
  214.                 }
  215.                 return str2xml;
  216.             }
  217.  
Nov 16 '08 #3
joedeene
583 Contributor
So, you fixed the problem? Because that was a lot of code, haha. I just skimmed it.

joedeene
Nov 16 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2654
by: Ramakrishnan Nagarajan | last post by:
Hi, I am converting Excel data into a Dataset in C#. There are around 24 columns in the Excel Sheet. First I tried to insert one row with correct values in the Excel sheet. i.e. for text columns I entered text values and for numeric columns I entered numeric values. It works fine and pass through all the validation checks and gets inserted into the database successfully. But when I gave some junk values in the excel sheet and tried to...
12
3201
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
12
2614
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for example: 21/05/2006
5
2323
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
2
4011
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The functions strtol, strtod, and strtoul, defined in <cstdlib>, convert a null- terminated character string to a long int, double, or unsigned long. You can use them to convert numeric strings of any base to a numeric
15
9672
by: allthecoolkidshaveone | last post by:
I want to convert a string representation of a number ("1234") to an int, with overflow and underflow checking. Essentially, I'm looking for a strtol() that converts int instead of long. The problem with strtol() is that a number that fits into a long might be too big for an int. sscanf() doesn't seem to do the over/underflow checking. atoi(), of course, doesn't do any checking. I've long thought it odd that there aren't strtoi() and...
2
11187
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") pArray is a variant array containing a date string at pArray(4, iRecord) in the format "yyyy/mm/dd"
3
7163
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying to autoformat the contents of text entries in a GUI. For numbers, I'm converting the text representation to the appropriate type (using atoi, atof, ...) and converting the result back to text with the correct format (using sprintf). But this does...
1
1722
by: PW | last post by:
Hi, When I run the following command, some fields are ending up blank when the clearly have values them in Excel. I have tried converting the columns to general and text. The ones that are ending up blank (Null, actually) contain: lu24769884l lu24769884xxl
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.