C#long型数据转化成时间戳(TimeSpan)

.Net中TimeSpan构造函数

1      
2         public TimeSpan(long ticks);   // 以 100 毫微秒为单位表示的时间段。
3 
4         public TimeSpan(int hours, int minutes, int seconds);
5     
6         public TimeSpan(int days, int hours, int minutes, int seconds);
7    
8         public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);

.Net中构造TimeSpan函数

  1         // 返回表示指定天数的 System.TimeSpan,其中对天数的指定精确到最接近的毫秒。
  2         //
  3         // 参数:  value:  天数,精确到最接近的毫秒
  4         public static TimeSpan FromDays(double value);
  5       
  6         // 返回表示指定小时数的 System.TimeSpan,其中对小时数的指定精确到最接近的毫秒。
  7         // 参数:  value: 精确到最接近的毫秒的小时数。
  8         public static TimeSpan FromHours(double value);
  9       
 10         //  返回表示指定毫秒数的 System.TimeSpan。
 11         //   参数: value: 毫秒数。
 12                public static TimeSpan FromMilliseconds(double value);
 13    
 14         // 返回表示指定分钟数的 System.TimeSpan,其中对分钟数的指定精确到最接近的毫秒。
 15         // 参数: value:分钟数,精确到最接近的毫秒。      
 16         public static TimeSpan FromMinutes(double value);
 17      
 18         // 返回表示指定秒数的 System.TimeSpan,其中对秒数的指定精确到最接近的毫秒。
 19         // 参数: value:    秒数,精确到最接近的毫秒。
 20         public static TimeSpan FromSeconds(double value);
 21       
 22         // 返回表示指定时间的 System.TimeSpan,其中对时间的指定以刻度为单位。
 23         //
 24         // 参数:  表示时间的刻度数。并非是毫秒数。
 25         public static TimeSpan FromTicks(long value);
 26      
 27         //     将时间间隔的字符串表示形式转换为等效的 System.TimeSpan。
 28      
 29         // 参数: s:     一个字符串,用于指定进行转换的时间间隔
 30         public static TimeSpan Parse(string s);
 31     
 32         //     使用指定的区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 System.TimeSpan。
 33         //
 34         // 参数:   input:    一个字符串,用于指定进行转换的时间间隔。
 35            //   formatProvider:   一个提供区域性特定的格式设置信息的对象。
 36         public static TimeSpan Parse(string input, IFormatProvider formatProvider);
 37     
 38         //     使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 System.TimeSpan。 字符串表示形式的格式必须与一种指定的格式完全匹配。
 39         //   input:  一个字符串,用于指定进行转换的时间间隔。
 40         //   formats:
 41         //     用于定义所需的 input 格式的标准或自定义格式字符串的数组。
 42         //   formatProvider:   一个对象,提供区域性特定的格式设置信息。 //
 43         //   styles:枚举值的按位组合,用于定义可出现在 input 中的样式元素。
 44         public static TimeSpan ParseExact(string input, string[] formats, IFormatProvider formatProvider, TimeSpanStyles styles);
 45       
 46         //     使用指定的格式字符串数组和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 System.TimeSpan。 字符串表示形式的格式必须与一种指定的格式完全匹配。
 47         //
 48         // 参数: input:     一个字符串,用于指定进行转换的时间间隔。
 49         //   formats:  用于定义所需的 input 格式的标准或自定义格式字符串的数组。
 50         //   formatProvider: 一个对象,提供区域性特定的格式设置信息。
 51           
 52         public static TimeSpan ParseExact(string input, string[] formats, IFormatProvider formatProvider);
 53   
 54         //     使用指定的格式和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 System.TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。    
 55         // 参数:   input:一个字符串,用于指定进行转换的时间间隔。
 56         //   format: 用于定义所需的 input 格式的标准或自定义格式字符串。
 57         //   formatProvider: 一个对象,提供区域性特定的格式设置信息。
 58             public static TimeSpan ParseExact(string input, string format, IFormatProvider formatProvider);
 59       
 60         //     使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 System.TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。
 61         // 参数:   input:一个字符串,用于指定进行转换的时间间隔。
 62         //   format:  用于定义所需的 input 格式的标准或自定义格式字符串。
 63               //   formatProvider:一个对象,提供区域性特定的格式设置信息。
 64         //   styles:     枚举值的按位组合,用于定义可出现在 input 中的样式元素。  
 65         public static TimeSpan ParseExact(string input, string format, IFormatProvider formatProvider, TimeSpanStyles styles);
 66       
 67         //     将时间间隔的字符串表示形式转换为其等效的 System.TimeSpan,并返回一个指示转换是否成功的值。
 68              // 参数:    s:   一个字符串,用于指定进行转换的时间间隔。
 69             //   result: 此方法返回时,包含表示由 s 指定的时间间隔的对象;或者如果转换失败,则包含 System.TimeSpan.Zero。 此参数未经初始化即被传递。    
 70         public static bool TryParse(string s, out TimeSpan result);
 71      
 72         //     使用指定的区域性特定格式设置信息,将时间间隔的字符串表示形式转换为其等效的 System.TimeSpan,并返回一个指示转换是否成功的值。
 73         // 参数: input:一个字符串,用于指定进行转换的时间间隔。
 74         //   formatProvider:一个提供区域性特定的格式设置信息的对象。     
 75         //   result: 此方法返回时,包含表示由 input 指定的时间间隔的对象;或者如果转换失败,则包含 System.TimeSpan.Zero。 此参数未经初始化即被传递。
 76               public static bool TryParse(string input, IFormatProvider formatProvider, out TimeSpan result);
 77      
 78         //     使用指定的格式和区域性特定格式信息,将时间间隔的指定字符串表示形式转换为其等效的 System.TimeSpan,并返回一个指示转换是否成功的值。 字符串表示形式的格式必须与一种指定的格式完全匹配。
 79         // 参数: input:
 80         //     一个字符串,用于指定进行转换的时间间隔。
 81         //
 82         //   formats:
 83         //     用于定义可接受的 input 格式的标准或自定义格式字符串的数组。
 84         //
 85         //   formatProvider:
 86         //     一个对象,提供区域性特定的格式设置信息。
 87         //
 88         //   result:
 89         //     此方法返回时,包含表示由 input 指定的时间间隔的对象;或者如果转换失败,则包含 System.TimeSpan.Zero。 此参数未经初始化即被传递。
 90         //
 91         // 返回结果:
 92         //     如果 true 成功转换,则为 input;否则为 false。
 93         public static bool TryParseExact(string input, string[] formats, IFormatProvider formatProvider, out TimeSpan result);
 94         //
 95         // 摘要:
 96         //     使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 System.TimeSpan,并返回一个指示转换是否成功的值。 字符串表示形式的格式必须与指定的格式完全匹配。
 97         //
 98         // 参数:
 99         //   input:
