Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare two dates time hours and minutes (not secs)?

Tags:

c#

This is may be silly question. But I am missing logic here. I have to compare dates with date time with hours and minutes (not with seconds).

IF first field time is older then second field execute condition

right now I am doing if (Convert.ToDateTime(newItem["Modified"]) < Convert.ToDateTime(properties.ListItem["Modified"]))

example if("02/12/2015 11:58" < "02/12/2015 12:01") then execute condition.

like image 678
James123 Avatar asked Feb 12 '15 17:02

James123


People also ask

How do you compare two dates without the time portion?

If you want to compare only the month, day and year of two dates, following code works for me: SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); sdf. format(date1). equals(sdf.

How do you compare two date functions?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.


1 Answers

You could create new DateTime objects with mostly the same values, but with seconds set to 0. Example:

DateTime date1WithoutSeconds = new DateTime(dt1.Year, dt1.Month, dt1.Day, dt1.Hour, dt1.Minute, 0);
DateTime date2WithoutSeconds = new DateTime(dt2.Year, dt2.Month, dt2.Day, dt2.Hour, dt2.Minute, 0);

bool b = date1WithoutSeconds < date2WithoutSeconds;
like image 150
ama1111 Avatar answered Sep 24 '22 16:09

ama1111