C #, Asp.NET introduced Excel, time format into a digital sequence processing method

In Excel, the date or time format: 42093.6506944444 or 0.650694444444444

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

When introducing C # reading this column, the conversion error occurs;

Now convert these formats to the normal date format: as follows:


/// <summary>
    /// 数字转换时间格式
    /// </summary>
    /// <param name="timeStr">数字,如:42095.7069444444/0.650694444444444</param>
    /// <returns>日期/时间格式</returns>
    private string ToDateTimeValue(string strNumber)
    {
        if (!string.IsNullOrWhiteSpace(strNumber))
        {
            Decimal tempValue;
            //先检查 是不是数字;
            if (Decimal.TryParse(strNumber, out tempValue))
            {
                //天数,取整
                int day = Convert.ToInt32(Math.Truncate(tempValue));
                //这里也不知道为什么. 如果是小于32,则减1,否则减2
                //日期从1900-01-01开始累加 
                // day = day < 32 ? day - 1 : day - 2;
                DateTime dt = new DateTime(1900, 1, 1).AddDays(day < 32 ? (day - 1) : (day - 2));
 
                //小时:减掉天数,这个数字转换小时:(* 24) 
                Decimal hourTemp = (tempValue - day) * 24;//获取小时数
                //取整.小时数
                int hour = Convert.ToInt32(Math.Truncate(hourTemp));
                //分钟:减掉小时,( * 60)
                //这里舍入,否则取值会有1分钟误差.
                Decimal minuteTemp = Math.Round((hourTemp - hour) * 60, 2);//获取分钟数
                int minute = Convert.ToInt32(Math.Truncate(minuteTemp));
                //秒:减掉分钟,( * 60)
                //这里舍入,否则取值会有1秒误差.
                Decimal secondTemp = Math.Round((minuteTemp - minute) * 60, 2);//获取秒数
                int second = Convert.ToInt32(Math.Truncate(secondTemp));
 
                //时间格式:00:00:00
                string resultTimes = string.Format("{0}:{1}:{2}",
                        (hour < 10 ? ("0" + hour) : hour.ToString()),
                        (minute < 10 ? ("0" + minute) : minute.ToString()),
                        (second < 10 ? ("0" + second) : second.ToString()));
 
                if (day > 0)
                    return string.Format("{0} {1}", dt.ToString("yyyy-MM-dd"), resultTimes);
                else
                    return resultTimes;
            }
        }
        return string.Empty;
    }

Original link: https://blog.csdn.net/XMM_1030/article/details/45580331 

 

2019/05/24 Update:

Reference the above wording friend found from Excel to read the data, some for a second or less. Modify the code as follows:

private string ToDateTimeValue(string strNumber)
{
	if (!string.IsNullOrWhiteSpace(strNumber))
	{
		Decimal tempValue;
		//先检查 是不是数字;
		if (Decimal.TryParse(strNumber, out tempValue))
		{
			int day = 0;
			int hour = 0;
			int minute = 0;
			int second = 0;
			int totalSecond = 0;

			//天数,取整,整数部分即为天数
			day = Convert.ToInt32(Math.Truncate(tempValue));

			//获取除去天数后剩下的秒数,四舍五入后取整
			totalSecond = Convert.ToInt32(Math.Truncate(Math.Round((tempValue - day) * 24 * 60 * 60)));

			//计算小时数,如果秒数大于一小时,说明存在小时数
			if (totalSecond > 3600)
				hour = totalSecond / 3600;
			else
				hour = 0;

			//计算分钟数,减去得到的小时数之后,如果剩下的秒数大于一分钟,说明存在分钟数
			if ((totalSecond - hour * 3600) > 59)
				minute = (totalSecond - hour * 3600) / 60;
			else
				minute=0;
			
			//减去小时数、分钟数用到的秒数之后,余下的就是最终的秒数
			second = totalSecond - hour * 3600 - minute * 60;

			//将天数累计到小时数上
			hour = hour + day * 24;

			//时间格式:00:00:00
			string resultTimes = string.Format("{0}:{1}:{2}",
					(hour < 10 ? ("0" + hour) : hour.ToString()),
					(minute < 10 ? ("0" + minute) : minute.ToString()),
					(second < 10 ? ("0" + second) : second.ToString()));
			
			return resultTimes;
		}
	}
	return strNumber;
}

note:

1. rewrite this code is not 24-hour time format, such as Microsoft Excel may be time data, the program made changes slightly;

2. The basic idea is: reading from Excel to the numeric string is actually the number of days, which is first converted into seconds, and then rounding to obtain an integer number of seconds (here, can solve the problem of the lack of one second), then additional time is calculated , minutes, seconds; 

Guess you like

Origin blog.csdn.net/qq_24470501/article/details/89537778