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;
Related
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.
I want to divide time in equal intervals in C#. Like from 3:00pm to 6:00pm create time intervals with a gap of 45 minutes (e.g, 3:00pm, 3:45pm, 4:30pm .... 6:00pm.
How can I acheive this in C# ?
You can use the DateTime.Ticks property to define your intervals, and then create a series of DateTime objects based on your defined interval. The example below can be run in LINQpad. Per the documentation, there are 10000000 ticks in one second. With that in mind:
var startTS = Convert.ToDateTime("6/17/2018 15:00:00");
var endTS = Convert.ToDateTime("6/17/2018 18:00:00");
long ticksPerSecond = 10000000;
long ticksPerMinute = ticksPerSecond * 60;
long ticksPer45Min = ticksPerMinute * 45;
long startTSInTicks = startTS.Ticks;
long endTsInTicks = endTS.Ticks;
for(long i = startTSInTicks; i <= endTsInTicks; i+=ticksPer45Min)
{
new DateTime(i).Dump();
}
In LINQpad, the output looks like this:
6/17/2018 15:00:00
6/17/2018 15:45:00
6/17/2018 16:30:00
6/17/2018 17:15:00
Try this
DateTime StartTime = DateTime.Parse("3:0:0");//If pm it should be 15
DateTime EndTime = DateTime.Parse("6:0:0");//If pm it should be 18
while (StartTime!=EndTime)
{
double minuts = +45;
StartTime = StartTime.AddMinutes(minuts);
}
Hope this helps
Datetime.AddMinutes(double value) should do what you are looking for. Just keep on adding until the result of the addition goes over the maximum date/time you have.
NOTE: This assumes you know your interval. If, on the other hand, you require to split a time span in a equal n parts you would require a different approach, as shown here.
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;
considering that each shipment takes two minutes and I have 500 users to send, how to calculate the estimated time?
I tried this:
var seconds = 120; //2minutes
var users = 500;
int t = 0;
int i = 0;
while(i <= users)
{
i++;
t += seconds;
}
how I convert t to something like: xxx hours and yyy minutes and zzz seconds?
thanks in advance.
TimeSpan duration = Timespan.FromSeconds(t);
Convert your seconds to a timespan object, and you will get all the extra information for free
I believe you are looking for something like this
you may intialize it like this
TimeSpan ts = new TimeSpan(0, 2, 0);
and addition in this fashion
ts.Add(new TimeSpan(0, 2, 0));
And your final format can be achieved as this
string.Format("{0} Hours {1} minutes {2} seconds", ts.Hours, ts.Minutes, ts.Seconds);
In case you can use a timer or calculate the entire duration then use that to initialize the timespan. Please modify as you need or add further details to your question
i need to measure time between 2 button press
in Windows-CE C#
how do do it ?
thank's in advance
DateTime.Now may not be precise enough for your needs. Link (Short short version: DateTime is extremely precise, DateTime.Now -> not so much.)
If you want better precision, use the Stopwatch class (System.Diagnostics.Stopwatch).
Stopwatch watch = new Stopwatch();
watch.Start();
// ...
watch.Stop();
long ticks = watch.ElapsedTicks;
Define a variable when the button is clicked once to NOW().
When you click a second time, measure the difference between NOW and your variable.
By doing NOW - a DateTime variable, you get a TimeSpan variable.
DateTime? time;
buttonClick(....)
{
if (time.HasValue)
{
TimeSpan diff = DateTime.Now.Subtract(time.Value);
DoSomethingWithDiff(diff);
time = null;
}
else
{
time = DateTime.Now;
}
}
See the static System.Environment.TickCount property.
This number of milliseconds elapsed since the system started, so calling it twice and subtracting the earlier value from the later will give you the elapsed time in milliseconds.