C#時(shí)間操作類時(shí)間計(jì)算等
來(lái)源:易賢網(wǎng) 閱讀:1206 次 日期:2014-08-20 15:00:16
溫馨提示:易賢網(wǎng)小編為您整理了“C#時(shí)間操作類時(shí)間計(jì)算等”,方便廣大網(wǎng)友查閱!

一個(gè)C#時(shí)間工具類,可用于時(shí)間計(jì)算,比如根據(jù)出生年月實(shí)現(xiàn)生日提醒、兩個(gè)日期天數(shù)比較、根據(jù)英文的星期幾返回中文的星期幾、計(jì)算兩個(gè)時(shí)間的差值,返回的是x天x小時(shí)x分鐘x秒、時(shí)間相差值、根據(jù)時(shí)間返回幾個(gè)月前,幾天前,幾小時(shí)前,幾分鐘前以及幾秒前,現(xiàn)在用不到的話,先收藏吧,代碼如下:

001using System;

002using System.Collections.Generic;

003using System.Linq;

004using System.Text;

005namespace CLB.Utility.CharTools

006{

007 public static class DateTimeHelper

008 {

009 ////

010 /// 根據(jù)時(shí)間返回幾個(gè)月前,幾天前,幾小時(shí)前,幾分鐘前以及幾秒前

011 ///

012 ///

013 ///

014 public static string DateStringFromNow(DateTime dt)

015 {

016 TimeSpan span = DateTime.Now - dt;

017 if (span.TotalDays > 60)

018 {

019 return dt.ToShortDateString();

020 }

021 else if (span.TotalDays > 30)

022 {

023 return "1個(gè)月前";

024 }

025 else if (span.TotalDays > 14)

026 {

027 return "2周前";

028 }

029 else if (span.TotalDays > 7)

030 {

031 return "1周前";

032 }

033 else if (span.TotalDays > 1)

034 {

035 return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));

036 }

037 else if (span.TotalHours > 1)

038 {

039 return string.Format("{0}小時(shí)前", (int)Math.Floor(span.TotalHours));

040 }

041 else if (span.TotalMinutes > 1)

042 {

043 return string.Format("{0}分鐘前", (int)Math.Floor(span.TotalMinutes));

044 }

045 else if (span.TotalSeconds >= 1)

046 {

047 return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));

048 }

049 else

050 {

051 return "1秒前";

052 }

053 }

054 ///

055 /// 時(shí)間相差值,返回時(shí)間差

056 /// 調(diào)用時(shí),isTotal為true時(shí),返回的時(shí)帶小數(shù)的天數(shù),否則返回的是整數(shù)

057 ///

058 ///

059 ///

060 ///

061 ///

062 public static string DateDiff(DateTime DateTime1, DateTime DateTime2, bool isTotal)

063 {

064 TimeSpan ts = DateTime2 - DateTime1;

065 if (isTotal)

066 //帶小數(shù)的天數(shù),比如1天12小時(shí)結(jié)果就是1.5

067 return ts.TotalDays.ToString();

068 else

069 //整數(shù)天數(shù),1天12小時(shí)或者1天20小時(shí)結(jié)果都是1

070 return ts.Days.ToString();

071 }

072 ///

073 /// 計(jì)算兩個(gè)時(shí)間的差值,返回的是x天x小時(shí)x分鐘x秒

074 ///

075 ///

076 ///

077 ///

078 public static string DateDiff(DateTime DateTime1, DateTime DateTime2)

079 {

080 string dateDiff = null;

081 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);

082 TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);

083 TimeSpan ts = ts1.Subtract(ts2).Duration();

084 //TimeSpan ts=ts1.Add(ts2).Duration();

085 dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小時(shí)" + ts.Minutes.ToString() + "分鐘"+ ts.Seconds.ToString() + "秒";

086 return dateDiff;

087 }

088 ///

089 /// 根據(jù)英文的星期幾返回中文的星期幾

090 /// 如WhichDay("Sunday"),返回星期日

091 ///

092 ///

093 ///

094 public static string WhichDay(string enWeek)

095 {

096 switch (enWeek.Trim())

097 {

098 case "Sunday":

099 return "星期日";

100 case "Monday":

101 return "星期一";

102 case "Tuesday":

103 return "星期二";

104 case "Wednesday":

105 return "星期三";

106 case "Thursday":

107 return "星期四";

108 case "Friday":

109 return "星期五";

110 case "Saturday":

111 return "星期六";

112 default:

113 return enWeek;

114 }

115 }

116 ///

117 /// 日期比較

118 ///

119 /// 當(dāng)前日期

120 /// 輸入日期

121 /// 比較天數(shù)

122 /// 大于天數(shù)返回true,小于返回false

123 public static bool CompareDate(string today, string writeDate, int n)

124 {

125 DateTime Today = Convert.ToDateTime(today);

126 DateTime WriteDate = Convert.ToDateTime(writeDate);

127 WriteDate = WriteDate.AddDays(n);

128 if (Today >= WriteDate)

129 return false;

130 else

131 return true;

132 }

133 ///

134 /// 根據(jù)出生年月進(jìn)行生日提醒

135 ///

136 ///

137 ///

138 public static string GetBirthdayTip(DateTime birthday)

139 {

140 DateTime now = DateTime.Now;

141 //TimeSpan span = DateTime.Now - birthday;

142 int nowMonth = now.Month;

143 int birtMonth = birthday.Month;

144 if (nowMonth == 12 && birtMonth == 1)

145 return string.Format("下月{0}號(hào)", birthday.Day);

146 if (nowMonth == 1 && birtMonth == 12)

147 return string.Format("上月{0}號(hào)", birthday.Day);

148 int months = now.Month - birthday.Month;

149 //int days = now.Day - birthday.Day;

150 if (months == 1)

151 return string.Format("上月{0}號(hào)", birthday.Day);

152 else if (months == -1)

153 return string.Format("下月{0}號(hào)", birthday.Day);

154 else if (months == 0)

155 {

156 if (now.Day == birthday.Day)

157 return "今天";

158 return string.Format("本月{0}號(hào)", birthday.Day);

159 }

160 else if (months > 1)

161 return string.Format("已過(guò){0}月", months);

162 else

163 return string.Format("{0}月{1}日", birthday.Month, birthday.Day);

164 }

165 }

166}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:C#時(shí)間操作類時(shí)間計(jì)算等
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)