C#,asp.net import Excel, time format a series of numeric conversions.

Source: Internet
Author: User

In Excel. The date or time format is: 42093.6506944444 or 0.650694444444444

Greater than 0 indicates a date (2015-03-30), less than 0 is the time (15:37)

When the C # import reads this column, the conversion error occurs;

Now convert this format to the normal date format:

1 /// <summary>2     ///Digital Conversion Time format3     /// </summary>4     /// <param name= "Timestr" >numbers, such as: 42095.7069444444/0.650694444444444</param>5     /// <returns>Date/Time format</returns>6     Private stringTodatetimevalue (stringstrnumber)7     {8         if(!string. Isnullorwhitespace (strnumber))9         {Ten Decimal Tempvalue; One             //first check is not the number; A             if(Decimal.tryparse (Strnumber, outtempvalue)) -             { -                 //days, rounding the                 intDay =Convert.ToInt32 (Math.truncate (Tempvalue)); -                 //and I don't know why. Minus 1 if it's less than 32, or minus 2. -                 //Date starting from 1900-01-01 cumulative -                 //Day = Day < day-1: day-2; +DateTime dt =NewDateTime (1900,1,1). AddDays (Day < +? (Day-1): (Day-2)); -  +                 //hours: Minus the number of days, this number converted hours: (*) ADecimal hourtemp = (tempvalue-day) * -;//gets the number of hours at                 //Rounding . Number of hours -                 inthour =Convert.ToInt32 (Math.truncate (hourtemp)); -                 //minutes: Lose Hours, (*) -                 //rounding here, otherwise the value will have a 1 minute error. -Decimal minutetemp = Math.Round ((hourtemp-hour) * -,2);//gets the number of minutes -                 intminute =Convert.ToInt32 (Math.truncate (minutetemp)); in                 //seconds: Lose minutes, (*) -                 //rounding here, otherwise the value will have a 1 second error. toDecimal secondtemp = Math.Round ((minutetemp-minute) * -,2);//gets the number of seconds +                 intSecond =Convert.ToInt32 (Math.truncate (secondtemp)); -  the                 //time format: 00:00:00 *                 stringResulttimes =string. Format ("{0}:{1}:{2}", $(Hour <Ten? ("0"+hour): hour. ToString ()),Panax Notoginseng(Minute <Ten? ("0"+minute): minute. ToString ()), -(Second <Ten? ("0"+second): second. ToString ())); the  +                 if(Day >0) A                     return string. Format ("{0} {1}"Dt. ToString ("YYYY-MM-DD"), resulttimes); the                 Else +                     returnResulttimes; -             } $         } $         return string. Empty; -}

C#,asp.net import Excel, time format a series of numeric conversions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.