Reputation: 77
To get the real date/time I'm using:
currentDate.text = DateTime.Now.ToShortDateString();
I'm also using playerprefs to determine if the player has played already that day:
{
return PlayerPrefs.HasKey(LastPlayed) &&
FromBase64(PlayerPrefs.GetString(LastPlayed)).Equals(DateTime.Now.ToString(DateFormat));
// Debug.Log("PLayed");
}
public static void SetPlayedToday()
{
PlayerPrefs.SetString(LastPlayed, ToBase64(DateTime.Now.ToString(DateFormat)));
}
private static string ToBase64(string value)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(value));
}
private static string FromBase64(string value)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(value));
}
}
I'd like to show a real time countdown timer counting toward midnight when the game is playable again. Any ideas how to achieve this in Unity?
Upvotes: 0
Views: 92
Reputation: 90749
To get the time that is missing until the next day you can take the current
DateTime now = DateTime.Now;
then get the next day but without any hours or minutes etc:
DateTime nextDay = new DateTime(now.Year, now.Month, now.Day + 1);
and now you an get the difference between both:
TimeSpan remaining = nextDay - now;
Debug.Log($"Time until midnight {remaining}");
Upvotes: 1