[C #] Comparison of datetime time sizes

Source: Internet
Author: User

C # Two operations to implement a DateTime time-size comparison

1. Direct comparison

String strTime1 = DateTime.Now.ToString (); String strTime2 = DateTime.Now.AddDays (-1).   ToString ();   DateTime dt1 = Convert.todatetime (strTime1);   DateTime DT2 = Convert.todatetime (strTime2); if (Dt1 > DT2) {//Other actions}

2. Use the CompareTo method to achieve a comparison of two time sizes

String strTime1 = DateTime.Now.ToString (); String strTime2 = DateTime.Now.AddDays (-1).   ToString ();   DateTime dt1 = Convert.todatetime (strTime1);   DateTime DT2 = Convert.todatetime (strTime2);   int n = dt1.compareto (DT2); if (n = = true) {//Other action}

C # DateTimePicker Time format is set to 00:00:00 seconds or 23:59:59

String stime = Convert.todatetime (this.dtpStart.Value).   ToString ("yyyyMMdd000000"); String etime = Convert.todatetime (this.dtpEnd.Value). ToString ("yyyyMMdd235959");

PS : Gets the stime time format as yyyyMMdd000000, such as 20150525000000

Get etime time format for yyyyMMdd235959, e.g. 20150525235959

This article from "Flower Blossom Fall" blog, declined reprint!

[C #] Comparison of datetime time sizes

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.