Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# how does comparing datetime work

using System;
public class Demo {
   public static void Main(){
      DateTime d1 = new DateTime(2019, 12, 20, 6, 20, 40);
      DateTime d2 = new DateTime(2019, 11, 20, 6, 20, 40);
      Console.WriteLine("DateTime 1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1);
      Console.WriteLine("DateTime 2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2);
      int res = DateTime.Compare(d1, d2);
      // returns equal to 0 since d1 is equal to d2
      Console.WriteLine(res);
   }
}
Comment

how to compare datetime in c#

DateTime.Compare(datetime1, datetime2);
/*
datetime1 before datetime2 = -ve
datetime1 equal datetime2 = 0
datetime1 after datetime2 = +ve
*/
Comment

C# compare date values

var target = DateTime.Parse("3/25/2020");
var todaysDate = DateTime.Today;

if(target > todaysDate)
{
    Console.WriteLine("Hello World!");
}
else 
{
    Console.WriteLine("Too Bad");
}
Comment

c# how to compare 2 dates without time

if(dateTime1.Date == dateTime2.Date)
  // or 
if (dateTime1.Date.CompareTo(dateTime2.Date))
{
}
Comment

c# compare dateTime with string

if ( DateTime.Parse(date2,CultureInfo.InvariantCulture) <=  DateTime.Parse(date1,CultureInfo.InvariantCulture))

{
  // perform some code here
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: build cs file 
Csharp :: c# console password 
Csharp :: c# return task list 
Csharp :: how to cap rigidbody velocity 
Csharp :: read file using c# 
Csharp :: destroy the game object if the animator has finished its animation 
Csharp :: length of a string c# 
Csharp :: asp.net textarea disable resize 
Csharp :: dictionary in c# unity 
Csharp :: c# read csv file 
Csharp :: change working directory shell 
Csharp :: c# get excel column number from letter 
Csharp :: how to check type c# 
Csharp :: get all components of type unity 
Csharp :: long number multiplication 
Csharp :: ternary operator in c# 
Csharp :: c# round number up 
Csharp :: c# public static string 
Csharp :: wpf get function name 
Csharp :: null check syntax c# 
Csharp :: c# streamwriter add new line 
Csharp :: Terrain Tools unity missing 
Csharp :: static c# 
Csharp :: Hello World Dotnet 
Csharp :: unity unfreeze position in script 
Csharp :: input field to float unity 
Csharp :: c# replace multiple characters 
Csharp :: c# modify dictionary in loop 
Csharp :: regex for accepting a file name c# 
Csharp :: ? operator 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =