Long类型转换成DateTime类型写法(字符串截取)
c#中字符串截取使用的方法
String substring(int beginIndex)
String substring(int beginIndex, int endIndex)
String.Substring (Int32) 子字符串从指定的字符位置开始。
String.Substring (Int32, Int32) 子字符串从指定的字符位置开始且具有指定的长度。
/// <summary> /// Long类型转换为Datetime类型 /// </summary> /// <param name="LongTime"></param> private DateTime? LongConvertDateTime(long LongTime) { try { DateTime dtime = new DateTime(); if (LongTime.ToString().Length > 8) { StringBuilder sb = new StringBuilder(); string str = LongTime.ToString(); string y, M, d, HH, mm, ss, Time; y = str.Substring(0, 4); M = str.Substring(4, 2); d = str.Substring(6, 2); HH = str.Substring(8, 2); mm = str.Substring(10, 2); ss = str.Substring(12, 2); Time = sb.Append(y).Append(M).Append(d).Append(HH).Append(mm).Append(ss).ToString(); dtime = Convert.ToDateTime(Time); } else { StringBuilder sb = new StringBuilder(); string str = LongTime.ToString(); string y, M, d, Time; y = str.Substring(0, 4); M = str.Substring(4, 2); d = str.Substring(6, 2); Time = sb.Append(y).Append(M).Append(d).ToString(); dtime = Convert.ToDateTime(Time); } return dtime; } catch (Exception) { return null; } }