0% found this document useful (0 votes)
812 views

C# - How To Check If Date Is Less Than or Equals To Today's Date - Stack Overflow

The document discusses different ways to check if a date entered by a user is less than or equal to the current date in C#. It provides multiple solutions using DateTime objects and methods like DateTime.ParseExact(), DateTime.Today, and DateTime.Compare() to compare dates without needing to convert them to strings or integers.

Uploaded by

SANKET BASU ROY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
812 views

C# - How To Check If Date Is Less Than or Equals To Today's Date - Stack Overflow

The document discusses different ways to check if a date entered by a user is less than or equal to the current date in C#. It provides multiple solutions using DateTime objects and methods like DateTime.ParseExact(), DateTime.Today, and DateTime.Compare() to compare dates without needing to convert them to strings or integers.

Uploaded by

SANKET BASU ROY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4/10/2019 c# - How to check if date is less than or equals to today's date?

- Stack Overflow

The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in
the 2019 Developer Survey.

How to check if date is less than or equals to Ask Question

today's date?

Come build the future with u

I need to determined if the date


entered by the user is less than or
4 equals to today's date.

I have the following code which


converts the dates to int and than
compare their values. Is there a more
2 efficient or lean way to get this
accomplished with less lines of code?

How do I do this with far less code or


extraneity?

Code:

class Program
{
public static bool IsDateBeforeOrToday(string input)
{
bool result = true;

if(input != null)
{
DateTime dTCurrent = DateTime.Now;
int currentDateValues = Convert.ToInt32(dTCurrent.ToString("MMddyyyy"));
int inputDateValues = Convert.ToInt32(input.Replace("/", ""));

result = inputDateValues <= currentDateValues;


}
else
{
result = true;
}

return result;
}

static void Main(string[] args)


{
Console.WriteLine(IsDateBeforeOrToday("03/26/2015"));
Console.ReadKey();
}
}

c#

https://stackoverflow.com/questions/29258169/how-to-check-if-date-is-less-than-or-equals-to-todays-date 1/4
4/10/2019 c# - How to check if date is less than or equals to today's date? - Stack Overflow
asked Mar 25 '15 at 14:14
Asynchronous
1,749 14 51 78

1 Are you really asking how to convert


the string to a date so you can check
it? Convert.ToDateTime(input) –
DLeh Mar 25 '15 at 14:16

4 Answers

Instead of converting current date to


string and then int and doing the
18 comparison, convert your parameter
date string to DateTime object and
then compare like:

var parameterDate = DateTime.ParseExa


CultureInfo.InvariantCulture);
var todaysDate = DateTime.Today;

if(parameterDate < todaysDate)


{
}

You can have your method as:

public static bool IsDateBeforeOrToda


{
DateTime pDate;
if(!DateTime.TryParseExact(input,
DateTimeStyles.None, out pDate))
{
//Invalid date
//log , show error
return false;
}
return DateTime.Today <= pDate;
}

Use DateTime.TryParseExact if
you want to avoid exception in
parsing.
Use DateTime.Today if you only
want to compare date and
ignore the time part.

answered Mar 25 '15 at 14:16


Habib
183k 23 314 357

@Habib, thanks. These responses


are certainly better than the solution I
designed, which is why I ask the
question, in a effort to code or make
things better. – Asynchronous Mar
25 '15 at 14:46

https://stackoverflow.com/questions/29258169/how-to-check-if-date-is-less-than-or-equals-to-todays-date 2/4
4/10/2019 c# - How to check if date is less than or equals to today's date? - Stack Overflow

Come build the future with u

You could use of


TryParse
TryParseExact which returns bool ,
3 whether parse succeeded or not.

In my first implementation I threw


exception, but it is useless, because
Parse or ParseExact will throw it
automatically if fails. So there is two
options:

Just use Parse and catch


exceptions in Main() ;
Use TryParse and do something
useful in IsDateBeforeOrToday() if
input is wrong.

Implementation:

class Program
{
public static bool IsDateBeforeOr
{
DateTime inputTime;
var parseResult = DateTime.Tr
if (!parseResult)
//Do something useful if
return inputTime <= DateTime.
}

static void Main(string[] args)


{
Console.WriteLine(IsDateBefor
Console.ReadKey();
}
}

edited Mar 25 '15 at 14:31


Patrick Hofman
129k 18 179 238

answered Mar 25 '15 at 14:18


Szer
2,543 11 30

3 If you're going to throw an exception if


TryParse fails, why not just use Parse
instead? – Jon Skeet Mar 25 '15 at
14:21

@JonSkeet yep, you are right. My


bad... – Szer Mar 25 '15 at 14:21

https://stackoverflow.com/questions/29258169/how-to-check-if-date-is-less-than-or-equals-to-todays-date 3/4
4/10/2019 c# - How to check if date is less than or equals to today's date? - Stack Overflow

You could use the


DateTime.Compare method. You
1 could do this:

DateTime dTCurrent = DateTime.Now;


DateTime inputDate = DateTime.ParseEx
CultureInfo.InvariantCulture);

int result = DateTime.Compare(dTCurre

The int 'result' would indicate if


dTCurrent is less than inputDate (less
than 0), same as (0) or greater than
(greater than 0).

answered Mar 25 '15 at 14:23


sr28
2,095 4 17 41

You can use DateTime.Compare() If


Result is less than that means first
0 date is less than second and 0 means
equal and greater

DateTime dileverydate = Convert.ToDat


var todaysDate = DateTime.Today;
int result = DateTime.Compare(dilever

edited Dec 17 '18 at 0:11


Alexander I.
993 2 8 25

answered Dec 16 '18 at 21:24


waqar
1 1

1 How is this different from this answer


from more than 3 years ago? –
Heretic Monkey Dec 16 '18 at 21:30

https://stackoverflow.com/questions/29258169/how-to-check-if-date-is-less-than-or-equals-to-todays-date 4/4

You might also like