Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compare two DateTimes

Tags:

c#

datetime

I have two dates:

DateTime date_of_submission = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"));
DateTime _effective_date = Convert.ToDateTime(TextBox32.Text);

Now the effective date cannot be more than 90 days in the future from date of submission.

How can I do this comparison?

One method that comes to mind is a naive convert date times to strings and then compare dd, mm, yyyy and see if both dates are within 90 days of each other. But I believe there has to be a better solution than that.

like image 815
Philo Avatar asked Mar 21 '14 16:03

Philo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

You can subtract two dates, and get a TimeSpan :

TimeSpan difference = _effective_date - date_of_submission;
if(difference.TotalDays > 90)
{
  // Bingo!
}
like image 114
Sean Avatar answered Sep 17 '22 13:09

Sean


var days = (_effective_date - date_of_submission).Days;
like image 33
Christian Hayter Avatar answered Sep 17 '22 13:09

Christian Hayter