100         //     一个字符串,用于指定进行转换的时间间隔。
101         //
102         //   format:
103         //     用于定义所需的 input 格式的标准或自定义格式字符串。
104         //
105         //   formatProvider:
106         //     一个对象,提供区域性特定的格式设置信息。
107         //
108         //   styles:
109         //     用于指示 input 的样式的一个或多个枚举值。
110         //
111         //   result:
112         //     此方法返回时,包含表示由 input 指定的时间间隔的对象;或者如果转换失败,则包含 System.TimeSpan.Zero。 此参数未经初始化即被传递。
113         //
114         // 返回结果:
115         //     如果 true 成功转换,则为 input;否则为 false。
116         public static bool TryParseExact(string input, string format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result);
117     
118         public static bool TryParseExact(string input, string[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result);
119      
120         public static bool TryParseExact(string input, string format, IFormatProvider formatProvider, out TimeSpan result);

时间为long型的毫秒转换成TimeSpan

public TimeSpan(long ticks);// 以 100 毫微秒为单位表示的时间段。

public static TimeSpan FromMilliseconds(double value);//long占8字节转double也是8字节。精度会丢失

自定义转换方法

 1 private TimeSpan RevertMillsecToTimeSpane(long msec)
 2 {
 3    long day = 0;
 4    long hour = 0;
 5    long minute = 0;
 6    long second = 0;
 7    int millsec = 0;
 8    if(msec > 1000)
 9   {
10       second = msec / 1000;
11       millsec = (int)(msec % 1000);
12   }
13   if(second > 60)
14  {
15      minute = second / 60;
16      second = second % 60;
17   }
18   if(minute > 60)
19   {
20      hour = minute / 60; 
21      minute = minute % 60;
22    }
23   if(hour > 24)
24   {
25      day = hour / 24;
26       hour = hour % 24;
27    }
28     return new TimeSpan((int)day, (int)hour, (int)minute, (int)second, millsec);
29 }
原文地址:https://www.cnblogs.com/VueDi/p/13653749.html