- Home /
How to get system time?
Is there anyway i can get the time of day from the system, and if so how?
Answer by jashan · Aug 05, 2010 at 11:19 AM
Yes:
DateTime.Now
For this to work, you need to have the namespace that provides DateTime available. There's a few options. You could use the fully qualified class name which includes the namespace. So that would be:
System.DateTime.Now
Or, at the beginning of your file, you could add a using directive:
C#
using System;
UnityScript/JavaScript:
import System;
For full documentation of DateTime, see API documentation of DateTime on MSDN
DateTime is in the System namespace, so you might have to include that. In C#, that's done via using System; in the beginning of your file. I'll check out how that's handled in UnityScript real quick and then edit the answer.
With this, can i just get the time, and not the date, and then convert the time so like 11:30 is 11.5, 1:45 is 13.75?
You could probably do something like DateTime.Now.ToString("HH:mm"); to get only the current time. You might still need an extra parameter, like DateTime.Now.ToString("HH:mm", CultureInfo.InvariantCulture); in which case you'd also have to import System.Globalization (check out the doc of the ToString()-method of DateTime, that should give you all you need). That might also give you your "special formats", maybe (but I never needed that, so I can't say much about it).