Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to datetime now and take the time ı want.ıf equal to two times ı want to run method is equal.

For example:now =>10:00
Want time=> 12:00

İf (time1=time2)
{
Mymethod();
}

What I have tried:

I want to take two time.
f (time1=time2)
{
Mymethod();
}
Posted
Updated 30-Oct-19 23:39pm

 
Share this answer
 
Firstly you need to check the correct operator for equality. The expression
C#
if (time1=time2)

will assign the value of time2 to time1. You need to use the correct operator:
C#
if (time1 == time2)

Secondly, make use of the DateTime Struct (System) | Microsoft Docs[^] which contains methods, operators and properties to solve the majority of issues.
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 31-Oct-19 6:05am    
Apart from the assignment, in C# it will also give a compilation error since if expects a boolean value—that will work just fine in C++ based on the resulting value.
Richard MacCutchan 31-Oct-19 6:15am    
You are correct. I wonder why some of these posters are so afraid to try anything.
Just to add to the other answers, comparing Time values is not just an equality test: DateTime values are stored as a number of ticks since a particular point in time, and a tick is one hundred nanoseconds or one ten-millionth of a second - so comparing the time part of a date time will only be equal if the two times match to the 100 nanosecond point, which is a very short window. That's why most comparisons are not equality tests, they are greater than or equal to checks - the first instance that is over the minimum is the one that signals something to happen.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900