Adding 1 second to TimeSpan not working - c#

I have this code:
private void TimePlayedTimer_Start()
{
timePlayedStr = "00:00:00";
timePlayed = new DispatcherTimer();
timePlayed.Tick += timePlayedTimer_Tick;
timePlayed.Interval = new TimeSpan(0, 0, 0, 1);
timePlayed.Start();
}
void timePlayedTimer_Tick(object sender, object e)
{
TimeSpan ts = TimeSpan.Parse(timePlayedStr);
ts = ts.Add(TimeSpan.FromSeconds(1));
timePlayedStr = ts.ToString();
}
When I debug this line by line, TimeSpan ts would equal "00:00:00" but after line ts = ts.Add(TimeSpan.FromSeconds(1)); it would some how have properties TotalDays = 2.313232439423 , TotalHours = 0.000555555 , TotalMilliseconds = 2000 rather than adding a 1 to the TotalSeconds properties I get these property values returned.
Does anyone know what I am doing wrong?
PS: I am just trying to add a second to the TimeSpan after every tick

The value for TotalDays is actually 2.31481481481481E-05, i.e. 0.0000231481481481481.
The value that you get is exactly what's expected at the second tick, you didn't manage to debug the first tick, and you are just interpreting the values wrong.
The TotalDays, TotalHours and TotalMilliseconds properties show the total value in the TimeSpan translated to that specific measurement, they don't form a value together.
2 seconds is the same as 2000 milliseconds, and the same as 0.000555555 hours.
If you want to look at the components in the value, you should look at the Days, Hours, Minutes, Seconds and Milliseconds properties. There you will find that the Seconds property is 2 and all the others are zero.

I think you're misreading the TotalDays value. When I run similar code I get my TotalDays value of 1.15740740740741E-05. That likely makes sense, one second is probably roughly that fraction of a day.
The Total* properties represent the overall value of the TimeSpan, not the discrete value of each portion of the TimeSpan.
Days, Hours, and Minutes will all be 0, but the Total* properties will represent the entirety of the value, even if those parts are fractional.

Related

How to subtract two times from two datetime and calculate is elapse time from 61 seconds or not?

I'm working on an asp.net core MVC project. This project about identifying online and offline users, I have two datetime, one of the stores in a database, and another is current datetime, and I must know that time stored in a database elapsed from 61 seconds or not?
I subtract two Datetime and finally use TotalSeconds property.but my output is -22095 or 2319208 and so on.
public void CheckUserStatus()
{
DateTime now = DateTime.Now;
var userTime = _context.Sessions.Where(x => x.LastOnline).Select(x => new {x.LastConnectTime, x.Id});
foreach (var time in userTime)
{
TimeSpan diffrence = now.Subtract(time.LastConnectTime);
int mytime = Convert.ToInt32(diffrence.TotalSeconds);
if ( mytime < 61)
{
Console.WriteLine(time.Id);
}
}
}
I expect out of time base on seconds, for example, right now my output is -22095 or 2319208, and so on but I don't know 2319208 is a regular time or not?
You can easily check that like this :
DateTime now = DateTime.Now;
TimeSpan past = now - now.Subtract(TimeSpan.FromSeconds(60));
TimeSpan post = now - now.Subtract(TimeSpan.FromSeconds(61));
Console.WriteLine(now);
// Should be False: Passed time is less than 60 seconds
Console.WriteLine(past.TotalSeconds > 60);
// Should be True: Passed time is more than 60 seconds
Console.WriteLine(post.TotalSeconds > 60);

Calculating time with hours and minutes only

