How would I calculate time left in winform?
Hi any ideas on how to calculate time left to a specific hour,
i.e. we start countdown with
if currentTime >= TimeSpan.Parse("06:40") && currentTime <= TimeSpan.Parse("07:25")
and then we parse current hour and end hour (in this example 7:25) and make a label show how many minutes and seconds are left.
I've tried making something with substracting timespan now and end time timespan but it didn't work out at all.
EDIT: The main idea is something like this, but I can't get it to work by using TimeSpan neither DateTime
string myTime;
void timer()
{
var endTime = DateTime.Parse(myTime);
var beginTime = DateTime.Now.TimeOfDay;
}
private void timer1_Tick(object sender, EventArgs e)
{
var endTime = DateTime.Parse(myTime);
var beginTime = DateTime.Now.TimeOfDay;
TimeSpan difference = endTime - beginTime;
TimeSpan currentTime = DateTime.Now.TimeOfDay;
if (currentTime >= TimeSpan.Parse("06:40") && currentTime <= TimeSpan.Parse("07:25"))
{
label5.Text = "0";
myTime = "07:25";
timer();
label6.Text = difference;
}
}
c# winforms
add a comment |
Hi any ideas on how to calculate time left to a specific hour,
i.e. we start countdown with
if currentTime >= TimeSpan.Parse("06:40") && currentTime <= TimeSpan.Parse("07:25")
and then we parse current hour and end hour (in this example 7:25) and make a label show how many minutes and seconds are left.
I've tried making something with substracting timespan now and end time timespan but it didn't work out at all.
EDIT: The main idea is something like this, but I can't get it to work by using TimeSpan neither DateTime
string myTime;
void timer()
{
var endTime = DateTime.Parse(myTime);
var beginTime = DateTime.Now.TimeOfDay;
}
private void timer1_Tick(object sender, EventArgs e)
{
var endTime = DateTime.Parse(myTime);
var beginTime = DateTime.Now.TimeOfDay;
TimeSpan difference = endTime - beginTime;
TimeSpan currentTime = DateTime.Now.TimeOfDay;
if (currentTime >= TimeSpan.Parse("06:40") && currentTime <= TimeSpan.Parse("07:25"))
{
label5.Text = "0";
myTime = "07:25";
timer();
label6.Text = difference;
}
}
c# winforms
Can you show us compilable code (like having an if statement with parentheses)? I'm assuming thatcurrentTime
is a DateTime. If so, you can't compare a DateTime with a TimeSpan. What are you really trying to do? In general, you should work with DateTime and TimeSpan quantities (initializing them with constructors or static methods (other than.Parse
), and then convert them to strings only when you need to display them to users
– Flydog57
Nov 19 '18 at 19:36
It is entirely unclear what you are asking about. Of course subtracting two TimeSpans will work; it will produce another TimeSpan indicating the delta between the two TimeSpans you subtracted. Please provide a Minimal, Complete, and Verifiable example demonstrating the problem you have.
– elgonzo
Nov 19 '18 at 19:36
Post some of your code and add some more details
– preciousbetine
Nov 19 '18 at 19:36
Added some code. Hope it will make whole thing easier to understand.
– user10665024
Nov 19 '18 at 20:31
add a comment |
Hi any ideas on how to calculate time left to a specific hour,
i.e. we start countdown with
if currentTime >= TimeSpan.Parse("06:40") && currentTime <= TimeSpan.Parse("07:25")
and then we parse current hour and end hour (in this example 7:25) and make a label show how many minutes and seconds are left.
I've tried making something with substracting timespan now and end time timespan but it didn't work out at all.
EDIT: The main idea is something like this, but I can't get it to work by using TimeSpan neither DateTime
string myTime;
void timer()
{
var endTime = DateTime.Parse(myTime);
var beginTime = DateTime.Now.TimeOfDay;
}
private void timer1_Tick(object sender, EventArgs e)
{
var endTime = DateTime.Parse(myTime);
var beginTime = DateTime.Now.TimeOfDay;
TimeSpan difference = endTime - beginTime;
TimeSpan currentTime = DateTime.Now.TimeOfDay;
if (currentTime >= TimeSpan.Parse("06:40") && currentTime <= TimeSpan.Parse("07:25"))
{
label5.Text = "0";
myTime = "07:25";
timer();
label6.Text = difference;
}
}
c# winforms
Hi any ideas on how to calculate time left to a specific hour,
i.e. we start countdown with
if currentTime >= TimeSpan.Parse("06:40") && currentTime <= TimeSpan.Parse("07:25")
and then we parse current hour and end hour (in this example 7:25) and make a label show how many minutes and seconds are left.
I've tried making something with substracting timespan now and end time timespan but it didn't work out at all.
EDIT: The main idea is something like this, but I can't get it to work by using TimeSpan neither DateTime
string myTime;
void timer()
{
var endTime = DateTime.Parse(myTime);
var beginTime = DateTime.Now.TimeOfDay;
}
private void timer1_Tick(object sender, EventArgs e)
{
var endTime = DateTime.Parse(myTime);
var beginTime = DateTime.Now.TimeOfDay;
TimeSpan difference = endTime - beginTime;
TimeSpan currentTime = DateTime.Now.TimeOfDay;
if (currentTime >= TimeSpan.Parse("06:40") && currentTime <= TimeSpan.Parse("07:25"))
{
label5.Text = "0";
myTime = "07:25";
timer();
label6.Text = difference;
}
}
c# winforms
c# winforms
edited Nov 19 '18 at 20:23
user10665024
asked Nov 19 '18 at 19:31
user10665024user10665024
102
102
Can you show us compilable code (like having an if statement with parentheses)? I'm assuming thatcurrentTime
is a DateTime. If so, you can't compare a DateTime with a TimeSpan. What are you really trying to do? In general, you should work with DateTime and TimeSpan quantities (initializing them with constructors or static methods (other than.Parse
), and then convert them to strings only when you need to display them to users
– Flydog57
Nov 19 '18 at 19:36
It is entirely unclear what you are asking about. Of course subtracting two TimeSpans will work; it will produce another TimeSpan indicating the delta between the two TimeSpans you subtracted. Please provide a Minimal, Complete, and Verifiable example demonstrating the problem you have.
– elgonzo
Nov 19 '18 at 19:36
Post some of your code and add some more details
– preciousbetine
Nov 19 '18 at 19:36
Added some code. Hope it will make whole thing easier to understand.
– user10665024
Nov 19 '18 at 20:31
add a comment |
Can you show us compilable code (like having an if statement with parentheses)? I'm assuming thatcurrentTime
is a DateTime. If so, you can't compare a DateTime with a TimeSpan. What are you really trying to do? In general, you should work with DateTime and TimeSpan quantities (initializing them with constructors or static methods (other than.Parse
), and then convert them to strings only when you need to display them to users
– Flydog57
Nov 19 '18 at 19:36
It is entirely unclear what you are asking about. Of course subtracting two TimeSpans will work; it will produce another TimeSpan indicating the delta between the two TimeSpans you subtracted. Please provide a Minimal, Complete, and Verifiable example demonstrating the problem you have.
– elgonzo
Nov 19 '18 at 19:36
Post some of your code and add some more details
– preciousbetine
Nov 19 '18 at 19:36
Added some code. Hope it will make whole thing easier to understand.
– user10665024
Nov 19 '18 at 20:31
Can you show us compilable code (like having an if statement with parentheses)? I'm assuming that
currentTime
is a DateTime. If so, you can't compare a DateTime with a TimeSpan. What are you really trying to do? In general, you should work with DateTime and TimeSpan quantities (initializing them with constructors or static methods (other than .Parse
), and then convert them to strings only when you need to display them to users– Flydog57
Nov 19 '18 at 19:36
Can you show us compilable code (like having an if statement with parentheses)? I'm assuming that
currentTime
is a DateTime. If so, you can't compare a DateTime with a TimeSpan. What are you really trying to do? In general, you should work with DateTime and TimeSpan quantities (initializing them with constructors or static methods (other than .Parse
), and then convert them to strings only when you need to display them to users– Flydog57
Nov 19 '18 at 19:36
It is entirely unclear what you are asking about. Of course subtracting two TimeSpans will work; it will produce another TimeSpan indicating the delta between the two TimeSpans you subtracted. Please provide a Minimal, Complete, and Verifiable example demonstrating the problem you have.
– elgonzo
Nov 19 '18 at 19:36
It is entirely unclear what you are asking about. Of course subtracting two TimeSpans will work; it will produce another TimeSpan indicating the delta between the two TimeSpans you subtracted. Please provide a Minimal, Complete, and Verifiable example demonstrating the problem you have.
– elgonzo
Nov 19 '18 at 19:36
Post some of your code and add some more details
– preciousbetine
Nov 19 '18 at 19:36
Post some of your code and add some more details
– preciousbetine
Nov 19 '18 at 19:36
Added some code. Hope it will make whole thing easier to understand.
– user10665024
Nov 19 '18 at 20:31
Added some code. Hope it will make whole thing easier to understand.
– user10665024
Nov 19 '18 at 20:31
add a comment |
1 Answer
1
active
oldest
votes
Use DateTime instead of TimeSpan when parsing points in time. TimeSpan is for durations. The difference between two points in time (DateTime) will be a duration (TimeSpan).
var end = DateTime.Parse("21:00");
var now = DateTime.Now; // Could also be some other point in time
TimeSpan timeLeft = end-now;
Console.WriteLine(timeLeft);
Result:
00:24:25.8581440
If you don't like the seconds and fractions of seconds, you can use a custom format, e.g.
Console.WriteLine(timeLeft.ToString("hh\:mm"));
That seems to work but the problem is that I need to display time left as text of a label and not just in console, and visual studio won't let me convert TimeSpan to string.
– user10665024
Nov 19 '18 at 20:32
@user10665024: did you see thattimeLeft.ToString()
piece of code? That's a string. You can use it anywhere.
– Thomas Weller
Nov 19 '18 at 22:10
Oh actually I missed it somehow. I'll try it out thanks!
– user10665024
Nov 19 '18 at 22:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381422%2fhow-would-i-calculate-time-left-in-winform%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use DateTime instead of TimeSpan when parsing points in time. TimeSpan is for durations. The difference between two points in time (DateTime) will be a duration (TimeSpan).
var end = DateTime.Parse("21:00");
var now = DateTime.Now; // Could also be some other point in time
TimeSpan timeLeft = end-now;
Console.WriteLine(timeLeft);
Result:
00:24:25.8581440
If you don't like the seconds and fractions of seconds, you can use a custom format, e.g.
Console.WriteLine(timeLeft.ToString("hh\:mm"));
That seems to work but the problem is that I need to display time left as text of a label and not just in console, and visual studio won't let me convert TimeSpan to string.
– user10665024
Nov 19 '18 at 20:32
@user10665024: did you see thattimeLeft.ToString()
piece of code? That's a string. You can use it anywhere.
– Thomas Weller
Nov 19 '18 at 22:10
Oh actually I missed it somehow. I'll try it out thanks!
– user10665024
Nov 19 '18 at 22:48
add a comment |
Use DateTime instead of TimeSpan when parsing points in time. TimeSpan is for durations. The difference between two points in time (DateTime) will be a duration (TimeSpan).
var end = DateTime.Parse("21:00");
var now = DateTime.Now; // Could also be some other point in time
TimeSpan timeLeft = end-now;
Console.WriteLine(timeLeft);
Result:
00:24:25.8581440
If you don't like the seconds and fractions of seconds, you can use a custom format, e.g.
Console.WriteLine(timeLeft.ToString("hh\:mm"));
That seems to work but the problem is that I need to display time left as text of a label and not just in console, and visual studio won't let me convert TimeSpan to string.
– user10665024
Nov 19 '18 at 20:32
@user10665024: did you see thattimeLeft.ToString()
piece of code? That's a string. You can use it anywhere.
– Thomas Weller
Nov 19 '18 at 22:10
Oh actually I missed it somehow. I'll try it out thanks!
– user10665024
Nov 19 '18 at 22:48
add a comment |
Use DateTime instead of TimeSpan when parsing points in time. TimeSpan is for durations. The difference between two points in time (DateTime) will be a duration (TimeSpan).
var end = DateTime.Parse("21:00");
var now = DateTime.Now; // Could also be some other point in time
TimeSpan timeLeft = end-now;
Console.WriteLine(timeLeft);
Result:
00:24:25.8581440
If you don't like the seconds and fractions of seconds, you can use a custom format, e.g.
Console.WriteLine(timeLeft.ToString("hh\:mm"));
Use DateTime instead of TimeSpan when parsing points in time. TimeSpan is for durations. The difference between two points in time (DateTime) will be a duration (TimeSpan).
var end = DateTime.Parse("21:00");
var now = DateTime.Now; // Could also be some other point in time
TimeSpan timeLeft = end-now;
Console.WriteLine(timeLeft);
Result:
00:24:25.8581440
If you don't like the seconds and fractions of seconds, you can use a custom format, e.g.
Console.WriteLine(timeLeft.ToString("hh\:mm"));
edited Nov 19 '18 at 19:37
answered Nov 19 '18 at 19:36


