Monday, June 14, 2010

Converts an Excel serial date to DateTime.

      

Converts an Excel serial date to DateTime

 

 

 

       /// <summary>

       /// converts an Excel serial date to DateTime

       /// </summary>

       /// <param name="nSerialDate"></param>

       /// <returns></returns>

 

 

 static  DateTime ExcelSerialToDateTime(int nSerialDate)

        {

            return (new DateTime(1900, 1, 1)).AddDays(nSerialDate - 2);

        }

 

===Or====

 

        static  DateTime ExcelSerialDateToDT(int nSerialDate)

        {

            int l = nSerialDate + 68569 + 2415019;

            int n = ((4 * l) / 146097);

            l = l - ((146097 * n + 3) / 4);

            int i = ((4000 * (l + 1)) / 1461001);

            l = l - ((1461 * i) / 4) + 31;

            int j = ((80 * l) / 2447);

            int nDay = l - ((2447 * j) / 80);

            l = (j / 11);

            int nMonth = j + 2 - (12 * l);

            int nYear = 100 * (n - 49) + i + l;

 

 

            return DateTime.Parse(nMonth + "/" + nDay + "/" + nYear);

        }

No comments:

Post a Comment

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code