I am attempting to create a timesheet calculator which takes calculates the time an employee works and I am close, with one problem.
As I perform the calculation, I only want hours and minutes to display. I am able to get that done, but that causes an issue. If the employee punches out before a full minute is elapsed, that minute is not included in the calculation.
For example, if an emp punches in at 12:00:30 and punches out at 5:00:29, that last minute is not counted in the calculation, so the time shows as 4:59 instead of 5:00.
How do I get the calculation to be based on the hours and minutes and exclude seconds completely?
This is the code I have:
private void btnPunchOut_Click(object sender, EventArgs e)
{
DateTime stopTime = DateTime.Now;
lblPunchOutTime.Text = stopTime.ToShortTimeString();
TimeSpan timeWorked = new TimeSpan();
timeWorked = stopTime - startTime;
lblTimeWorked.Text = timeWorked.ToString(#"hh\:mm");
}
Use TimeSpan.TotalSeconds perhaps...And then add 30 seconds or more, before you convert it to hours by dividing by 3600.
As in
lblTimeWorked.Text = ((timeWorked.TotalSeconds+30)/3600).ToString("0.00") + " hours";
Use Timespan.TotalHours if you want the hours.
But if you want to be accurate, you should create a separate class dedicated to calculating the hours worked by a staff member. Then you can encapsulate lots of business rules in the dedicated class. Staff have entitlements and overtime, expenses or penalty rates - so this can get complex if done properly.
If you want a calculation that really ignores the seconds, the clearest way to accomplish that is to get rid of the seconds on both the start time and the end time. It might not seem accurate because it allows a difference of one second to become a difference of one minute. But that could still be a valid business rule, that you want to subtract according the the minutes that appeared on the clock rather than the actual elapsed seconds.
In other words,
1:00:01 is adjusted to 1:00:00.
1:00:59 is adjusted to 1:00:00.
1:01:00 is "adjusted" to 1:01:00.
1:01:01 is adjusted to 1:01:00.
You can accomplish that with an extension like this:
public static class TimespanExtensions
{
public static TimeSpan TrimToMinutes(this TimeSpan input)
{
return TimeSpan.FromMinutes(Math.Truncate(input.TotalMinutes));
}
}
(I'm sure there's a more efficient way of truncating the seconds, but at least this is clear.)
Now instead of having to figure out how to calculate the difference while rounding seconds or adding seconds, you just trim the seconds before calculating the difference. Here's a unit test:
[TestMethod]
public void NumberOfMinutesIgnoresSeconds()
{
var startTime = TimeSpan.FromSeconds(59).TrimToMinutes();
var endTime = TimeSpan.FromSeconds(60).TrimToMinutes();
Assert.AreEqual(1, (endTime - startTime).TotalMinutes);
}
One Timespan represents 59 seconds, and the next one is 60, or the first second of the next minute. But if you trim the seconds and then calculate the difference you get exactly one minute.
In the context of your code,
DateTime stopTime = DateTime.Now;
lblPunchOutTime.Text = stopTime.ToShortTimeString();
var timeWorked = stopTime.TrimToMinutes() - startTime.TrimToMinutes();
lblTimeWorked.Text = timeWorked.ToString(#"hh\:mm");

how to set a timer based on numericupdown values?

I have a program set up that has 3 numericupdowns. Their names are secondsN, minutesN, and hoursN. I want to set a timer based on the values of the numericupdowns.
Example: if secondsN has a value of 3 then I want the clock to be set to 3 seconds which I think is 3000 milliseconds.
so how can I do this?
Thanks for your help!
You can combine them into a TimeSpan. Then use the TotalMilliseconds property off of that.
int numberOfHours, numberOfMinutes, numberOfSeconds;
var timeSpan = new TimeSpan(numberOfHours, numberOfMinutes, numberOfSeconds);
myTimer.Interval = timeSpan.TotalMilliseconds;
How about
Timespan ts = new Timespan(
Convert.ToInt32(value_from_hoursN),
Convert.ToInt32(value_from_minutesN),
Convert.ToInt32(value_from_secondsN));
Then double interval = ts.TotalMilliSeconds;

Calculating how many minutes there are between two times

I have a datagridview in my application which holds start and finish times. I want to calculate the number of minutes between these two times. So far I have got:
var varFinish = tsTable.Rows[intCellRow]["Finish Time"];
TimeSpan varTime = (DateTime)varFinish - (DateTime)varValue;
int intMinutes = TimeSpan.FromMinutes(varTime);
But the last line won't compile because it says I am using invalid arguments for the Timespan constructor. I've researched quite a bit about how to calculate the number of minutes between two times, but I'm hitting a bit of a brick wall. Can someone please advise me on the best way to achieve my objective.
EDIT/
Now my code is as follows:
var varFinish = tsTable.Rows[intCellRow]["Finish Time"];
TimeSpan varTime = (DateTime)varFinish - (DateTime)varValue;
int intMinutes = (int)varTime.TotalMinutes;
But I am getting an invalid cast on the second line. Both varFinish and varValue are times e.g. 10:00 and 8:00 say. So not sure why they won't cast to type DateTime?
Try this
DateTime startTime = varValue
DateTime endTime = varTime
TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (minutes): " + span.TotalMinutes );
Edit:
If are you trying 'span.Minutes', this will return only the minutes of timespan [0~59], to return sum of all minutes from this interval, just use 'span.TotalMinutes'.
double minutes = varTime.TotalMinutes;
int minutesRounded = (int)Math.Round(varTime.TotalMinutes);
TimeSpan.TotalMinutes: The total number of minutes represented by this instance.
In your quesion code you are using TimeSpan.FromMinutes incorrectly. Please see the MSDN Documentation for TimeSpan.FromMinutes, which gives the following method signature:
public static TimeSpan FromMinutes(double value)
hence, the following code won't compile
var intMinutes = TimeSpan.FromMinutes(varTime); // won't compile
Instead, you can use the TimeSpan.TotalMinutes property to perform this arithmetic. For instance:
TimeSpan varTime = (DateTime)varFinish - (DateTime)varValue;
double fractionalMinutes = varTime.TotalMinutes;
int wholeMinutes = (int)fractionalMinutes;
You just need to query the TotalMinutes property like this varTime.TotalMinutes
If the difference between endTime and startTime is greater than or equal to 60 Minutes , the statement:endTime.Subtract(startTime).Minutes; will always return (minutesDifference % 60). Obviously which is not desired when we are only talking about minutes (not hours here).
Here are some of the ways if you want to get total number of minutes(in different typecasts):
// Default value that is returned is of type *double*
double double_minutes = endTime.Subtract(startTime).TotalMinutes;
int integer_minutes = (int)endTime.Subtract(startTime).TotalMinutes;
long long_minutes = (long)endTime.Subtract(startTime).TotalMinutes;
string string_minutes = (string)endTime.Subtract(startTime).TotalMinutes;

Not allowing a user to put "Clock Out" time < "Clock In" time?

Public void Fee()
{
TimeSpan span1 = TimeSpan.FromHours(dtmIn.Value.Hour);
TimeSpan span2 = TimeSpan.FromHours(dtmOut.Value.Hour);
TimeSpan span3 = TimeSpan.FromMinutes(dtmIn.Value.Minute);
TimeSpan span4 = TimeSpan.FromMinutes(dtmOut.Value.Minute);
TimeSpan span5 = span2.Subtract(span1) + span4.Subtract(span3);
lblTotal.Text = (span5.TotalHours * 3).ToString("$#.00");
}
I do not want the user to be able to be able to clock in during PM and clock out during AM(basically overnight working). Also, not allowing the clock out time being before the clock in time.
You should call new TimeSpan(hours, minutes, seconds: 0) and check whether the in TimeSpan is > the out TimeSpan.
It appears from your code sample that dtmIn and dtmOut are nullable DateTime variables. If so, all you have to do is this:
if (dtmIn.Value >= dtmOut.Value)
{
//'in' time is equal to or greater than 'out' time
... show my error message ...
}
Of course you will need to ensure the DateTime? variables have a value (i.e. do appropriate error checking before using them in the expression).
You probably need to be a little more specific with your logic. Do you mean...
The user should be able to work overnight? If so, that means you need to check to make sure that the date they clocked in is the same as the date they clocked out. `
For example...
if (dtmIn.Value.Date != dtmOut.Value.Date)
{
...
}
The user should not be able to work more than 24 hours? If so, you should subtract the two dates and use the resulting TimeSpan to see how many days they worked.
For example...
if ((dtmOut.Value - dtmIn.Value).TotalDays > 1)
{
...
}
In neither case should you check the time explicitly. For one, if I worked 25 hours then my check out time would still be after the check in time.

Categories

Resources