Thomas WellerThomas Weller
28.4k1063135
28.4k1063135
That seems to work but the problem is that I need to display time left as text of a label and not just in console, and visual studio won't let me convert TimeSpan to string.
– user10665024
Nov 19 '18 at 20:32
@user10665024: did you see thattimeLeft.ToString()
piece of code? That's a string. You can use it anywhere.
– Thomas Weller
Nov 19 '18 at 22:10
Oh actually I missed it somehow. I'll try it out thanks!
– user10665024
Nov 19 '18 at 22:48
add a comment |
That seems to work but the problem is that I need to display time left as text of a label and not just in console, and visual studio won't let me convert TimeSpan to string.
– user10665024
Nov 19 '18 at 20:32
@user10665024: did you see thattimeLeft.ToString()
piece of code? That's a string. You can use it anywhere.
– Thomas Weller
Nov 19 '18 at 22:10
Oh actually I missed it somehow. I'll try it out thanks!
– user10665024
Nov 19 '18 at 22:48
That seems to work but the problem is that I need to display time left as text of a label and not just in console, and visual studio won't let me convert TimeSpan to string.
– user10665024
Nov 19 '18 at 20:32
That seems to work but the problem is that I need to display time left as text of a label and not just in console, and visual studio won't let me convert TimeSpan to string.
– user10665024
Nov 19 '18 at 20:32
@user10665024: did you see that
timeLeft.ToString()
piece of code? That's a string. You can use it anywhere.– Thomas Weller
Nov 19 '18 at 22:10
@user10665024: did you see that
timeLeft.ToString()
piece of code? That's a string. You can use it anywhere.– Thomas Weller
Nov 19 '18 at 22:10
Oh actually I missed it somehow. I'll try it out thanks!
– user10665024
Nov 19 '18 at 22:48
Oh actually I missed it somehow. I'll try it out thanks!
– user10665024
Nov 19 '18 at 22:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381422%2fhow-would-i-calculate-time-left-in-winform%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you show us compilable code (like having an if statement with parentheses)? I'm assuming that
currentTime
is a DateTime. If so, you can't compare a DateTime with a TimeSpan. What are you really trying to do? In general, you should work with DateTime and TimeSpan quantities (initializing them with constructors or static methods (other than.Parse
), and then convert them to strings only when you need to display them to users– Flydog57
Nov 19 '18 at 19:36
It is entirely unclear what you are asking about. Of course subtracting two TimeSpans will work; it will produce another TimeSpan indicating the delta between the two TimeSpans you subtracted. Please provide a Minimal, Complete, and Verifiable example demonstrating the problem you have.
– elgonzo
Nov 19 '18 at 19:36
Post some of your code and add some more details
– preciousbetine
Nov 19 '18 at 19:36
Added some code. Hope it will make whole thing easier to understand.
– user10665024
Nov 19 '18 at 20:31