Implementation Code of asp.net computing time and date Interval

Source: Internet
Author: User

In development, we often encounter things like forums that do not provide this function in many development languages, such as the number of pre-posted posts or uploaded files of computing users, we need to customize some functions. Below are some time and date interval programs. If you need them, please refer to them.

First, let's look at the simplest. net code for calculating the time difference.

The Code is as follows: Copy code

DateTime dt1 = new DateTime );
DateTime dt2 = new DateTime );
System. TimeSpan st = dt2.sSubtract (dt1 );
This. TextBox1.Text = st. Days. ToString () + "day"
+ St. Hours. ToString () + "hour"
+ St. Minutes. ToString () + "Minute"
+ St. Seconds. ToString () + "Seconds ";

Instances, such as the number of days and seconds before the date and time

Function:

The Code is as follows: Copy code
/// <Summary>
/// Return the time interval between two dates (y: year interval, M: month interval, d: day interval, h: hour interval, m: minute interval, s: second interval)
/// </Summary>
/// <Param name = "Date1"> Start date </param>
/// <Param name = "Date2"> end date </param>
/// <Param name = "Interval"> Interval flag </param>
/// <Returns> return interval indicates the specified interval </returns>
Private int DateDiff (System. DateTime Date1, System. DateTime Date2, string Interval)
{
Double dblYearLen = 365; // The length of the Year, 365 days
Double dblMonthLen = (365/12); // Average number of days per month
System. TimeSpan objT;
ObjT = Date2.Subtract (Date1 );
Switch (Interval)
{
Case "y": // returns the year interval of the date.
Return System. Convert. ToInt32 (objT. Days/dblYearLen );
Case "M": // return the month interval of the date
Return System. Convert. ToInt32 (objT. Days/dblMonthLen );
Case "d": // The number of days before the return date.
Return objT. Days;
Case "h": // returns the hour interval of the date
Return System. Convert. ToInt32 (objT. TotalHours );
Case "m": // return the minute interval of the date
Return System. Convert. ToInt32 (objT. TotalMinutes );
Case "s": // return the second interval of the date
Return System. Convert. ToInt32 (objT. TotalSeconds );
Default:
Break;
}
Return 0;
}

Usage:

The Code is as follows: Copy code

Private void Form1_Load (object sender, System. EventArgs e)
{

System. DateTime dt1, dt2;
Dt1 = new DateTime (November 5, 1 );
Dt2 = new DateTime (November 6, 30 );
System. Console. WriteLine (this. DateDiff (dt1, dt2, "Y "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "M "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "d "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "h "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "m "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "s "));
}

Instance

The Code is as follows: Copy code

DateTime dt1, dt2; // if not the time type, convert it to the time type
TimeSpan d = dt1.Subtract (dt2 );
D. Days =... // This is the number of Days
D. Minutes =... // This is minute

Public enum DateInterval
{
Second, Minute, Hour, Day, Week, Month, Quarter, Year
}
Public sealed class DateTimeManger
{
Private DateTimeManger ()
{}// End of default constructor
Public static long DateDiff (DateInterval Interval, System. DateTime StartDate, System. DateTime EndDate)
{
Long lngDateDiffValue = 0;
System. TimeSpan TS = new System. TimeSpan (EndDate. Ticks-StartDate. Ticks );
Switch (Interval)
{
Case DateInterval. Second:
LngDateDiffValue = (long) TS. TotalSeconds;
Break;
Case DateInterval. Minute:
LngDateDiffValue = (long) TS. TotalMinutes;
Break;
Case DateInterval. Hour:
LngDateDiffValue = (long) TS. TotalHours;
Break;
Case DateInterval. Day:
LngDateDiffValue = (long) TS. Days;
Break;
Case DateInterval. Week:
LngDateDiffValue = (long) (TS. Days/7 );
Break;
Case DateInterval. Month:
LngDateDiffValue = (long) (TS. Days/30 );
Break;
Case DateInterval. Quarter:
LngDateDiffValue = (long) (TS. Days/30)/3 );
Break;
Case DateInterval. Year:
LngDateDiffValue = (long) (TS. Days/365 );
Break;
}
Return (lngDateDiffValue );
} // End of DateDiff
} // End of class

The call method is simple in the form of a function.

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.