Getting Time Data of Operational System
In this Article I will talk about how to get Time data of Operational System in your game.
Where can you use this?
You can use this content in games that make use of System Data infos for things such as set event times, give a bonus for daily login, etc.
For use the time in your game is very simple, you must use the namespace System of C#.
What is the System Namespace?
The Microsoft’s documentation say that: “ The System Namespace is responsible for contains reference’s data, and values used frequently, He also contains events, event handlers, interfaces, attributes and processing’s exception ”.
You can see more details in the link: https://docs.microsoft.com/en-us/dotnet/api/system?view=netstandard-2.0#fields .
To get the Time, We will use the field DateTime that is contained in the namespace System.
For this example I will get these data:
- Current Data
- Current Day
- Current Hour
- Current Minutes
- Current Seconds
- Current Milisseconds
- Current Day of Week
- Current Day of Year
- Ticks
- UTCNow
Once I get the data, I will show it in the Text Component.
Now before show the practical example, I will talk about the structure responsible for to get time data, that is the DateTime, it is responsible for work with the Time data.
In the DateTime struct We have some fields as:
– DateTime.Now
– DateTime.UtcNow
– DateTime.Today
– DateTime.Now.Hour
– DateTime.Now.Minute
– DateTime.Now.Second
– DateTime.Now.Miliseconds
– DateTime.Now.Ticks
– DateTime.Now.DayOfWeek
– DateTime.Now.DayOfYear
We also have the methods that will permit manipulating the files, but I will not talk about these methods in this article, because the focus of this article is to show how to get the time’s data of the operational system.
You can see more infos in the official documentation of C# by link: https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netstandard-2.0#methods .
So let’s go for our implementation, this content is very simple to make the implementation, see this example.
public class GettingTime : MonoBehaviour { public Text dateText; private void Update() { dateText.text = "Now: " + DateTime.Now + "\n" + "Utc Now: " + DateTime.UtcNow + "\n" + "Today: " + DateTime.Today + "\n" + "Hours: " + DateTime.Now.Hour + " : " + "Minutes: " + DateTime.Now.Minute + " : " + "Seconds: " + DateTime.Now.Second + "\n" + "Miliseconds: " + DateTime.Now.Millisecond + "\n" + "Ticks: " + DateTime.Now.Ticks + "\n" + "Day of Week: " + DateTime.Now.DayOfWeek + "\n" + "Day of Year: " + DateTime.Now.DayOfYear; } }
So, in this article We talk about how to Get data of the Operational System.
This project is available in my Github.
Follow-me on Instagram for stay knowing when I create new posts.
You can also leave your feedback, and your suggestion, I will read all, and I will answer for you.
Thank you for reading.
Leave a Reply