C # Time Operation Time Calculation

Source: Internet
Author: User

A c # Time tool class that can be used for time computing, for example, a birthday reminder is implemented based on the date of birth, a comparison of two days of the date, a Chinese day of the week is returned Based on the day of the English day, and the difference between the two time is calculated, returns x days x hours x minutes x seconds, Time Difference values, returns based on time a few months ago, a few days ago, a few hours ago, a few minutes ago, and a few seconds ago, if you cannot use it now, add it to favorites first. The Code is as follows:

001 using System;

002 using System. Collections. Generic;

003 using System. Linq;

004 using System. Text;

005 namespace CLB. Utility. CharTools

006 {

007 public static class DateTimeHelper

008 {

009 ////

010 // return a few months ago, several days ago, several hours ago, several minutes ago, and several seconds ago

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. tow.datestring ();

020}

021 else if (span. TotalDays> 30)

022 {

023 return "1 month ago ";

024}

025 else if (span. TotalDays> 14)

026 {

027 return "2 weeks ago ";

028}

029 else if (span. TotalDays> 7)

030 {

031 return "1 week ago ";

032}

033 else if (span. TotalDays> 1)

034 {

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

036}

037 else if (span. TotalHours> 1)

038 {

039 return string. Format ("{0} Hours Ago", (int) Math. Floor (span. TotalHours ));

040}

041 else if (span. TotalMinutes> 1)

042 {

043 return string. Format ("{0} Minutes Ago", (int) Math. Floor (span. TotalMinutes ));

044}

045 else if (span. TotalSeconds> = 1)

046 {

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

048}

049 else

050 {

051 return "1 second ago ";

052}

053}

054 ///

055 // time difference, return time difference

056 // when isTotal is set to true, the number of days with decimal digits is returned. Otherwise, the returned number is an integer.

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 // Number of days with decimals. For example, if one day is 12 hours, the result is 1.5.

067 return ts. TotalDays. ToString ();

068 else

069 // integer number of days, 12 hours a day or 20 hours a day results in 1

070 return ts. Days. ToString ();

071}

072 ///

073 // calculate the difference between the two time points. the return value is x days x hours x minutes x seconds.

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 () + "day" + ts. hours. toString () + "Hour" + ts. minutes. toString () + "Minute" + ts. seconds. toString () + "seconds ";

086 return dateDiff;

087}

088 ///

089 // return the Chinese day of the week based on the English day of the week

090 // For example, WhichDay ("Sunday"), return to Sunday

091 ///

092 ///

093 ///

094 public static string WhichDay (string enWeek)

095 {

096 switch (enWeek. Trim ())

097 {

098 case "Sunday ":

099 return "Sunday ";

100 case "Monday ":

101 return "Monday ";

102 case "Tuesday ":

103 return "Tuesday ";

104 case "Wednesday ":

105 return "Wednesday ";

106 case "Thursday ":

107 return "Thursday ";

108 case "Friday ":

109 return "Friday ";

110 case "Saturday ":

111 return "Saturday ";

Default 112:

113 return enWeek;

114}

115}

116 ///

117 // Date comparison

118 ///

119 // current date

120 // enter the date

121 // compare days

122 // if the number of days is greater than, true is returned. If the number of days is less than, false is returned.

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 // birthday Reminder Based on the date of birth

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} next month", birthday. Day );

146 if (nowMonth = 1 & birtMonth = 12)

147 return string. Format ("last month {0}", birthday. Day );

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

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

150 if (months = 1)

151 return string. Format ("last month {0}", birthday. Day );

152 else if (months =-1)

153 return string. Format ("{0} next month", birthday. Day );

154 else if (months = 0)

155 {

156 if (now. Day = birthday. Day)

157 return "today ";

158 return string. Format ("{0} this month", birthday. Day );

159}

160 else if (months> 1)

161 return string. Format ("{0} months passed", months );

162 else

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

164}

165}

166}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.