/// <summary> /// 使用TimeSpan计算两个时间差 /// </summary> /// <param name="DateTime1"></param> /// <param name="DateTime2"></param> /// <returns>Subtract:从中减去另一个TimeSpan值</returns> private string DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; TimeSpan ts = DateTime1.Subtract(DateTime2).Duration(); dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒"; return dateDiff; } /// <summary> /// // 计算字符串的 MD5 哈希值,并将该哈希作为 32 字符的十六进制格式字符串返回 /// </summary> /// <param name="inputStr"></param> /// <returns>常用于用户注册</returns> public string ToMD5Hash(string inputStr) { // 创建一个MD5CryptoServiceProvider对象的新实例 MD5 newMd5 = new MD5CryptoServiceProvider(); // 将输入的字符串转换为一个字节数组并计算哈希值。 byte[] sourceBit = Encoding.Default.GetBytes(inputStr); byte[] dataBit = newMd5.ComputeHash(sourceBit); string directStr = BitConverter.ToString(dataBit).Replace("-", ""); return directStr; } /// <summary> /// // 验证输入字符串经MD5转换后,是否等同于一个哈希值 /// </summary> /// <param name="inputStr"></param> /// <param name="hash"></param> /// <returns>常用于用户登录</returns> public bool VerifyMd5Hash(string inputStr, string hash) { // 将输入字符串MD5加密 string hashOfInput = ToMD5Hash(inputStr); // 创建一个StringComparer实例来比较两个哈希值 // StringComparer.OrdinalIgnoreCase:获取 StringComparer 对象 // 该对象执行不区分大小写的序号字符串比较。 StringComparer comparer = StringComparer.OrdinalIgnoreCase; return comparer.Compare(hashOfInput, hash) == 0; } /// <summary> /// 生成年月日时分秒时间流水号格式 /// </summary> /// <param name="now"></param> /// <returns></returns> public static string SerialNumber(DateTime now) { int year = now.Year; int Month = now.Month;//月部分 int Day = now.Day;//月中的第几天 int Hour = now.Hour;//时 int Minute = now.Minute;//分 int Second = now.Second; //秒 return year.ToString("0000") + Month.ToString("00") + Day.ToString("00") + Hour.ToString("00") + Minute.ToString("00") + Second.ToString("00"); } /// <summary> /// 根据号以及年月日时分秒生成订单编号 /// </summary> /// <param name="now"></param> /// <param name="berthCode"></param> /// <returns></returns> public static string SerialNumber(DateTime now, string Code) { int year = now.Year; int Month = now.Month;//月部分 int Day = now.Day;//月中的第几天 int Hour = now.Hour;//时 int Minute = now.Minute;//分 int Second = now.Second; //秒 return Code + year.ToString("0000") + Month.ToString("00") + Day.ToString("00") + Hour.ToString("00") + Minute.ToString("00") + Second.ToString("00"); } /// <summary> /// 传过来的DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string ConventDateTimeInt(string time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); //截取时间转换为标准时间格式 string str = time.Substring(0, 4) + "-" + time.Substring(4, 2) + "-" + time.Substring(6, 2) + " " + time.Substring(8, 2) + ":" + time.Substring(10, 2) + ":" + time.Substring(12, 2); DateTime nowTime = Convert.ToDateTime(str); long unixTime = (long)Math.Round((nowTime - startTime).TotalMilliseconds / 1000, MidpointRounding.AwayFromZero) * 1000; return unixTime.ToString(); } /// <summary> /// Unix时间戳转换为DateTime类型时间 /// </summary> /// <param name="d"></param> /// <returns></returns> public static System.DateTime ConvertIntDateTime(double d) { System.DateTime time = System.DateTime.MinValue; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); time = startTime.AddMilliseconds(d); return time; } /// <summary> /// 时间差 /// </summary> /// <param name="now"></param> /// <returns></returns> public static int DateTimeSubtract(DateTime now) { TimeSpan time = DateTime.Now.Subtract(now); int hours = time.Hours;//获取小时 int Minutes = time.Minutes;//获取分钟 int Seconds = time.Seconds;//获取秒数 return hours * 60 * 60 + Minutes * 60 + Seconds; }
MD5加密 时间差 流水号等方法,布布扣,bubuko.com
原文:http://www.cnblogs.com/Warmsunshine/p/3720737.html