0

How to parse data in a java script file through .net .

I have an url like this :

http://..../xml/en/file_1.js

The data in my .js file is like this :

getdate(20140802,'','','',10,5);
getdate(20140802,'','','',10,5);
getdate(20140727,'','','',10,5);
getdate(20140727,'','','',10,5);
getdate(20140723,'','','',10,5);
getdate(20140723,'','','',10,5);

for example I want to interpret 20140802 to 02 as day and 08 as month and 2014 as a year and so on through the whole file ...

1
  • are you able to get the string "20140802" so that it is easy to parse as you wish. Commented Nov 19, 2013 at 9:38

2 Answers 2

1

Step1: Read All Lines from the JS file.

Step2: from each Line get the substring from ( to next 8 characters(Date is 8 characters length)

Step3: you can Convert the obtained date string into Date Type using ParseExact() method by providing format yyyyMMdd

Complete Solution:

                String [] JSLines=System.IO.File.ReadAllLines("c:\\myfile.js");
                String strDate = "";
                for(int i=0;i<JSLines.Length;i++)
                {                        
                    strDate=JSLines[i].Substring(JSLines[i].LastIndexOf("(")+1,8);               
                    DateTime myDate = DateTime.ParseExact(strDate, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);                               
                }

Output: myDate Contains the Date

1

So basically if you have a current date presented like yyyyMMdd, you can parse it like :

    string d = "20131118"; 
    System.Globalization.CultureInfo provider = 
            System.Globalization.CultureInfo.InvariantCulture;

    DateTime da = DateTime.ParseExact(d, "yyyyMMdd", provider); 

This will result in DateTime instance with

Day :   18
Month : 11
Year:   2013
2
  • thanks but i don't ask basically about that , i ask how to the load .js file and get all the numbers after the first circle bracket through all the file Commented Nov 19, 2013 at 9:47
  • @just_name: can build regular expression, for example with: gskinner.com/RegExr
    – Tigran
    Commented Nov 19, 2013 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.