Check if current time is given time [duplicate]
This question already has an answer here:
How to compare two DateTime to seconds?
4 answers
Parse string in HH.mm format to TimeSpan
6 answers
In my main method I'm constantly checking if given time is equal to current time. But my code seems to fail. This is a windows forms application, not a console application. Not trying to convert string to time span, need some way of checking if two times are equal.
static void Main()
{
TimeSpan start = TimeSpan.Parse("10:09:00");
while (true)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (now == start)
{
Debug.WriteLine("Fount at " + now + " " + start);
Environment.Exit(1);
}
Debug.WriteLine("Running Loop at " + now + " "+start);
}
}
I tried altering the first line as,
TimeSpan start = TimeSpan.Parse("10:09:00:0000000");
That doesn't seem to work either, Can anybody suggest me a better way?
c#
marked as duplicate by Ken Y-N, Tetsuya Yamamoto, Niranjan Singh, Alexei Levenkov
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 6:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
How to compare two DateTime to seconds?
4 answers
Parse string in HH.mm format to TimeSpan
6 answers
In my main method I'm constantly checking if given time is equal to current time. But my code seems to fail. This is a windows forms application, not a console application. Not trying to convert string to time span, need some way of checking if two times are equal.
static void Main()
{
TimeSpan start = TimeSpan.Parse("10:09:00");
while (true)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (now == start)
{
Debug.WriteLine("Fount at " + now + " " + start);
Environment.Exit(1);
}
Debug.WriteLine("Running Loop at " + now + " "+start);
}
}
I tried altering the first line as,
TimeSpan start = TimeSpan.Parse("10:09:00:0000000");
That doesn't seem to work either, Can anybody suggest me a better way?
c#
marked as duplicate by Ken Y-N, Tetsuya Yamamoto, Niranjan Singh, Alexei Levenkov
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 6:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
How do you check seconds?
– Just code
Nov 21 '18 at 4:44
@Justcode I'm checking seconds as00
in10:09:00
– Nishan Chathuranga
Nov 21 '18 at 4:47
@Nishin ButDateTime.Now.TimeOfDay
isn't.
– John
Nov 21 '18 at 4:48
DateTime.Now.TimeOfDay
has00:00:00:0000000
format, that is why i changed my code toTimeSpan.Parse("10:09:00:0000000")
@John
– Nishan Chathuranga
Nov 21 '18 at 4:50
1
You do realize that DateTime and TimeSpan objects have millisecond (or better) precision. It's very possible that the current time (as you calculate it) will never be exactly equal to yourstart
. Instead, subtract the two times and check that the result is "close enough" to call them equal
– Flydog57
Nov 21 '18 at 4:54
|
show 2 more comments
This question already has an answer here:
How to compare two DateTime to seconds?
4 answers
Parse string in HH.mm format to TimeSpan
6 answers
In my main method I'm constantly checking if given time is equal to current time. But my code seems to fail. This is a windows forms application, not a console application. Not trying to convert string to time span, need some way of checking if two times are equal.
static void Main()
{
TimeSpan start = TimeSpan.Parse("10:09:00");
while (true)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (now == start)
{
Debug.WriteLine("Fount at " + now + " " + start);
Environment.Exit(1);
}
Debug.WriteLine("Running Loop at " + now + " "+start);
}
}
I tried altering the first line as,
TimeSpan start = TimeSpan.Parse("10:09:00:0000000");
That doesn't seem to work either, Can anybody suggest me a better way?
c#
This question already has an answer here:
How to compare two DateTime to seconds?
4 answers
Parse string in HH.mm format to TimeSpan
6 answers
In my main method I'm constantly checking if given time is equal to current time. But my code seems to fail. This is a windows forms application, not a console application. Not trying to convert string to time span, need some way of checking if two times are equal.
static void Main()
{
TimeSpan start = TimeSpan.Parse("10:09:00");
while (true)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (now == start)
{
Debug.WriteLine("Fount at " + now + " " + start);
Environment.Exit(1);
}
Debug.WriteLine("Running Loop at " + now + " "+start);
}
}
I tried altering the first line as,
TimeSpan start = TimeSpan.Parse("10:09:00:0000000");
That doesn't seem to work either, Can anybody suggest me a better way?
This question already has an answer here:
How to compare two DateTime to seconds?
4 answers
Parse string in HH.mm format to TimeSpan
6 answers
c#
c#
edited Nov 21 '18 at 6:54
Alexei Levenkov
84.3k890132
84.3k890132
asked Nov 21 '18 at 4:40
Nishan ChathurangaNishan Chathuranga
451414
451414
marked as duplicate by Ken Y-N, Tetsuya Yamamoto, Niranjan Singh, Alexei Levenkov
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 6:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Ken Y-N, Tetsuya Yamamoto, Niranjan Singh, Alexei Levenkov
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 6:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
How do you check seconds?
– Just code
Nov 21 '18 at 4:44
@Justcode I'm checking seconds as00
in10:09:00
– Nishan Chathuranga
Nov 21 '18 at 4:47
@Nishin ButDateTime.Now.TimeOfDay
isn't.
– John
Nov 21 '18 at 4:48
DateTime.Now.TimeOfDay
has00:00:00:0000000
format, that is why i changed my code toTimeSpan.Parse("10:09:00:0000000")
@John
– Nishan Chathuranga
Nov 21 '18 at 4:50
1
You do realize that DateTime and TimeSpan objects have millisecond (or better) precision. It's very possible that the current time (as you calculate it) will never be exactly equal to yourstart
. Instead, subtract the two times and check that the result is "close enough" to call them equal
– Flydog57
Nov 21 '18 at 4:54
|
show 2 more comments
How do you check seconds?
– Just code
Nov 21 '18 at 4:44
@Justcode I'm checking seconds as00
in10:09:00
– Nishan Chathuranga
Nov 21 '18 at 4:47
@Nishin ButDateTime.Now.TimeOfDay
isn't.
– John
Nov 21 '18 at 4:48
DateTime.Now.TimeOfDay
has00:00:00:0000000
format, that is why i changed my code toTimeSpan.Parse("10:09:00:0000000")
@John
– Nishan Chathuranga
Nov 21 '18 at 4:50
1
You do realize that DateTime and TimeSpan objects have millisecond (or better) precision. It's very possible that the current time (as you calculate it) will never be exactly equal to yourstart
. Instead, subtract the two times and check that the result is "close enough" to call them equal
– Flydog57
Nov 21 '18 at 4:54
How do you check seconds?
– Just code
Nov 21 '18 at 4:44
How do you check seconds?
– Just code
Nov 21 '18 at 4:44
@Justcode I'm checking seconds as
00
in 10:09:00
– Nishan Chathuranga
Nov 21 '18 at 4:47
@Justcode I'm checking seconds as
00
in 10:09:00
– Nishan Chathuranga
Nov 21 '18 at 4:47
@Nishin But
DateTime.Now.TimeOfDay
isn't.– John
Nov 21 '18 at 4:48
@Nishin But
DateTime.Now.TimeOfDay
isn't.– John
Nov 21 '18 at 4:48
DateTime.Now.TimeOfDay
has 00:00:00:0000000
format, that is why i changed my code to TimeSpan.Parse("10:09:00:0000000")
@John– Nishan Chathuranga
Nov 21 '18 at 4:50
DateTime.Now.TimeOfDay
has 00:00:00:0000000
format, that is why i changed my code to TimeSpan.Parse("10:09:00:0000000")
@John– Nishan Chathuranga
Nov 21 '18 at 4:50
1
1
You do realize that DateTime and TimeSpan objects have millisecond (or better) precision. It's very possible that the current time (as you calculate it) will never be exactly equal to your
start
. Instead, subtract the two times and check that the result is "close enough" to call them equal– Flydog57
Nov 21 '18 at 4:54
You do realize that DateTime and TimeSpan objects have millisecond (or better) precision. It's very possible that the current time (as you calculate it) will never be exactly equal to your
start
. Instead, subtract the two times and check that the result is "close enough" to call them equal– Flydog57
Nov 21 '18 at 4:54
|
show 2 more comments
3 Answers
3
active
oldest
votes
Your code does not work since you compare different timespans, and when you add fractional milliseconds it's unlikely they will match.
You can use the following code snippet to compare time:
TimeSpan start = new TimeSpan(11, 49, 0);
while (true)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (Math.Abs((now - start).TotalMilliseconds)<1000)
{
Debug.WriteLine("Found at " + now + " " + start);
Environment.Exit(1);
}
Debug.WriteLine("Running Loop at " + now + " " + start);
}
I think i like your approach better, upvote
– TheGeneral
Nov 21 '18 at 4:54
Worked!. Thank you very much. :)
– Nishan Chathuranga
Nov 21 '18 at 4:56
3
Why does everyone seem to like initializing TimeSpans by parsing a string. That struct has an hour, min, sec constructor.
– Flydog57
Nov 21 '18 at 4:58
add a comment |
Your problem is probably the milliseconds
You could also use an extension to trim, which will give you a truncated time up to the second
public static class Extensions
{
public static DateTime TrimMilliseconds(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0, dt.Kind);
}
}
Full Demo Here
add a comment |
Your "start" value is OK, the problem is that TimeOfDay
will include much more precision than minutes. You need to somehow truncate the value down to minutes.
This is one such way you could do that:
var now = TimeSpan.FromSeconds(Math.Floor(DateTime.Now.TimeOfDay.TotalSeconds));
TotalSeconds will return a non-integer value (i.e. it will include milliseconds as a fraction), so we take that value, floor it to remove the fraction component, and then convert it back into seconds - now without any values shorter than a second.
Of course you can switch out "Seconds" for Minutes, Hours, etc. as necessary.
Upvotes all round, neater floor, although does he really want minutes?
– TheGeneral
Nov 21 '18 at 4:56
@TheGeneral It looks that way, but OP can adjust to taste :-)
– John
Nov 21 '18 at 4:57
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code does not work since you compare different timespans, and when you add fractional milliseconds it's unlikely they will match.
You can use the following code snippet to compare time:
TimeSpan start = new TimeSpan(11, 49, 0);
while (true)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (Math.Abs((now - start).TotalMilliseconds)<1000)
{
Debug.WriteLine("Found at " + now + " " + start);
Environment.Exit(1);
}
Debug.WriteLine("Running Loop at " + now + " " + start);
}
I think i like your approach better, upvote
– TheGeneral
Nov 21 '18 at 4:54
Worked!. Thank you very much. :)
– Nishan Chathuranga
Nov 21 '18 at 4:56
3
Why does everyone seem to like initializing TimeSpans by parsing a string. That struct has an hour, min, sec constructor.
– Flydog57
Nov 21 '18 at 4:58
add a comment |
Your code does not work since you compare different timespans, and when you add fractional milliseconds it's unlikely they will match.
You can use the following code snippet to compare time:
TimeSpan start = new TimeSpan(11, 49, 0);
while (true)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (Math.Abs((now - start).TotalMilliseconds)<1000)
{
Debug.WriteLine("Found at " + now + " " + start);
Environment.Exit(1);
}
Debug.WriteLine("Running Loop at " + now + " " + start);
}
I think i like your approach better, upvote
– TheGeneral
Nov 21 '18 at 4:54
Worked!. Thank you very much. :)
– Nishan Chathuranga
Nov 21 '18 at 4:56
3
Why does everyone seem to like initializing TimeSpans by parsing a string. That struct has an hour, min, sec constructor.
– Flydog57
Nov 21 '18 at 4:58
add a comment |
Your code does not work since you compare different timespans, and when you add fractional milliseconds it's unlikely they will match.
You can use the following code snippet to compare time:
TimeSpan start = new TimeSpan(11, 49, 0);
while (true)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (Math.Abs((now - start).TotalMilliseconds)<1000)
{
Debug.WriteLine("Found at " + now + " " + start);
Environment.Exit(1);
}
Debug.WriteLine("Running Loop at " + now + " " + start);
}
Your code does not work since you compare different timespans, and when you add fractional milliseconds it's unlikely they will match.
You can use the following code snippet to compare time:
TimeSpan start = new TimeSpan(11, 49, 0);
while (true)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (Math.Abs((now - start).TotalMilliseconds)<1000)
{
Debug.WriteLine("Found at " + now + " " + start);
Environment.Exit(1);
}
Debug.WriteLine("Running Loop at " + now + " " + start);
}
edited Nov 21 '18 at 5:02
John
11.9k32038
11.9k32038
answered Nov 21 '18 at 4:49
Access DeniedAccess Denied
5,06621543
5,06621543
I think i like your approach better, upvote
– TheGeneral
Nov 21 '18 at 4:54
Worked!. Thank you very much. :)
– Nishan Chathuranga
Nov 21 '18 at 4:56
3
Why does everyone seem to like initializing TimeSpans by parsing a string. That struct has an hour, min, sec constructor.
– Flydog57
Nov 21 '18 at 4:58
add a comment |
I think i like your approach better, upvote
– TheGeneral
Nov 21 '18 at 4:54
Worked!. Thank you very much. :)
– Nishan Chathuranga
Nov 21 '18 at 4:56
3
Why does everyone seem to like initializing TimeSpans by parsing a string. That struct has an hour, min, sec constructor.
– Flydog57
Nov 21 '18 at 4:58
I think i like your approach better, upvote
– TheGeneral
Nov 21 '18 at 4:54
I think i like your approach better, upvote
– TheGeneral
Nov 21 '18 at 4:54
Worked!. Thank you very much. :)
– Nishan Chathuranga
Nov 21 '18 at 4:56
Worked!. Thank you very much. :)
– Nishan Chathuranga
Nov 21 '18 at 4:56
3
3
Why does everyone seem to like initializing TimeSpans by parsing a string. That struct has an hour, min, sec constructor.
– Flydog57
Nov 21 '18 at 4:58
Why does everyone seem to like initializing TimeSpans by parsing a string. That struct has an hour, min, sec constructor.
– Flydog57
Nov 21 '18 at 4:58
add a comment |
Your problem is probably the milliseconds
You could also use an extension to trim, which will give you a truncated time up to the second
public static class Extensions
{
public static DateTime TrimMilliseconds(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0, dt.Kind);
}
}
Full Demo Here
add a comment |
Your problem is probably the milliseconds
You could also use an extension to trim, which will give you a truncated time up to the second
public static class Extensions
{
public static DateTime TrimMilliseconds(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0, dt.Kind);
}
}
Full Demo Here
add a comment |
Your problem is probably the milliseconds
You could also use an extension to trim, which will give you a truncated time up to the second
public static class Extensions
{
public static DateTime TrimMilliseconds(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0, dt.Kind);
}
}
Full Demo Here
Your problem is probably the milliseconds
You could also use an extension to trim, which will give you a truncated time up to the second
public static class Extensions
{
public static DateTime TrimMilliseconds(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0, dt.Kind);
}
}
Full Demo Here
answered Nov 21 '18 at 4:51
TheGeneralTheGeneral
29.8k63465
29.8k63465
add a comment |
add a comment |
Your "start" value is OK, the problem is that TimeOfDay
will include much more precision than minutes. You need to somehow truncate the value down to minutes.
This is one such way you could do that:
var now = TimeSpan.FromSeconds(Math.Floor(DateTime.Now.TimeOfDay.TotalSeconds));
TotalSeconds will return a non-integer value (i.e. it will include milliseconds as a fraction), so we take that value, floor it to remove the fraction component, and then convert it back into seconds - now without any values shorter than a second.
Of course you can switch out "Seconds" for Minutes, Hours, etc. as necessary.
Upvotes all round, neater floor, although does he really want minutes?
– TheGeneral
Nov 21 '18 at 4:56
@TheGeneral It looks that way, but OP can adjust to taste :-)
– John
Nov 21 '18 at 4:57
add a comment |
Your "start" value is OK, the problem is that TimeOfDay
will include much more precision than minutes. You need to somehow truncate the value down to minutes.
This is one such way you could do that:
var now = TimeSpan.FromSeconds(Math.Floor(DateTime.Now.TimeOfDay.TotalSeconds));
TotalSeconds will return a non-integer value (i.e. it will include milliseconds as a fraction), so we take that value, floor it to remove the fraction component, and then convert it back into seconds - now without any values shorter than a second.
Of course you can switch out "Seconds" for Minutes, Hours, etc. as necessary.
Upvotes all round, neater floor, although does he really want minutes?
– TheGeneral
Nov 21 '18 at 4:56
@TheGeneral It looks that way, but OP can adjust to taste :-)
– John
Nov 21 '18 at 4:57
add a comment |
Your "start" value is OK, the problem is that TimeOfDay
will include much more precision than minutes. You need to somehow truncate the value down to minutes.
This is one such way you could do that:
var now = TimeSpan.FromSeconds(Math.Floor(DateTime.Now.TimeOfDay.TotalSeconds));
TotalSeconds will return a non-integer value (i.e. it will include milliseconds as a fraction), so we take that value, floor it to remove the fraction component, and then convert it back into seconds - now without any values shorter than a second.
Of course you can switch out "Seconds" for Minutes, Hours, etc. as necessary.
Your "start" value is OK, the problem is that TimeOfDay
will include much more precision than minutes. You need to somehow truncate the value down to minutes.
This is one such way you could do that:
var now = TimeSpan.FromSeconds(Math.Floor(DateTime.Now.TimeOfDay.TotalSeconds));
TotalSeconds will return a non-integer value (i.e. it will include milliseconds as a fraction), so we take that value, floor it to remove the fraction component, and then convert it back into seconds - now without any values shorter than a second.
Of course you can switch out "Seconds" for Minutes, Hours, etc. as necessary.
edited Nov 21 '18 at 5:02
answered Nov 21 '18 at 4:54
JohnJohn
11.9k32038
11.9k32038
Upvotes all round, neater floor, although does he really want minutes?
– TheGeneral
Nov 21 '18 at 4:56
@TheGeneral It looks that way, but OP can adjust to taste :-)
– John
Nov 21 '18 at 4:57
add a comment |
Upvotes all round, neater floor, although does he really want minutes?
– TheGeneral
Nov 21 '18 at 4:56
@TheGeneral It looks that way, but OP can adjust to taste :-)
– John
Nov 21 '18 at 4:57
Upvotes all round, neater floor, although does he really want minutes?
– TheGeneral
Nov 21 '18 at 4:56
Upvotes all round, neater floor, although does he really want minutes?
– TheGeneral
Nov 21 '18 at 4:56
@TheGeneral It looks that way, but OP can adjust to taste :-)
– John
Nov 21 '18 at 4:57
@TheGeneral It looks that way, but OP can adjust to taste :-)
– John
Nov 21 '18 at 4:57
add a comment |
How do you check seconds?
– Just code
Nov 21 '18 at 4:44
@Justcode I'm checking seconds as
00
in10:09:00
– Nishan Chathuranga
Nov 21 '18 at 4:47
@Nishin But
DateTime.Now.TimeOfDay
isn't.– John
Nov 21 '18 at 4:48
DateTime.Now.TimeOfDay
has00:00:00:0000000
format, that is why i changed my code toTimeSpan.Parse("10:09:00:0000000")
@John– Nishan Chathuranga
Nov 21 '18 at 4:50
1
You do realize that DateTime and TimeSpan objects have millisecond (or better) precision. It's very possible that the current time (as you calculate it) will never be exactly equal to your
start
. Instead, subtract the two times and check that the result is "close enough" to call them equal– Flydog57
Nov 21 '18 at 4:54