Simple countdown and datetime. Subtract ()

Source: Internet
Author: User

 

  1. Drag a label and a timer to the main interface and set the timer interval to 1000.
  2. Add a global variable to indicate the time point after 40 minutes (exam time.PrivateDatetime endtime;

  3. Set the endtime value in the window loading event and enable timer. Private   Void Form1_load ( Object Sender, eventargs E)
    {
    Endtime=Datetime. Now. addminutes (40);
    Timer1.enabled= True;
    }

  4. In the timer tick event, use endtime minus the current time to get the remaining time, so as to implement the countdown. Private   Void Timereffectick ( Object Sender, eventargs E)
    {
    Datetime show=Start. Subtract (NewTimespan (datetime. Now. ticks ));
    Label1.text=Show. tolongtimestring ();
    }

note this method: datetime. subtract (), which has more than one overload:
datetime subtract (timespan) and timespan subtract (datetime). When the parameter is of the datetime type, the return value is of the timespan type, when the parameter is timespan, the return value is of the datetime type.

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.