Time-dependent coloring in C# - c#

I need time-dependent coloring in a project. If the system is 10 minutes past the entrance time, the background will be orange. If it is 20 minutes past it will be red. I found the difference between the two dates using the
DateTime.Parse(timeNow).Subtract(DateTime.Parse(timeLogged));
but I can't compare the result.
if(Convert.ToInt32(DateTime.Parse(timeNow).Subtract(DateTime.Parse(timeLogged)))>10)
Does it have a similar use? Can you help me how to do it?
I am using Google Translate because my English is not very good, and I apologize for the language mistakes I made.

You can subtract DateTime objects. You'll get a TimeSpan. You can use that TimeSpan to determine the difference between the original objects:
DateTime now = DateTime.Parse(timeNow);
DateTime logged = DateTime.Parse(timeLogged);
TimeSpan diff = now - logged;
if (diff.TotalMinutes > 10)
// It's been more than 10 minutes.

You could do this aswell
// If older than 20 min
if(DateTime.Parse(timeLogged) < DateTime.Now.AddMinutes(-20))
{
// Do stuff
}

DateTime.Subtract returns a Timespan object that has a TotalMinutes property, so you could do this:
if (DateTime.Parse(timeNow).Subtract(DateTime.Parse(timeLogged)).TotalMinutes > 10)

Related

Display items that are no more than 6 months old

What is the correct method to display items that are no more than 6 months (180 days) old? I'm using the following code, but it seems to be showing items that don't fit the criterion accurately.
DateTime.Now.Subtract(Convert.ToDateTime(e.DateCreated)).Days <= 180
Where could I have gone wrong?
Edit
Thanks everyone for your help. Turns out the hours were a key factor in deciding the age of an item. I don't really need it to be accurate to the hour, just to the date.
I would just use this to get a date six months ago
var sixMonthsAgo = DateTime.Now.AddDays(-180);
and then compare it with whatever you want to compare. I guess
if (Convert.ToDateTime(e.DateCreated) >= sixMonthsAgo)
in your case.
EDIT:
I performed a test with a test value provided in comments.
var input = DateTime.Parse("2013-06-23 18:14:47.937");
My current date is 21.12.2013 and time is about 11:00 AM.
With that defined, your code yields a result
180.16:39...
So it still meets your requirements, since it is exactly 180 days old + few hours and minutes.
My code yields a result
24.6.2013 about 11:00 AM
and since your date is 23.6. then it is older then the result and therefore does not meet your requirements.
As you can see, the hours play a big role here. So in the end, it very much depends on how you define "180 days ago". If you still feel that neither of the variants work well, give me at least 10 days you compare, both where it works and where it does not work and mark which should be older and which not.
TimeSpan ts = DateTime.Now - Convert.ToDateTime(e.datecreated);
if (ts.TotalDays <= 180)
{
//perform some task
}

difference between two date times

I want to calculate the difference between two DateTimes. This is what I have:
if ((DateTime.Now.Date - TheUTCDateTime.Date).TotalMinutes > 180)
{
ValidObject = false;
}
Basically, I want to make sure that TheUTCDateTime is not more than 3 hours old. Is what I am doing the best way to do this?
You probably don't want to extract the date and maybe want to use UtcNow instead of Now.
You can also use TimeSpan.FromHours for the period:
if ((DateTime.UtcNow - TheUTCDateTime) > TimeSpan.FromHours(3))
or simply
ValidObject = (DateTime.UtcNow - TheUTCDateTime) <= TimeSpan.FromHours(3);
Your approach is ok, but you could improve it a little bit:
TimeSpan span = DateTime.Now - TheUTCDateTime.Date;
ValidObject = span.TotalHours <= 3;
Since you want to check the hours i have used TotalHours, i have used DateTime.Now instead of Date which truncates the time and i set it also to true whereas your code only sets it to false.
If you want to check whether TheUTCDateTime is older than 3 hours, you shouldn't be using the .Date property:
DateTime.UtcNow - TheUTCDateTime > TimeSpan.FromHours(3)

C# get time between two datetime range

Hello everybody i have asp.net mvc4 application where i made countdown something like this:
How i can get how many days, hours, minutes and seconds are remaining with two daetime range
i have tried this does not working on minutes .TotalMinutes
datetime.TotalDays();
Use
Timespan remtime = YourLaunchDateTime - DateTime.Now;
Then:
Use remtime.Days for Days
Use remtime.Hours for Hours
Use remtime.Minutes for Minutes
Use remtime.Seconds for Seconds
Do not use any Total... property as it converts all time into that entity.
You are using remtime.TotalMinutes. This will convert your Time into Minutes.
For eg. if 3 Hours are left this will return 180 Minutes (bcoz 3 * 60 = 180)
TimeSpan span = dateTime1 - dateTime2;
span will contain needed data.
You should use Minutes instead of TotalMinutes. TotalMinutes makes sum of all minutes within the timespan.

