This is my code:
IdleTime = System.Environment.TickCount - LastInput.dwTime;
int hour = ((IdleTime + 500) / 86400000);
int min = ((IdleTime + 500) / 60000) - (hour * 60);
int sec = ((IdleTime + 500) / 1000) - (min * 60);
I got a idle timer for this in a timer that tracks the idle time
The seconds works and the min works but im not sure if it will work once the hour hits 24 I think it might bug out on me coz 86400000 millie milliseconds is a day and I have the sec and the min getting data from the previous time like sec is gettings data from the min. Can anyone help?
I recommend that you work with the TimeSpan type to avoid doing the math yourself:
int milliseconds = Environment.TickCount - LastInput.dwTime;
TimeSpan idleTime = TimeSpan.FromMilliseconds(milliseconds + 500);
int hour = (int) idleTime.TotalHours;
int minutes = idleTime.Minutes;
int seconds = idleTime.Seconds;
I would say
IdleTime = System.Environment.TickCount - LastInput.dwTime;
int hours = IdleTime / 3600000;
int minutes = IdleTime / 60000 - hour * 60;
int seconds = IdleTime / 1000 - min * 60;
I'm not sure why you substract 500 from IdleTime.
Related
I have a situation where in I have time strings like
10:20:70
11:65:40
I need to convert them into proper time in hh:mm:ss format using c# console.
For eg : 10:20:70 will be 10:21:10 after fixing
26:12:20 will be 02:12:10 as 26hours to be considered as 2 hours
How to achieve this? Please help me out.
Any help would be appreciated
Split the input and and either use a TimeSpan to get the real representation of the input.
Or use modulo operator % to fix the overflow.
var split = date.Split(":").Select(int.Parse).ToArray();
if(split.Count() != 3) {Console.WriteLine("bad format"); continue;}
/// public TimeSpan (int hours, int minutes, int seconds);
var realTimeSpanRepresentation = new TimeSpan(split[0],split[1],split[2]);
var correctedTimeSpanRepresentation = new TimeSpan(split[0]%24,split[1]%60,split[2]%60);
Console.WriteLine(date+" => "+realTimeSpanRepresentation+" / "+correctedTimeSpanRepresentation);
/// public DateTime (int year, int month, int day, int hour, int minute, int second);
//var realDateTimeRepresentation = new DateTime(1,1,1,split[0],split[1],split[2]); // Will go boom cause overflow
var correctedDateTimeRepresentation = new DateTime(1,1,1,split[0]%24,split[1]%60,split[2]%60);
Console.WriteLine(date+" => "+correctedDateTimeRepresentation);
Result:
10:20:70 => 10:21:10 / 10:20:10
10:20:70 => 01/01/0001 10:20:10
11:65:40 => 12:05:40 / 11:05:40
11:65:40 => 01/01/0001 11:05:40
99:99:99 => 4.04:40:39 / 03:39:39
99:99:99 => 01/01/0001 03:39:39
demo: https://dotnetfiddle.net/Uwb4zc
NB: I nammed it real representation, cause Imo "00:60:00" is one Hour not "00:00:00"
Here is a method that takes a total amount of seconds and gives proper values for how many years, days, hours, minutes and seconds in there.
public static void GetTimeFromSeconds(float secondsTotal, out int s, out int m, out int h, out int d, out int y)
{
s = m = h = d = y = 0;
s = (int)(secondsTotal % 60);
// substruct the seconds remainder from the total amount (75 - 15 = 60, 125 - 5 = 120).
secondsTotal -= s;
// if nothing left then it was less than 1 minute (45 - 0 = 45).
if (secondsTotal < 60)
return;
// secondsTotal / 60 => how many minutes total
// % 60 => how many minutes remain after splitting to whole hours
m = (int)(secondsTotal / 60 % 60);
// substruct the minutes remainder from the total amount (every minute takes 60 secs from the total)
secondsTotal -= m * 60;
// if there's not enough seconds remain in the total to get at least 1 whole hour (3600 secs)
// then it means there was less than 1 hour.
if (secondsTotal < 3600)
return;
// secondsTotal / 3600 => how many hours total
// % 24 => what will remain after splitting to whole days (24 hours)
h = (int)(secondsTotal / 3600 % 24);
// every whole hour takes 3600 secs from the total
secondsTotal -= h * 3600;
// 24 hours = 86400 seconds.
// If there's less remaining than it was less than 24 hours.
if (secondsTotal < 86400)
return;
// secondsTotal/ 86400 => how many days total
// % 365 => how many will remain after splitting to years
d = (int)(secondsTotal / 86400 % 365);
// substruct whole days
secondsTotal -= d * 86400;
// 1 year = 31536000 secs.
// is there enough secs remaining to get a whole year?
if (secondsTotal < 31536000)
return;
y = (int)(secondsTotal / 31536000);
}
So, you could parse your time into separate values
26:70:20 => hours=26, minutes=70, seconds=20
then count the total amount of seconds:
secondsTotal = hours * 3600 + minutes * 60 + seconds
and then use the method above:
int years, days, hours, mins, secs;
GetTimeFromSeconds(secondsTotal, out secs, out mins, out hours, out days, out years);
For 26 hours, 70 mins, 20 secs the results will be days: 1, hours: 3, minutes: 10, secs: 20.
Then format it into the format you need. For example:
TimeSpan properTime = new TimeSpan(hours, mins, secs);
properTime.ToString(#"hh\:mm\:ss");
Candlesticks on stock market charts are created every minute. I have created a count down timer to tell me how many seconds are left until next candlestick is to be created.
//logic for 1 min candlestick
const int MINUTE = 60;
int currentSecond = DateTime.UtcNow.Second;
int nextMin = MINUTE - currentSecond;
minuteLabel.Text = nextMin.ToString();
The chart can also display candlesticks every 5 minutes for a different perspective. So in this scenario a candlestick is created every 5 minutes. This is what I'm having trouble with. How do I create a count down timer to show me how much time is left until the next candlestick is to be created? This is what I have so far:
//inefficient logic for 5 min candlestick
int currentMinute = DateTime.UtcNow.Minute;
int nextFiveMin;
if (currentMinute >= 0 && currentMinute < 5) {
nextFiveMin = ((5 * MINUTE) - (currentMinute * MINUTE)) - currentSecond;
}
else if(currentMinute >= 5 && currentMinute < 10) {
nextFiveMin = ((10 * MINUTE) - (currentMinute * MINUTE)) - currentSecond;
}
else if (currentMinute >= 10 && currentMinute < 15) {
nextFiveMin = ((15 * MINUTE) - (currentMinute * MINUTE)) - currentSecond;
}
//etc all the way to currentMinute > 55
TimeSpan t = TimeSpan.FromSeconds(nextFiveMin);
fiverLabel.Text = t.ToString(#"mm\:ss");
Although this code works fine I think that there's probably a much easier way to implement this that I can't think of.
You can just do this:
int currentMinute = DateTime.UtcNow.Minute;
int diffMinutes = (currentMinute/5 +1) * 5;
int nextFiveMin = ((diffMinutes * MINUTE) - (currentMinute * MINUTE)) - currentSecond;
Another approach:
private void timer1_Tick(object sender, EventArgs e)
{
// from the current time, strip the seconds, then add one minute:
DateTime dt = DateTime.Today.Add(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)).AddMinutes(1);
// keep adding minutes until it's a multiple of 5
while (dt.Minute % 5 != 0)
{
dt = dt.AddMinutes(1);
}
// display how much time until the next five minute mark:
TimeSpan t = dt.Subtract(DateTime.Now);
fiverLabel.Text = t.ToString(#"mm\:ss");
}
I am dealing with this code for countdown timer in asp.net, jQuery, C#.
I have this jQuery code for the countdown timer:
<div id="timelabel"></div>
<script type="text/javascript">
var leave = <%=seconds %>;
CounterTimer();
var interv = setInterval(CounterTimer,1000);
function CounterTimer()
{
var day = Math.floor(leave / ( 60 * 60 * 24))
var hour = Math.floor(leave / 3600) - (day * 24)
var minute = Math.floor(leave / 60) - (day * 24 *60) - (hour * 60)
var second = Math.floor(leave) - (day * 24 *60*60) - (hour * 60 * 60) -(minute*60)
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second<10 ? "0" + second : second;
var remain = day + " days " + hour + ":" + minute + ":" + second;
leave = leave - 1;
document.getElementById("timelabel").innerText = remain;
}
</script>
And I am passing end date from code behind file that is .cs:
public double seconds;
protected void Page_Load(object sender, EventArgs e)
{
seconds = (GetEndTime() - GetStartTime()).TotalSeconds;
}
private DateTime GetStartTime()
{
return DateTime.Now;
}
private DateTime GetEndTime()
{
return new DateTime(2016, 6, 12, 11, 57, 00); //end date yr-month-day hr-mnt-sec
}
I am facing a problem that this timer wont stops when it hits 0 days 00:00:00
it goes beyond that like -1 days 23:48:20. I want to fix this as I don't have that much knowledge about jQuery I am finding it pretty difficult so can someone guide me with needed modifications? Please help. Thank you in advance.
You need to clear interval after it goes to 0 or beyond. Add this to the bottom of your CounterTimer function.
if(leave <= 0) clearInterval(interv);
Okay first off, I'm pretty sure I'm not expected to use TimeSpan for this assignment; rather a formula series which shows the seconds, minutes, and hours in a message box when the user enters the number of seconds in the text box.
Here's where I'm stuck. We're supposed to check our answers with the example: 7565 seconds is 2 hours, 6 minutes, and 5 seconds. However, my code ends up calculating it as 2 hours, 6 minutes, and 6 seconds. It also keeps that answer when the initial number is 7560 seconds. I'm so confused!! It's a conditional scenario, in which the message box shows only the seconds if the user enters under 60 seconds, only minutes + seconds if the user enters between 60 and 3600 seconds, and hours + minutes + seconds if over 3600 seconds is entered. Here is what I have so far, and I'd appreciate any insight as to why my calculation is off :)
Thanks for the answers! But the 7565 isn't a constant; the user can enter any amount of seconds but my professor used 7565 as an example to check if we're on the right track.
private void calculateButton1_Click(object sender, EventArgs e)
{
int totalSeconds, hours, minutes, minutesRemainder, hoursRemainderMinutes, hoursRemainderSeconds;
totalSeconds = int.Parse(secondsTextBox1.Text);
minutes = totalSeconds / 60;
minutesRemainder = totalSeconds % 60;
hours = minutes / 60;
hoursRemainderMinutes = minutes % 60;
hoursRemainderSeconds = hoursRemainderMinutes % 60;
if (totalSeconds < 60)
{
MessageBox.Show(totalSeconds.ToString());
}
else if (totalSeconds < 3600)
{
MessageBox.Show(minutes.ToString() + " minutes, " + minutesRemainder.ToString() + " seconds");
}
else if (totalSeconds>3600)
{
MessageBox.Show(hours.ToString() + " hours, " + hoursRemainderMinutes.ToString() + " minutes, " + hoursRemainderSeconds.ToString() + " seconds");
}
}
Try using modular arithmetics
int totalSeconds = 7565;
int hours = totalSeconds / 3600;
int minutes = (totalSeconds % 3600) / 60;
int seconds = (totalSeconds % 60);
...
if (hours > 0)
MessageBox.Show(String.Format("{0} hours, {1} minutes, {2} seconds", hours, minutes, seconds));
else if (minutes > 0)
MessageBox.Show(String.Format("{0} minutes, {1} seconds", minutes, seconds));
else
MessageBox.Show(String.Format("{0} seconds", seconds));
How can I divide time by using intervals?
like 01:00 divided by 20 mins = 3?
06:00 divided by 2 hours = 3?
/M
I'd just use the TimeSpan object:
int hours = 1;
int minutes = 0;
int seconds = 0;
TimeSpan span = new TimeSpan(hours, minutes, seconds);
double result = span.TotalMinutes / 20; // 3
Don't bother manually doing any conversions, the TimeSpan object with it's TotalHours, TotalMinutes, TotalSeconds properties, etc, do it all for you.
Something like this should work well, I suppose:
public static double SplitTime(TimeSpan input, TimeSpan splitSize)
{
double msInput = input.TotalMilliseconds;
double msSplitSize = splitSize.TotalMilliseconds;
return msInput / msSplitSize;
}
Example; split 1 hour in 20 minute chunks:
double result = SplitTime(new TimeSpan(1,0,0), new TimeSpan(0,20,0));
I guess the method could fairly easily be reworked to return an array of TimeSpan objects containing the different "slices".
First convert everything to seconds.
01:00 => 3600 seconds, 20 mins => 1200 seconds
then you can divide
Convert to minutes and then do the divison.
h - hours
m - minutes
hd - divider hours
md - divider minutes
(h * 60 + m) / (hd * 60 + md)