+13 votes

Hi, I am new to GDScript. Been trying to look at the API but I don't seem to find anything related to date and time. How do I get the system time?

in Engine by (59 points)

4 Answers

+17 votes
Best answer

OS.get_time() returns dictionary with three values: hour, minute and second
example:

var timeDict = OS.get_time();
var hour = timeDict.hour;
var minute = timeDict.minute;
var seconds = timeDict.second;
by (1,297 points)
selected by

Thanks! This is exactly what I'm looking for :)

no problem :)

is it limited only up to the hour? or can we also get the date of the current time?

+5 votes

Here is a method to get it in a more curated form

`:

var time = OS.get_time()
var time_return = String(time.hour) +":"+String(time.minute)+":"+String(time.second)
by (40 points)
0 votes

I had a similar question. I needed the unix timestamp in milliseconds. This function was what I was looking for: https://docs.godotengine.org/en/stable/classes/class_os.html#class-os-method-get-system-time-msecs

by (14 points)
+2 votes

OS.get_time() was deprecated in the latest versions of Godot 3 and was removed from Godot 4.

In both Godot 3 and Godot 4, you can use the singleton Time for this task:

  • Time.get_time_dict_from_system() to get a dictionary with hour, minute, and second (same behavior as the old OS.get_time()).
  • Time.get_time_string_from_system() can be used to get a preformatted string in the format HH:MM:SS.
by (20 points)
edited by

And Time.get_datetime_string_from_system() if you want to include date, the format is YYYY-MM-DDTHH:MM:SS.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.