Anyname Donotcare
Anyname Donotcare

Reputation: 11393

How to parse data in javascript file through .net?

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 ...

Upvotes: 0

Views: 198

Answers (2)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26199

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

Upvotes: 1

Tigran
Tigran

Reputation: 62248

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

Upvotes: 1

Related Questions