2015年3月30日

C#,Asp.NET 导入Excel,时间格式一串数字转换.

在Excel中.日期或者时间格式为:42093.6506944444 或者 0.650694444444444

大于0 表示有日期(2015-03-30),小于零则是时间(15:37)

在C# 导入读取这列时,转换会发生错误;

现在将这格式转换为正常的日期格式:如下

 1 /// <summary>
 2     /// 数字转换时间格式
 3     /// </summary>
 4     /// <param name="timeStr">数字,如:42095.7069444444/0.650694444444444</param>
 5     /// <returns>日期/时间格式</returns>
 6     private string ToDateTimeValue(string strNumber)
 7     {
 8         if (!string.IsNullOrWhiteSpace(strNumber))
 9         {
10             Decimal tempValue;
11             //先检查 是不是数字;
12             if (Decimal.TryParse(strNumber, out tempValue))
13             {
14                 //天数,取整
15                 int day = Convert.ToInt32(Math.Truncate(tempValue));
16                 //这里也不知道为什么. 如果是小于32,则减1,否则减2
17                 //日期从1900-01-01开始累加 
18                 // day = day < 32 ? day - 1 : day - 2;
19                 DateTime dt = new DateTime(1900, 1, 1).AddDays(day < 32 ? (day - 1) : (day - 2));
20 
21                 //小时:减掉天数,这个数字转换小时:(* 24) 
22                 Decimal hourTemp = (tempValue - day) * 24;//获取小时数
23                 //取整.小时数
24                 int hour = Convert.ToInt32(Math.Truncate(hourTemp));
25                 //分钟:减掉小时,( * 60)
26                 //这里舍入,否则取值会有1分钟误差.
27                 Decimal minuteTemp = Math.Round((hourTemp - hour) * 60, 2);//获取分钟数
28                 int minute = Convert.ToInt32(Math.Truncate(minuteTemp));
29                 //秒:减掉分钟,( * 60)
30                 //这里舍入,否则取值会有1秒误差.
31                 Decimal secondTemp = Math.Round((minuteTemp - minute) * 60, 2);//获取秒数
32                 int second = Convert.ToInt32(Math.Truncate(secondTemp));
33 
34                 //时间格式:00:00:00
35                 string resultTimes = string.Format("{0}:{1}:{2}",
36                         (hour < 10 ? ("0" + hour) : hour.ToString()),
37                         (minute < 10 ? ("0" + minute) : minute.ToString()),
38                         (second < 10 ? ("0" + second) : second.ToString()));
39 
40                 if (day > 0)
41                     return string.Format("{0} {1}", dt.ToString("yyyy-MM-dd"), resultTimes);
42                 else
43                     return resultTimes;
44             }
45         }
46         return string.Empty;
47     }

 

posted @ 2015-03-30 17:17 555[] 阅读(3013) 评论(0) 推荐(0) 编辑

2014年7月9日

iframe 一次下载多个文件,不需要打包.

摘要: 多谢这位哥们解决了我的难题,收藏了引用:http://liyifeng20021.blog.163.com/blog/static/1016315920142551453938/稍微修改一点function batch_download(ids){var tmp_array = [];tmp_arr... 阅读全文

posted @ 2014-07-09 12:05 555[] 阅读(3146) 评论(0) 推荐(0) 编辑

2013年11月18日

C# 字符串,查找替换,支持正则

摘要: /// /// 获取字符串,查找替换 /// /// 字符串 /// 替换值 /// 查找规则[正则,如:@"\d{2}",直接匹配字符,如:'abc']..注意正则转义 /// 要替换出现的位置从1开始 /// 如果只出现一次,是否替换,[0:否,1:是] /// 是否全部替换 /// public string GetStringReplace(string text, string val, string rex, int textIndex,int once, bool flag) { Regex ... 阅读全文

posted @ 2013-11-18 09:05 555[] 阅读(6236) 评论(0) 推荐(0) 编辑

Javascript 操作合并table列rowspan

摘要: View Code 此代码是转载. 阅读全文

posted @ 2013-11-18 09:00 555[] 阅读(1272) 评论(0) 推荐(0) 编辑

2011年11月2日

AspNetPager 搜索返回当前页

摘要: CommodityList.aspx:page 1 <div style="margin: 0 auto; text-align: center; width: 500px;"> 2 <asp:DropDownList ID="DropDownList1" runat="server"> 3 <asp:ListItem Selected="True" Value="0">产品名称</asp:ListItem> 4 </asp:DropDown 阅读全文

posted @ 2011-11-02 14:21 555[] 阅读(662) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示