How to calculate the time difference in C ?,

Source: Internet
Author: User
Tags difference between two times

How to calculate the time difference in C ?,

In C #, how to calculate the difference between the two periods. Calculate the difference between two times, which can be calculated to the hour, minute, and second!

<1>
Label1.Text = "15:36:05 ";
Label2.Text = "20:16:35 ";
DateTime d1 = new DateTime );
DateTime d2 = new DateTime );

TimeSpan d3 = d2.Subtract (d1 );

Label3.Text = "difference :"
+ D3.Days. ToString () + "day"
+ D3.Hours. ToString () + "hour"
+ D3.Minutes. ToString () + "Minute"
+ D3.Seconds. ToString () + "seconds ";


<2>
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
DateTime dt1 = Convert. ToDateTime ("00:00:00 ");
DateTime dt2 = Convert. ToDateTime ("00:00:00 ");
This. Label1.Text = this. DateDiff (dt1, dt2 );
}
}
Private 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 ();
DateDiff = ts. days. toString () + "day" + ts. hours. toString () + "Hour" + ts. minutes. toString () + "Minute" + ts. seconds. toString () + "seconds ";
}
Catch
{}
Return dateDiff;
}

<3>
String st1 = "14: 16 ";
String st2 = "14:14 ";
DateTime dt1 = Convert. ToDateTime (st1 );
DateTime dt2 = Convert. ToDateTime (st2 );

Int result = DateTime. Compare (dt1, dt2 );
If the time of st1 is greater than st2, result = 1
If they are equal to 0,
If the value is smaller than-1


In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

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.