Instance 365 (7) --------- use the DateAdd method to add a period of time interval to a specified date, and use the TimeSpan object to obtain the time interval

Source: Internet
Author: User

I. Use the DateAdd method to add a period of time interval to a specified date,

Ii. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using Microsoft. visualBasic; namespace AddDate {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private DateTime G_datetime; // defines the time field private void Frm_Main_Load (object sender, eventArgs e ){ G_datetime = DateTime. now; // obtain the current system time lab_time.Text = G_datetime.ToString (// display the time information "Time: yyyy, month, day, hour, minute, second ");} /* Interval Type: Microsoft. visualBasic. dateInterval is required. The DateInterval enumerated value or String expression of the time interval to be added. Number Type: Required for System. Double. Double. indicates the floating point expression of the number of time intervals to be added. Number can be a positive Number (the future date/time value will be obtained at this time) or a negative Number (the past date/time value will be obtained at this time ). When Interval specifies the hour, minute, or second, this parameter can contain the fractional part. For other types of Interval values, all decimal parts of Number are ignored. DateValue type: required by System. DateTime. Date indicates the expression of the Date and time to be added based on the time interval. DateValue itself has not changed in the calling program. Return Value Type: System. DateTime returns a Date value, which contains the Date and time values that have been added for the specified time interval. */Private void btn_AddM_Click (object sender, EventArgs e) {G_datetime = DateAndTime. dateAdd (// Add one-minute DateInterval to the time field. minute, 1, G_datetime); lab_time.Text = G_datetime.ToString (// display time information "Time: yyyy, M, D, H, m, s, s ");} private void btn_AddH_Click (object sender, EventArgs e) {G_datetime = DateAndTime. dateAdd (// Add one hour DateInterval to the time field. hour, 1, G_datetime); lab_time.Text = G_datetime.ToString (// display time information "Time: yyyy, M, D, H, m, s, s ");} private void btn_addD_Click (object sender, EventArgs e) {G_datetime = DateAndTime. dateAdd (// Add one-day DateInterval to the time field. day, 1, G_datetime); lab_time.Text = G_datetime.ToString (// display the time information "Time: yyyy, month, Day, hour, minute, second ");}}}
3. Use a TimeSpan object to obtain the time interval,

4. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace GetInterval {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private DateTime G_DateTime_First, // defines two time fields G_DateTime_Second; private void btn_First_Click (object sender, EventArgs E) {G_DateTime_First = DateTime. now; // value lab_first.Text = "system time:" + // display time G_DateTime_First.ToString ("yyyy, M, D, H, m, s, fff, Ms") for the time field ");} private void btn_Second_Click (object sender, EventArgs e) {G_DateTime_Second = DateTime. now; // value lab_second.Text = "system time:" + // display time G_DateTime_Second.ToString ("yyyy, M, D, H, m, s, fff, Ms ");} private void btn_Result_Click (object sender, EventArgs e) {TimeSpan P _ Timespan_temp = // initialize the new TimeSpan structure to the specified number of days, hours, minutes, seconds, and milliseconds. Calculate the two time intervals G_DateTime_First> G_DateTime_Second? G_DateTime_First-G_DateTime_Second: G_DateTime_Second-G_DateTime_First; lab_result.Text = string. format (// display interval "interval: {0} day {1} hour {2} minute {3} second {4} millisecond", P_timespan_temp.Days, P_timespan_temp.Hours, // P_timespan_temp.Days obtain the number of days in the current TimeSpan structure. P_timespan_temp.Minutes, P_timespan_temp.Seconds, P_timespan_temp.Milliseconds );}}}

 

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.