在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
大气象
protected void btnPost_Click(object sender, EventArgs e)
{ if (Session["lastTime"] == null) { Session["lastTime"] = DateTime.Now; } DateTime dt = Convert.ToDateTime(Session["lastTime"]); TimeSpan ts = DateTime.Now - dt; Response.Write(ts.Days + ":" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds); if (ts.Hours > 0 || ts.Minutes > 0 || ts.Seconds > 30) { Session["lastTime"] = DateTime.Now; Response.Write("可以发帖!"); } else { Response.Write("你的发帖速度过快!时间间隔不要小于30秒!"); } } /// <summary> /// 把秒转换成分钟 /// </summary> /// <returns></returns> public static int SecondToMinute(int Second) { decimal mm = (decimal)((decimal)Second / (decimal)60); return Convert.ToInt32(Math.Ceiling(mm)); } /// <summary> /// 计算两个时间的时间间隔 /// </summary> /// <param name="DateTimeOld">较早的日期和时间</param> /// <param name="DateTimeNew">较后的日期和时间</param> /// <returns></returns> public static string DateDiff(DateTime DateTimeOld, DateTime DateTimeNew) { string dateDiff = ""; TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks); TimeSpan ts2 = new TimeSpan(DateTimeNew.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); int day = ts.Days; int hou = ts.Hours; int minu = ts.Minutes; int sec = ts.Seconds; if (day > 0) { if (day > 30) { if (day > 364) { dateDiff += day / 365 + "年"; } else { dateDiff += day / 30 + "个月"; } } else { dateDiff += day.ToString() + "天"; } } else { if (hou > 0) { dateDiff += hou.ToString() + "小时"; } else { if (minu > 0) { dateDiff += minu.ToString() + "分钟"; } else { if (sec > 0) { dateDiff += sec.ToString() + "秒"; } else { dateDiff += "0秒"; } } } } if (DateTimeNew.CompareTo(DateTimeOld) > 0) { dateDiff += "前"; } else { dateDiff += "后"; } return dateDiff; } /// <summary> ///判断是否于5分钟之前 /// </summary> /// <param name="DateTimeOld">较早的日期和时间</param> /// <returns></returns> public static bool DateDiff_minu(DateTime DateTimeOld) { TimeSpan ts1 = new TimeSpan(DateTimeOld.Ticks); TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); int minu = ts.Minutes; if (minu > 5) { return true; } else { return false; } } /// <summary> /// 与当前时间比较,重载时间比较函数,只有一个参数 /// </summary> /// <param name="DateTimeOld">较早的日期和时间</param> /// <returns></returns> public static string DateDiff(DateTime DateTimeOld) { return DateDiff(DateTimeOld, DateTime.Now); } /// <summary> /// 日期比较,返回精确的几分几秒 /// </summary> /// <param name="DateTime1">较早的日期和时间</param> /// <param name="DateTime2">较迟的日期和时间</param> /// <returns></returns> public static string DateDiff_full(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒"; return dateDiff; } /// <summary> /// 时间比较,返回精确的几秒 /// </summary> /// <param name="DateTime1">较早的日期和时间</param> /// <param name="DateTime2">较迟的日期和时间</param> /// <returns></returns> public static int DateDiff_Sec(DateTime DateTime1, DateTime DateTime2) { TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); int dateDiff = ts.Days * 86400 + ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds; return dateDiff; } /// <summary> /// 日期比较 /// </summary> /// <param name="today">当前日期</param> /// <param name="writeDate">输入日期</param> /// <param name="n">比较天数</param> /// <returns>大于天数返回true,小于返回false</returns> public static bool CompareDate(string today, string writeDate, int n) { DateTime Today = Convert.ToDateTime(today); DateTime WriteDate = Convert.ToDateTime(writeDate); WriteDate = WriteDate.AddDays(n); if (Today >= WriteDate) return false; else return true; }
|
请发表评论