C # Compare size of two dates two case resolution

Source: Internet
Author: User

Method 1:

Datetime.compare (T1,T2) compares two date size, row front small, row in the back big , for example: 2011-2-1 is less than 2012-3-2
The return value is less than 0: T1 is less than T2.
The return value equals zero: T1 equals T2.
The return value is greater than 0: T1 is greater than T2.

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace ConsoleApplication1  {      class program      {          static void Main (string[] args)         {               DateTime T1 = DateTime.Parse ("2011-2-2");             DateTime t2 = datetime.parse ("2011-3-1");              if (Datetime.compare (t1, T2) > 0)                  Console.WriteLine ("T1 > t2");             if (Datetime.compare (t1, t2) = = 0)                   Console.WriteLine ("T1 = = t2");             if (Datetime.compare (t1, T2) < 0)                   Console.WriteLine ("T1 < T2");}}    


Method 2.

Using system;using system.collections;public class myclass{public static void Main () {DateTime dt1 = DateTime.Parse ("2006 -04-01 ");D atetime DT2 = DateTime.Parse (" 2006-05-01 "); TimeSpan ts = dt2. Subtract (DT1); Console.WriteLine (TS. Totaldays); Console.ReadLine ();}}


System.TimeSpan diff1 = Date2. Subtract (date1);
This method can be used to specify the difference in value
Subtract two datetime directly, the result is a timespan, so that you can take the timespan of days and other properties

C # Compare size of two dates two case resolution

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.