C#日期转换格式/ Date(1453154400000)/格式为正常日期(DD / MM / YY)

时间:2018-10-31 05:53:42

标签: c# excel date

我正在将一个excel文件导入一个哈希表,然后将该哈希表作为WebServiceResponse响应的一部分传递给前端以进行显示。某些文件的数据显示,但某些我尝试导入日期的文件显示为 / Date(1453154400000)/。

我正在寻找一种将该格式转换为DD / MM / YYYY日期格式的方法

   public WebServiceResponse ReadFile(string fileName, string inputFormat)
    {
        string filePath = Server.MapPath("~/upload/");

      string filePath = Server.MapPath("~/upload/");

        string strConnection =
            string.Format(
      "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source={0}{1};" 
           + "Extended Properties=\"Excel 
      12.0;HDR=NO;IMEX=1;ImportMixedTypes=Text;\"", filePath, fileName);

        var jsonArray = new List<Hashtable>();


     string. 
        using (var cnConnection = new OleDbConnection(strConnection))
        { 
            cnConnection.Open();

            try
            {

                var cmdSelectSections = new 
       OleDbCommand(string.Format("SELECT * FROM [{0}]", "Sheet1$"), 
           cnConnection);

                OleDbDataReader drdSections = 
             cmdSelectSections.ExecuteReader();

                // Import excel rows into XML files 
                if (drdSections != null)
                {
                    while (drdSections.Read())
                    {

                        var record = new Hashtable();

                        for (int j = 0; j < drdSections.FieldCount; j++)
                        {
     record.Add("Col" + j.ToString(CultureInfo.InvariantCulture), 
                 drdSections[j]);
                        }

                        jsonArray.Add(record);
                        //}
                    }
                }
            }
            finally
            {

                cnConnection.Close();
            }

        }
        var coldef = new string[20]; 
        var numberOfCols = jsonArray[0].Count; 
        var i = 0; 
        var result = new Hashtable
                         {
                             {"colDef", coldef},
                             {"data", jsonArray}
                         };

        return WebServiceResponse.PassResponse(result);
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试

var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var date = epoch.AddMilliseconds(1453154400000).ToString("dd/MM/yy");

希望您能得到正确的时间