如何通过.net解析javascript文件中的数据

How to parse data in javascript file through .net?

本文关键字:数据 文件 javascript 何通过 net 解析      更新时间:2023-09-26

如何通过.net解析java脚本文件中的数据。

我有一个这样的网址:

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

我的.js文件中的数据如下:

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

例如我想通过整个文件来解释20140802to 02 as day and 08 as month and 2014 as a year等等。。。

步骤1:JS文件读取所有行。

步骤2:从每行获取(next 8字符的子字符串(日期为8个字符长)

步骤3:您可以通过提供格式yyyyMMdd ,使用ParseExact()方法将获得的date string转换为Date类型

完整解决方案:

                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);                               
                }

输出:myDate包含Date

因此,基本上,如果您有一个像yyyyMMdd这样的当前日期,您可以像这样解析它

    string d = "20131118"; 
    System.Globalization.CultureInfo provider = 
            System.Globalization.CultureInfo.InvariantCulture;
    DateTime da = DateTime.ParseExact(d, "yyyyMMdd", provider); 

这将导致带有的DateTime实例

Day :   18
Month : 11
Year:   2013