今天要计算用户在线的时间, 时间的格式: 2014-07-09 15:27:38
写了一个静态方法, 输入两个时间,可以得到两个时间差.
1 static class time 2 { 3 static string reg_d="(?<=(\-))\d+"; 4 static string reg_h="\d+(?=\:)"; 5 static int[] monthdays_p={0,0,131,59,90,120,151,181,212,243,273,304,334}; 6 //static int monthdays_r[]={0,0,131,59,90,120,151,181,212,243,273,304,334}; 7 public static int cha(string a,string b) 8 { 9 int am=0,ad=0,ah=0,af=0; 10 int bm=0,bd=0,bh=0,bf=0; 11 int i=0; 12 foreach(Match m in Regex.Matches(a,reg_d)) 13 { 14 if(i++==0) 15 am=int.Parse(m.ToString()); 16 ad=int.Parse(m.ToString()); 17 } 18 i=0; 19 foreach(Match m in Regex.Matches(b,reg_d)) 20 { 21 if(i++==0) 22 bm=int.Parse(m.ToString()); 23 bd=int.Parse(m.ToString()); 24 } 25 i=0; 26 foreach(Match m in Regex.Matches(a,reg_h)) 27 { 28 if(i++==0) 29 ah=int.Parse(m.ToString()); 30 af=int.Parse(m.ToString()); 31 } 32 i=0; 33 foreach(Match m in Regex.Matches(b,reg_h)) 34 { 35 if(i++==0) 36 bh=int.Parse(m.ToString()); 37 bf=int.Parse(m.ToString()); 38 } 39 40 int chazhi=0; 41 42 chazhi=bf-af+(bh-ah)*60+(bd-ad+monthdays_p[bm]-monthdays_p[am])*1440; 43 return chazhi; 44 } 45 }