C # time Operation class time calculation, etc.

Source: Internet
Author: User
Tags bool comparison datetime static class

A C # Time tool class that can be used for time calculations, such as birthday reminders based on the date of birth, two date days comparison, the day of the week in English to return to the Chinese day of the day, calculated two time difference, returned is x days x hours x minutes x seconds, time difference, according to the time returned a few months ago, a few days ago, a few hours ago, A few minutes ago and a few seconds ago, now not used, the first collection, the code is as follows:

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///According to time, a few months ago, a few days ago, a few hours ago, a few minutes ago, and a few 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. ToShortDateString ();

020}

021 else if (span. Totaldays > 30)

022 {

023 return "1 months 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 weeks 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 seconds ago";

052}

053}

054///

055///Time difference value, return times

056///Call, when Istotal is true, returns the number of days with decimals, otherwise returning 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, such as 1 days and 12 hours, the result is 1.5.

067 return TS. Totaldays.tostring ();

068 Else

069//Integer days, 1 days 12 hours or 1 days 20 hours results are 1

070 return TS. Days.tostring ();

071}

072///

073///calculates the difference of two time, returns 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 () + "days" + TS. Hours.tostring () + "hour" + TS. Minutes.tostring () + "minutes" + TS. Seconds.tostring () + "seconds";

086 return dateDiff;

087}

088///

089 weeks of///in English on the day of the week returned to Chinese

090///such as 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";

Case "Monday":

"Monday";

102 Case "Tuesday":

The return of "Tuesday";

Case "Wednesday":

"Wednesday";

The case "Thursday":

"Thursday";

108 Case "Friday":

109 return "Friday";

The case "Saturday":

"Saturday";

112 Default:

113 return Enweek;

114}

115}

116///

117///Date Comparison

118///

119///Current Date

120///Input Date

121///Comparison Days

122///is greater than the number of days returns true, less than return false

123 public static bool Comparedate (string today, string writedate, int n)

124 {

"DateTime today = Convert.todatetime";

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 Reminders based on date of birth

135///

136///

137///

138 public static string Getbirthdaytip (DateTime birthday)

139 {

140 DateTime now = DateTime.Now;

//timespan span = Datetime.now-birthday;

Nowmonth int = now. Month;

143 int birtmonth = birthday. Month;

144 if (Nowmonth = && Birtmonth = 1)

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

146 if (Nowmonth = = 1 && birtmonth = 12)

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

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

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

The IF (months = 1)

151 return string. Format ("{0} last month", 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 ("past {0} months", months);

162 Else

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

164}

165}

166}

Related Article

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.