You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Date time in C#

Ben Liu
Ben Liu

It is quite common to see Date and Time used in the game, here are some frequently used methods:

Get the current date time

// Get the current date and time
DateTime currentDateTime= DateTime.Now;
Console.WriteLine(currentDateTime.ToString());

Get date and time difference between two dates

// Get time difference between two date
DateTime oldDateTime = new DateTime(2020,1,1);
DateTime newDateTime= DateTime.Now;

TimeSpan ts = newDateTime - oldDateTime;

int diffDays = ts.Days;
Console.WriteLine(diffDays);

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp