0

l am importing an excel file into a hashtable then passing the hashtable to the front end as part of a WebServiceResponse response for display. Data displays for some files but for some files that l try to import the date is shown as /Date(1453154400000)/.

am looking for a way to convert this format to DD/MM/YYYY date format

   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);
    }
Prince
  • 43
  • 9
  • 1
    What date does `1234656000000` represent? – mjwills Oct 31 '18 at 05:54
  • 1
    duplicate: https://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c – jazb Oct 31 '18 at 05:56
  • 1
    /Date(1453154400000)/ is 01/19/2016 on the spread sheet – Prince Oct 31 '18 at 06:41
  • is 1453154400000 long int value? – Md. Abdul Alim Oct 31 '18 at 06:45
  • @Prince where did that text come from? `/Date(1453154400000)/` is the very old and abandoned JSON date format introduced by Microsoft back when ther was no convention about JSON dates. Does your service return that text or does *Excel* contain that text? What is `WebServiceResponse` and what does `PassResponse` do? – Panagiotis Kanavos Nov 08 '18 at 15:04
  • @Prince if the value is formatted this way by your service you'll have to configure it so it *doesn't* do that. One option would be to *not* use hashtables, use JSON.NET directly and construct a JObject instead. When serialized to a string it will produce properly formatted ISO8601 dates. You could create an anonymous type and serialize it to JSON with `JsonConvert.SerializeObject` instead of building a hashtable too – Panagiotis Kanavos Nov 08 '18 at 15:07

1 Answers1

1

You can try with this

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

I hope you will get your correct time

Md. Abdul Alim
  • 707
  • 1
  • 6
  • 19
  • `DateTimeOffset.FromUnixTimeMilliseconds(1453154400000)` would also work and saves the manual construction/calculation. However, the OP still has a format of `/Date(x)/` where they need to extract the `x`. While that part should be trivial, I'm not sure if it's obvious to the OP. A complete answer would include that step – pinkfloydx33 Oct 31 '18 at 08:58