Get month and day

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godot_Starter

I have two variables:

var month :int = null
var day :int = null

Now in the _ready() function I want to set the month and day variable to the real month and day.

For example today is the 09.11.2020 and today month should be set to 11 and day should be set to 9.

Thanks for answers!

:bust_in_silhouette: Reply From: jgodfrey

You’re looking for OS.get_datetime(). That returns a dictionary containing a bunch of date/time components.

Something like this should work:

func _ready():
	var time = OS.get_datetime()
	var day = time["day"]
	var month = time["month"]
    print("Month: %s, Day: %s" % [month, day])

Output (for today):

Month: 11, Day: 9

See the docs for details…

Thanks a lot, exactly what I was looking for

Godot_Starter | 2020-11-09 18:48

works, thank you

grymjack | 2023-02-19 19:34

Note that in (at least) 3.5.1, OS.get_datetime() has been deprecated in favor of Time.get_datetime_dict_from_system().

Docs here:

Time — Godot Engine (stable) documentation in English

jgodfrey | 2023-02-19 20:38

Thank you. I had to create a date/time stamp for a record I was posting to a postgresql table. Here is the working function in case anyone else is interested.

func get_time():
	# returning time/date stamp
	var time = Time.get_datetime_dict_from_system()
	var zone = OS.get_time_zone_info()
	var ret_time = (str(time["year"]) + "/" + str(time["month"]) + "/" + str(time["day"]) +
		" " + str(time["hour"]) + ":" + str(time["minute"]) + ":" + str(time["second"]) +
		" " + str(zone['bias']))
	return ret_time

grymjack | 2023-02-20 03:53

In case it’s helpful, that is probably a bit more readable if written using in-built string formatting. So, something like:

func get_time():
    # returning time/date stamp
    var time = Time.get_datetime_dict_from_system()
    var zone = Time.get_time_zone_from_system()
    var ret_time = "%d/%02d/%02d %02d:%02d:%02d %d" % \
        [time.year, time.month, time.day, time.hour, time.minute, time.second, zone.bias]
    return ret_time

Note, that forces most numbers to be 2-digits, zero padded. While it may not be required(?) by postgresql, I’d say it’s certainly more “standard”…

jgodfrey | 2023-02-20 04:12

Also, notice the use of the new Time.get_time_zone_from_system() instead of the OS function (though, that OS function isn’t marked as deprecated like the one mentioned above).

jgodfrey | 2023-02-20 04:35

Thank you, always helpful. Did you get the dl link ok? :slight_smile:

grymjack | 2023-02-20 11:59

ok I feel dumb. I didn’t know you could split a line of code with a ''. Up until now I had just been enclosing the line in a parentheses.

grymjack | 2023-02-20 12:07

Did you get the dl link ok? :slight_smile:

Yes, I did. Apologies for not updating you. I haven’t had a chance to look at it yet, but will try to do so soon…

jgodfrey | 2023-02-20 15:43

I didn’t know you could split a line of code with a ‘/’. Up until now I had just been enclosing the line in a parentheses.

Do know that that’s a backslash ( \ ), not a forward slash ( / ). And, there’s certainly nothing wrong with using parens. In fact, according to the docs:

GDScript allows wrapping statements using multiple lines using parentheses or backslashes. Parentheses are favored in this style guide since they make for easier refactoring. With backslashes, you have to ensure that the last line never contains a backslash at the end. With parentheses, you don’t have to worry about the last line having a backslash at the end.

jgodfrey | 2023-02-20 15:50

doh!..bad typing ////////

grymjack | 2023-02-20 22:00