C # Summary of the time Timehelper class

Source: Internet
Author: User

Using System;

Namespace Dotnet.utilities
{
<summary>
Time class
1, secondtominute (int Second) convert seconds to minutes
</summary>
public class Timehelper
{
<summary>
Formats the time into a month-by-day format and returns the current system time if the time is null
</summary>
<param name= "DT" > Month and Day separators </param>
<param name= "Separator" ></param>
<returns></returns>
public string getformatdate (DateTime dt, char Separator)
{
if (dt! = null &&!dt. Equals (DBNull.Value))
{
string tem = string. Format ("Yyyy{0}mm{1}dd", Separator, Separator);
Return dt. ToString (TEM);
}
Else
{
Return Getformatdate (DateTime.Now, Separator);
}
}
<summary>
Formats the time in seconds and minutes, and returns the current system time if the time is null
</summary>
<param name= "DT" ></param>
<param name= "Separator" ></param>
<returns></returns>
public string Getformattime (DateTime dt, char Separator) {
if (dt! = null &&!dt. Equals (DBNull.Value))
{
string tem = string. Format ("Hh{0}mm{1}ss", Separator, Separator);
Return dt. ToString (TEM);
}
Else
{
Return Getformatdate (DateTime.Now, Separator);
}
}
<summary>
Convert seconds to minutes
</summary>
<returns></returns>
public static int Secondtominute (int Second)
{
decimal mm = (decimal) ((decimal) Second/(decimal) 60);
Return Convert.ToInt32 (math.ceiling (mm));
}

#region return to the last day of a certain month
<summary>
Returns the last day of the month of a year
</summary>
<param name= "Year" > Years </param>
<param name= "Month" > Month </param>
<returns> Day </returns>
public static int Getmonthlastdate (int year, int month)
{
DateTime lastday = new DateTime (year, Month, New System.Globalization.GregorianCalendar (). Getdaysinmonth (year, month));
int day = Lastday.day;
Return day;
}
#endregion

#region Return time difference
public static string DateDiff (DateTime DateTime1, datetime DateTime2)
{
string dateDiff = null;
Try
{
TimeSpan ts1 = new TimeSpan (datetime1.ticks);
TimeSpan ts2 = new TimeSpan (datetime2.ticks);
TimeSpan ts = ts1. Subtract (TS2). Duration ();
TimeSpan ts = datetime2-datetime1;
if (TS. Days >= 1)
{
DateDiff = DateTime1.Month.ToString () + "month" + DateTime1.Day.ToString () + "Day";
}
Else
{
if (TS. Hours > 1)
{
DateDiff = ts. Hours.tostring () + "hours ago";
}
Else
{
DateDiff = ts. Minutes.tostring () + "Minutes ago";
}
}
}
Catch
{ }
return DateDiff;
}
#endregion

#region get a two-day interval
<summary>
Get a two-date interval
</summary>
<param name= "DateTime1" > Date one. </param>
<param name= "DateTime2" > Date two. </param>
<returns> date interval TimeSpan. </returns>
public static TimeSpan DateDiff2 (DateTime DateTime1, datetime DateTime2)
{
TimeSpan ts1 = new TimeSpan (datetime1.ticks);
TimeSpan ts2 = new TimeSpan (datetime2.ticks);
TimeSpan ts = ts1. Subtract (TS2). Duration ();
return TS;
}
#endregion

#region Format Date Time
<summary>
Format Date Time
</summary>
<param name= "dateTime1" > Date Time </param>
<param name= "Datemode" > Display mode </param>
Date of <returns>0-9 mode </returns>
public static string FormatDate (DateTime dateTime1, String datemode)
{
Switch (Datemode)
{
Case "0":
Return datetime1.tostring ("Yyyy-mm-dd");
Case "1":
Return datetime1.tostring ("Yyyy-mm-dd HH:mm:ss");
Case "2":
Return datetime1.tostring ("Yyyy/mm/dd");
Case "3":
return datetime1.tostring ("yyyy mm month DD day");
Case "4":
Return datetime1.tostring ("Mm-dd");
Case "5":
Return datetime1.tostring ("Mm/dd");
Case "6":
Return datetime1.tostring ("MM month DD day");
Case "7":
Return datetime1.tostring ("yyyy-mm");
Case "8":
Return datetime1.tostring ("yyyy/mm");
Case "9":
return datetime1.tostring ("yyyy mm month");
Default
return datetime1.tostring ();
}
}
#endregion

#region Get random Dates
<summary>
Get a random date
</summary>
<param name= "time1" > Start date </param>
<param name= "time2" > End Date </param>
Random dates between <returns> interval dates </returns>
public static datetime getrandomtime (DateTime time1, datetime time2)
{
Random random = new random ();
DateTime mintime = new DateTime ();
DateTime maxtime = new DateTime ();

System.TimeSpan ts = new System.TimeSpan (time1. Ticks-time2. Ticks);

Gets the number of seconds between two time intervals
Double Dtotalsecontds = ts. TotalSeconds;
int Itotalsecontds = 0;

if (Dtotalsecontds > System.Int32.MaxValue)
{
Itotalsecontds = System.Int32.MaxValue;
}
else if (Dtotalsecontds < System.Int32.MinValue)
{
Itotalsecontds = System.Int32.MinValue;
}
Else
{
Itotalsecontds = (int) Dtotalsecontds;
}


if (Itotalsecontds > 0)
{
Mintime = time2;
MaxTime = time1;
}
else if (Itotalsecontds < 0)
{
Mintime = time1;
MaxTime = time2;
}
Else
{
return time1;
}

int maxValue = Itotalsecontds;

if (Itotalsecontds <= System.Int32.MinValue)
MaxValue = System.Int32.MinValue + 1;

int i = random. Next (System.Math.Abs (MaxValue));

return mintime.addseconds (i);
}
#endregion
}
}

C # Summary of the time Timehelper class

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.