Calculate the number of Ticks passed between two times, and calculate the 2-time Ticks.

Source: Internet
Author: User

Calculate the number of Ticks passed between two times, and calculate the 2-time Ticks.

Ticks is a cycle that stores one hundred nanoseconds, converted to seconds, and 10 million to one second.
We need to calculate the number of Ticks between two times, which can be achieved using the following method, using two time subtraction.

The result is a positive number, which is a later time minus an earlier time. Otherwise, it is a negative number, that is, the earlier time minus the later time.

Create an object:

 

Class Ag {private DateTime _ StartDate; public DateTime StartDate {get {return _ StartDate;} set {_ StartDate = value ;}} private DateTime _ EndDate; public DateTime EndDate {get {return _ EndDate;} set {_ EndDate = value ;}} public Ag (DateTime startDate, DateTime endDate) {this. _ StartDate = startDate; this. _ EndDate = endDate;} public void DurationTicks () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); Console. writeLine (ts. ticks );}}Source Code


Run the preceding program on the console:



 
Now, the actual situation is not to calculate the Ticks between two time periods, but to calculate the seconds, minutes, hours, and days between two time periods.
To meet the above requirements, we need to understand some basic knowledge:
1 day = 24;
1 hour = 60 minutes;
1 minute = 60 seconds;
1 sec = 10000000 Ticks;

Therefore, based on the preceding calculation result (Ticks) and formula, the second, minute, hour, and day between two periods can be calculated.

In the category, add the following four methods:



Public void DurationSeconds () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); Console. writeLine ("Duraction Seconds: {0}", ts. ticks/0000000l);} public void DurationMinutes () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); Console. writeLine ("Duraction Minutes: {0}", ts. ticks/(0000000l * 60);} public void DurationHours () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); Console. writeLine ("Duraction Hours: {0}", ts. ticks/(0000000l * 60*60);} public void DurationDays () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); Console. writeLine ("Duraction Days: {0}", ts. ticks/(0000000l * 60*60*24 ));}Source Code

 

After writing the method, test the above method on the console:

 

The above calculation method is a bit primitive. MSDN has provided the corresponding method to calculate the second, minute, hour, and day between two times. For example:
TotalSeconds (), TotalMinutes (), TotalHours (), TotalDays ();
To demonstrate and test, Insus. NET creates another four methods:



Public void TotalSeconds () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); Console. writeLine ("Duraction Seconds: {0}", ts. totalSeconds);} public void TotalMinutes () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); Console. writeLine ("Duraction Minutes: {0}", ts. totalMinutes);} public void TotalHours () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); Console. writeLine ("Duraction Hours: {0}", ts. totalHours);} public void TotalDays () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); Console. writeLine ("Duraction Days: {0}", ts. totalDays );}Source Code

 

 

Run the program again:

 


The final ready-made method encapsulates the unit conversion and formula calculation.

Careful users can find that the two results are somewhat different, with no decimals or decimals. In fact, we can solve this problem by converting the base number within one second to the double data type. That is to say, put the incoming L --> D:

Public void DurationSeconds () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); // Console. writeLine ("Duraction Seconds: {0}", ts. ticks/0000000l); Console. writeLine ("Duraction Seconds: {0}", ts. ticks/0000000d);} public void DurationMinutes () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); // Console. writeLine ("Duraction Minutes: {0}", ts. ticks/(0000000l * 60); Console. writeLine ("Duraction Minutes: {0}", ts. ticks/(0000000d * 60);} public void DurationHours () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); // Console. writeLine ("Duraction Hours: {0}", ts. ticks/(0000000l * 60*60); Console. writeLine ("Duraction Hours: {0}", ts. ticks/(0000000d * 60*60);} public void DurationDays () {TimeSpan ts = (_ EndDate-_ StartDate ). duration (); // Console. writeLine ("Duraction Days: {0}", ts. ticks/(0000000l * 60*60*24); Console. writeLine ("Duraction Days: {0}", ts. ticks/(0000000d * 60*60*24 ));}Source Code

 

This time, we run the program again to see if the results are the same as those calculated using the built-in method:


OK. In this case, you can use the built-in ready-made method and write it by yourself.

The result should be written here, but let's look back at the code we have written. There is one sentence:

TimeSpan ts = (_EndDate - _StartDate).Duration();


Every method has code redundancy. We can simplify it and write a method or attribute:


Public TimeSpan Ts {get {return (_ EndDate-_ StartDate). Duration () ;}} public long Ticks {get {return Ts. Ticks ;}}Source Code

 

After one refactoring, you only need one sentence of code in the eight methods:

 

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.