How to compare DateTime in C#?
MSDN: DateTime.Compare
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";
Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
// The example displays the following output:
// 8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM
C# correct method to compare 2 date time
Compare datetime as you would compare numbers such as
DateTime expiration_date = newVer.License.Status.Expiration_Date;
DateTime currentDateTime = DateTime.Now;
if( expiration_date < currentDateTime)
{
// expired
}
If you need only date and not time then use
DateTime expiration_date = newVer.License.Status.Expiration_Date.Date;
DateTime currentDateTime = DateTime.Now.Date;
You can also use day difference of two date.
int daydiff = (int)((currentDateTime - expiration_date).TotalDays)
C# compare two DateTimes
You can subtract two dates, and get a TimeSpan :
TimeSpan difference = _effective_date - date_of_submission;
if(difference.TotalDays > 90)
{
// Bingo!
}
Best way to Compare DateTime to trigger code
To compare Dates
, your method is efficient one because according to MSDN
The CompareTo method compares the Ticks property of the current instance and value but ignores their Kind property. Before comparing DateTime objects, make sure that the objects represent times in the same time zone.
So as it does compare Ticks of two instances of DateTime, so it is the efficient method for comparison.
As a side note, if you want to find interval between DateTime Instances then you can use DateTime.Subtraction it will give TimeSpan
of both DateTime instances. So you can find total difference in their minutes, hours, days, seconds, milliseconds by using TimeSpan properties.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15);
DateTime dateNow = DateTime.Now;
TimeSpan interval = dateNow.Subtract(date1);
double totalHours= interval.TotalHours;
double totalMinutes = interval.TotalMinutes;
double totalSeconds= interval.TotalSeconds;
double totalMilliseconds= interval.TotalMilliseconds;
How to compare date time without time portion in C#
You just need to compare DateTime.Today
and DateTime.Date
:
if(_dateJoin.Date > DateTime.Today)
{
// ...
}
else
{
// ...
}
Update:
object
value has date likeDate = {03-16-2016 12:00:00 AM}
when
execute this line
DateTime _dateJoin = DateTime.ParseExact(value.ToString(), "MM/dd/yyyy", null);
then i'm getting error like String was not recognized as a valid DateTime. –
That's a different issue, you have to use the correct format provider:
DateTime _dateJoin = DateTime.Parse(value.ToString(), CultureInfo.InvariantCulture);
with ParseExact
(not necessary in this case):
DateTime _dateJoin = DateTime.ParseExact(value.ToString(), "MM-dd-yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
Related Topics
How to Write Some Data to Excel File(.Xlsx)
Embedding One Dll Inside Another as an Embedded Resource and Then Calling It from My Code
How to Automatically Scroll to the Bottom of a Multiline Text Box
How to Get Intptr from Byte[] in C#
Editing Dictionary Values in a Foreach Loop
Foreach VS Somelist.Foreach(){}
Dictionary Returning a Default Value If the Key Does Not Exist
Understanding Async/Await in C#
Display Image from Database in Asp MVC
How to Get an Oauth 2.0 Authentication Token in C#
Entity Framework Self Referencing Loop Detected
How to Check a Input Ip Fall in a Specific Ip Range
Web API Put Request Generates an Http 405 Method Not Allowed Error
Task Parallel Library Replacement for Backgroundworker
Hyphenated HTML Attributes with ASP.NET MVC
How to Combine More Than Two Generic Lists in C# Zip
How to Use Non-Thread-Safe Async/Await APIs and Patterns with ASP.NET Web API