Time difference between 2 timespans fails to return expected data

I have created 2 Timespans below:
TimeSpan currentTs = TimeSpan.FromSeconds(43995); //12:13:15
TimeSpan towTime = TimeSpan.FromSeconds(303072); //12:11:12
I'm trying to find the difference in minutes (by the seconds I'm passing it, it looks like they are on different days). I am hoping for around 2 minutes difference, but in reality, I'm getting -57 minutes.
int timeDifference = (int)currentTs.Subtract(towTime).Minutes;
Can someone explain what i am doing wrong?
If you are just looking for the difference between the minutes (not including the days, hours or seconds), then you can use
double timeDifference = currentTs.Minutes - towTime.Minutes; // 2
This is just the difference of the minutes component though. As other people have said, these times are separated by 3 days, so a TimeSpan possibly isn't ideal.
Or, If you want the seconds to be included as well, you can use
TimeSpan minutes1 = new TimeSpan (0, currentTs.Minutes, currentTs.Seconds);
TimeSpan minutes2 = new TimeSpan (0, towTime.Minutes, towTime.Seconds);
double timeDifference = minutes1.TotalMinutes - minutes2.TotalMinutes // 2.05;
You are looking for the TotalMinutes property not Minutes - the later is just the minutes part of the time difference, the former is the full time difference expressed in fractional minutes:
double timeDifference = currentTs.Subtract(towTime).TotalMinutes;
Also easier to read is:
double timeDifference = (currentTs - towTime).TotalMinutes;
I'm guessing you want to completely ignore the fact that the times are different by about 3 days. In that case, if you take the remainder after division by 86400 (the number of seconds in a day) and pass that to TimeSpan.FromSeconds, you should hopefully get what you want:
TimeSpan currentTs = TimeSpan.FromSeconds(43995 % 86400); //12:13:15
TimeSpan towTime = TimeSpan.FromSeconds(303072 % 86400); //12:11:12, ignoring the 3 days.
double timeDifference = (currentTs - towTime).TotalMinutes;
I got the value of timeDifference as 2.05 when I tried this.
Of course, if currentTs is earlier in the day than towTime (perhaps because midnight falls between them), the time difference will come out negative. If you still want to count the number of minutes between the two, assuming that currentTs is always after towTime, then you'll need to add 1440 (the number of minutes in a day) to timeDifference if it works out negative:
if (timeDifference < 0)
{
timeDifference += 1440;
}
Because the second timespan isn't 12:11:12, its 3:12:11:12.
So you're going to get a negative result which would be in the thousands if you did total minutes (since its representing something like -2days, 57 minutes, 57 seconds)...since you just ask for Minutes and not TotalMinutes you just get the -57.
currentTime is less then towTime (you seem to be missing one digit).

C#: calculating time diff

using the following:-
TimeSpan diff = dt2.TimeOfDay - dt1.TimeOfDay;
d1, d2 are 2 variables of type DateTime and they have got values in them
Now I want to check if there's a difference of 12 hours b/w them
if(diff>12)
{
//do stuff
}
now of course it wont wont coz 12 is an int..so how do I check if the time is more than 12 hrs or not ?? need help with this if statement only..thnx
The general solution is to construct a TimeSpan object that corresponds to the cut-off:
if(diff > TimeSpan.FromHours(12))
{
...
}
If the cut-off corresponds to a multiple of a 'nice' unit of time like days, hours, minutes, seconds or milliseconds as in your example, you could use the TotalXXX property of TimeSpan, as others have posted:
if(diff.TotalHours > 12)
{
...
}
To plainly answer your question, you use one of the properties on the TimeSpan struct, typically the TotalHours property. There are other, simpler, answers here that tells you how to do that, so I won't repeat it.
However, this means I must trust your question to be complete, and I think that just ignoring the date portion will give you edge-cases that you at least need to know about.
For instance, with the following two time-of-day values, how much time is between them?
18:00
08:00
Is it -10 hours, or 14hours between these two?
To properly answer your question, personally I would want you to tell me how you intend to use these values, what it means to you, and give a few examples.
use
if( diff.TotalHours > 12){
....
}
Have you actually used intellisense and noticed the Hours and TotalHours properties on the diff TimeSpan object?
TimeSpan.FromHours you need to use to find the difference
TimeSpan diff = dt2.Substract(dt1);
if (diff.TotalHours > 12)
{
// Do something
}

Categories

Resources