Convert from Decimal value to DateTime

0

I get values from my DataTable from the database, and the data I get are in decimals example (20160901) which in this case would be September 01, 2016 but I can not convert it to date format with the method Convert.ToDateTime ( );

Does anyone know how I can do it?

I attach part of the code:

  DateTime iDateInicio = Convert.ToDateTime(ds.Tables[1].Rows[0]["DESC"].ToString());
  DateTime iDateFin = DateTime.Now; 

// Here I get the data

            for (DateTime dt = iDateInicio; dt <= iDateFin; dt = dt.AddDays(1))
            {
                if (iMesBandera != dt.Month)
                {
                    iHeaderFinal = iii - 1;
                    e.HeaderRange[Helper.GetColumnNameNew(iHeaderInicial).ToString() + "8:" + Helper.GetColumnNameNew(iHeaderFinal).ToString() + "8"].Cells.Merge();
                    iHeaderInicial = iii;
                    iMesBandera = dt.Month;
                }
                iii++;
            }

// this is the functionality of my journey.

    
asked by Cris Valdez 17.01.2017 в 01:17
source

2 answers

3

You can use DateTime.ParseExact or DateTime.TryParseExact . In your case, it would be something like this:

DateTime iDateInicio = DateTime.ParseExact(ds.Tables[1].Rows[0]["DESC"].ToString(), 
                                       "yyyyMMdd",
                                        System.Globalization.CultureInfo.InvariantCulture);

In any case, if you can modify the structure of the database, it is best to store the dates as DateTime . It does not make much sense to use a numeric data type.

    
answered by 17.01.2017 в 08:59
1

You can also calculate from the numerical value the year, month and day:

 var iDateInicio = new DateTime(n/10000, (n/100)%100, n %100)
    
answered by 17.01.2017 в 20:38