Reputation: 3977
I need to determined if the date entered by the user is less than or 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 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();
}
}
Upvotes: 15
Views: 115966
Reputation:
You can use DateTime.Compare()
If Result is less than that means first date is less than second and 0 means equal and greater
DateTime dileverydate = Convert.ToDateTime(dileveryDate.Text);
var todaysDate = DateTime.Today;
int result = DateTime.Compare(dileverydate, todaysDate);
Upvotes: 1
Reputation: 3476
You could use TryParse
of TryParseExact
which returns bool
, 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 IsDateBeforeOrToday(string input)
{
DateTime inputTime;
var parseResult = DateTime.TryParse(input, inputTime);
if (!parseResult)
//Do something useful if parse failed.
return inputTime <= DateTime.Now
}
static void Main(string[] args)
{
Console.WriteLine(IsDateBeforeOrToday("03/26/2015"));
Console.ReadKey();
}
}
Upvotes: 3
Reputation: 5156
You could use the DateTime.Compare method. You could do this:
DateTime dTCurrent = DateTime.Now;
DateTime inputDate = DateTime.ParseExact(input, "MM/dd/yyyy", CultureInfo.InvariantCulture);
int result = DateTime.Compare(dTCurrent, inputDate);
The int 'result' would indicate if dTCurrent is less than inputDate (less than 0), same as (0) or greater than (greater than 0).
Upvotes: 6
Reputation: 223412
Instead of converting current date to string and then int
and doing the comparison, convert your parameter date string to DateTime
object and then compare like:
var parameterDate = DateTime.ParseExact("03/26/2015", "MM/dd/yyyy", CultureInfo.InvariantCulture);
var todaysDate = DateTime.Today;
if(parameterDate < todaysDate)
{
}
You can have your method as:
public static bool IsDateBeforeOrToday(string input)
{
DateTime pDate;
if(!DateTime.TryParseExact(input, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out pDate))
{
//Invalid date
//log , show error
return false;
}
return DateTime.Today <= pDate;
}
DateTime.TryParseExact
if you want to avoid exception in
parsing.DateTime.Today
if you only want to compare date and ignore the
time part.Upvotes: 36