简体   繁体   中英

Xamarin Forms get the hours of the phone

I am a novice in Xamarin.

I am looking for a way to get the local date time of my phone in hours in order to compare it with 19h00 or 7:00pm depending on the country

Here is my code but it is not okey:

// Handle when your app starts
DateTime dt = DateTime.Now.ToLocalTime();
DateTime hour_only = dt.Hour; 
if (hour_only = "19:00") 
{
    Debug.WriteLine("it time "); 
}

If there is a better example I will take ,

thanks

The property Hour of a DateTime is an int type that represents the actual hour in 24 hours format.

As you can see in the Microsoft documentation

public int Hour { get; }

Property Value

Type: System.Int32

The hour component, expressed as a value between 0 and 23.

So if you want to compare the actual hour with 7PM, you have to use the following code:

DateTime dt = DateTime.Now.ToLocalTime();

int hour_only = dt.Hour;

if (hour_only == 19) {

    Debug.WriteLine("it time ");

}

Be careful that use two == symbols to make a comparation, you were using only one:

if (hour_only = "19:00") {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM