question

Liam avatar image
Liam asked

How to get the ban expire in HOURS

Hello! I am making a game, and was wondering how would I show a player how long they were banned from ONLY IN HOURS. Here is my code:

         if (error.Error == PlayFabErrorCode.AccountBanned) 
         {
             Debug.Log("The user is banned from Funny Apes.");
    
             foreach (var item in error.ErrorDetails)
             {
                 computerManager.ShowErrorMessage("You have been banned from playing Funny Apes.\nReason: " + item.Key + "\n Time Left: " + item.Value[0]);
             }
         }
         else
         {
             computerManager.ShowErrorMessage("Unable to authenticate with PlayFab. For more info, read our FAQ in our Discord!");
         }

Can anyone help me? The current code shows the date, minutes, and hours. How would I make it show it only shows hours? (eg. "You have been banned from Funny Apes. Reason: Testing, Expires: 1h".

apisunity3dcommunity
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Xiao Zha avatar image
Xiao Zha answered

You could calculate the time difference between the current UTC time and the Ban expire time. Here is the example code you could refer to.

 // Get the current UTC time
     DateTime now = DateTime.UtcNow;
        
     //Parsing BanExpireTime
     DateTime BanExpireTime = DateTime.Parse(item.Value[0]);
        
     // Calculate time difference
     TimeSpan difference = BanExpireTime - now;
        
     // Display in hours
     double hours = difference.TotalHours;
        
     Debug.Log(hours);
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.