Comparing datetimes in eval to hours - c#

I've got a datetime and I want to check if there is 24 hours difference between those two. I just don't know how to do that.
So far I've got this:
<%# (DateTime.Now - Convert.ToDateTime(Eval("new_date"))) < 24 ? "Today" : Eval("new_date") %>
It does not work tho :<
#Edit
And this is how datetime in my database looks like for example:
2016-09-18 12:26:14

The difference between 2 DateTimes is a TimeSpan, which has a TotalDays property you could compare to 1..

The result oft subtracting two DateTime values is a TimeSpan which has properties for hours, minutes, etc.
If you've got two DateTime values you can check whether the difference between them is less than 24 hours like this:
(DateTime.Now - otherDateTime).TotalHours < 24

Related

Calculate hour difference between given date and current date in C#

I want to check to see how many hours difference there are between a given date and the current date. I'm looking for a date that's less then 24 hours away!
I thought this would work but it's giving me a true statement when the EventDateTime is Sunday which is greater then 24 hours out!
bool lessThen24Hours = (spaceEvent.EventDateTime - DateTime.Now).Hours < 24 ? true : false;
you want the TotalHours property rather than Hours:
bool lessThen24Hours = (spaceEvent.EventDateTime - DateTime.Now).TotalHours < 24;
Try (spaceEvent.EventDateTime - DateTime.Now).TotalHours.
It gives you all the hours distance between two dates
Use .TotalHours
(spaceEvent.EventDateTime - DateTime.Now).TotalHours < 24
put that inside Math.Abs() if you want the proximity of 24 hour regardless which time is first
Use DateTimeOffset instead of DateTime so that time zones don't mess up your math.

Get Hours and Minutes from Datetime

Out Time :
2013-03-08 15:00:00.000
In Time :
2013-03-08 11:21:03.290
I need to get Hours and Minutes separately for same date from above, when (Out Time - In Time).
How can I do that ?
I think you probably just want:
TimeSpan difference = outTime - inTime;
int hours = (int) difference.TotalHours;
int minutes = difference.Minutes;
Note that Minutes will give you "just the minutes (never more than 59)" whereas TotalHours (truncated towards zero) will give you "the total number of hours" which might be more than 23 if the times are more than a day apart.
You should also consider what you want to do if the values are negative - either consider it, or explicitly rule it out by validating against it.
The Subtract method on the DateTime class will allow you subtract that date from the other date.
It will give you a TimeSpan which will be the difference.
I'll leave it to you to work out the actual code.
http://msdn.microsoft.com/en-GB/library/8ysw4sby.aspx
You can use Hours property and Minutes
link : http://msdn.microsoft.com/en-us/library/system.datetime.hour.aspx

How to format a TimeSpan for hours not days

The following code
Console.WriteLine("{0:%h} hours {0:%m} minutes",
new TimeSpan(TimeSpan.TicksPerDay));
produces this output:
0 hours 0 minutes
What I would like is this output:
24 hours 0 minutes
What am I missing in this format string?
P.S. I know that I could manually bust up the TimeSpan into days and hours, and multiply the two but would rather use a custom format string, as these timespans are being displayed in a silverlight datagrid and people are expecting to see horus, not days.
According to MSDN, using %h will show you
The number of whole hours in the time interval that are not counted as part of days.
I think you will need to use the TotalHours property of the TimeSpan class like:
TimeSpan day= new TimeSpan(TimeSpan.TicksPerDay);
Console.WriteLine("{0} hours {1} minutes", (int)day.TotalHours, day.Minutes);
Update
If you absolutely need to be able to achieve the stated format by passing custom formatters to the ToString method, you will probably need to create your own CustomTimeSpan class. Unfortunately, you cannot inherit from a struct, so you will have to build it from the ground up.
There doesn't seem to be a format option for getting the total hours out of a TimeSpan. Your best bet would be to use the TotalHours property instead:
var mySpan = new TimeSpan(TimeSpan.TicksPerDay);
Console.WriteLine("{0} hours {1} minutes", (int)mySpan.TotalHours, mySpan.Minutes);
TotalHours returns a double as it includes the fractional hours so you need to truncate it to just the integer part.
Another possibility is:
TimeSpan day = new TimeSpan(2,1,20,0); // 2.01:20:00
Console.WriteLine("{0} hours {1} minutes", (int)day.TotalHours, day.Minutes);
Console will show:
49 hours 20 minutes

Difference between 2 DateTimes in Hours? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Showing Difference between two datetime values in hours
Hi,
is there an easy way to get the difference between 2 DateTime-Values in Hours?
I know, its a possibility to calculate itself by get for each day difference 24h, for each month 188h and so on... but is there an easy way given mybe?
Example:
1) 01.02.2010 12:00
2) 03.03.2011 14:00
= ? Hours differnce
It's pretty simple:
TimeSpan diff = secondDate - firstDate;
double hours = diff.TotalHours;
Note that if these DateTime values have been taken locally, the results may not be the number of elapsed hours. For example, you could have one DateTime of midnight and one of 2am, but only one hour had elapsed - because the clocks went forward at 1am. This may or may not be an issue for you. It won't be a problem if you're dealing with UTC DateTime values.
(dateTime1-dateTime2).TotalHours will give a double with the total difference in hours between the two.
date1.Subtract(date2).TotalHours
TimeSpan difference = firstDateTime - secondDateTime;
double diffInHours = difference.TotalHours
DateTime.Subtract(DateTime) will return a TimeSpan that has a TotalHours property.

C# Time calculation confusion :: TimeSpan.FromHours(12) will check if its more than 12 hrs

Will TimeSpan.FromHours check if the time is more than 12 hours 0 minutes and 0 seconds?
Or is it ONLY going to check if the number is more than 12 or not, i.e. ONLY hours? If yes, how do you check if it's not even a second more than 12 hours?
The FromHours method doesn't compare times, it creates a new TimeSpan value. TimeSpan.FromHours(12) creates a TimeSpan with the value 12:00:00.00000.
You can use it to compare it with a TimeSpan value:
if (someTimeSpan > TimeSpan.FromHours(12)) ...
This will not enter the block if someTimeSpan is 12:00:00.00000 but it will if it's 12:00:00.00001 or more.
TimeSpan.FromHours(x) - TimeSpan.FromHours(12) <= TimeSpan.Zero
Or, of course, bypass the TimeSpan entirely before calling FromHours:
if (x - 12 <= 0) return TimeSpan.FromHours(x);
The TimeSpan class represents a span of time, TimeSpan.FromHours(12) is a time span of 12 hours. Maybe you confused it with the DateTime class?

Categories