Home > Backend Development > C#.Net Tutorial > Using datetime in C#

Using datetime in C#

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-09-01 20:45:15
forward
1133 people have browsed it

在 C# 中使用日期时间

The DateTime class in C# is used to represent dates and times in C#.

The following are some properties of DateTime in C#-

tr>##345
Advanced number Properties and descriptions
1 Date

Get the date component

2 Day

Get the date and month

hourGet the hour of the month

minuteGet the minute of the date

MonthGet the month of the date

Let us see an example to compare dates in C#.

To compare dates in C#, you need to first set up the two dates you want to compare using a DateTime object. We will use DateTime class in C# -

Date 1

DateTime date1 = new DateTime(2018, 07, 20);
Console.WriteLine("Date 1 : {0}", date1);
Copy after login

Date 2

DateTime date2 = new DateTime(2018, 07, 25);
Console.WriteLine("Date 2 : {0}", date2);
Copy after login

Now let us compare two dates in C#. Below is an example of comparing dates in C# -

Example

Live Demonstration

using System;
namespace Program {
   class Demo {
      static int Main() {
         DateTime date1 = new DateTime(2018, 07, 20);
         Console.WriteLine("Date 1 : {0}", date1);

         DateTime date2 = new DateTime(2018, 07, 25);
         Console.WriteLine("Date 2 : {0}", date2);
         if (date1 < date2)
         Console.WriteLine("{0} comes before {1}", date1, date2);
         Console.Read();
         return 0;

      }
   }
}
Copy after login

Output

Date 1 : 7/20/2018 12:00:00 AM
Date 2 : 7/25/2018 12:00:00 AM
7/20/2018 12:00:00 AM comes before 7/25/2018 12:00:00 AM
Copy after login

The above is the detailed content of Using datetime in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template