|
|
|
|
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
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?
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?
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