0 votes

i use this code to regenerate the date and time for my program but i need the time in 12 hours (am, pm) format, not in 24 hours format

var time = OS.get_datetime()
var nameweekday= ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
var namemonth= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var dayofweek = time["weekday"]   # from 0 to 6 --> Sunday to Saturday
var day = time["day"]                         #   1-31
var month= time["month"]               #   1-12
var year= time["year"]             
var hour= time["hour"]                     #   0-23
var minute= time["minute"]             #   0-59
var second= time["second"]             #   0-59

var dateRFC1123 = str(nameweekday[dayofweek]) + " " + str("%02d" % [day]) + " " + str(namemonth[month-1]) + ", " + str("%02d" % [hour]) + ":" + str("%02d" % [minute])

note: this code is not my own i got it from another question here in Godot forum.

Godot version 3.2
in Engine by (50 points)

1 Answer

+1 vote
Best answer

Simple way to do it would be to just subtract 12 from hour and if its less than 0 its am

var time = OS.get_datetime()
var nameweekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
var namemonth = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var dayofweek = time["weekday"]   # from 0 to 6 --> Sunday to Saturday
var day = time["day"]                         #   1-31
var month = time["month"]               #   1-12
var year = time["year"]             
var hour = time["hour"]                     #   0-23
var minute= time["minute"]             #   0-59
var second= time["second"]             #   0-59
var meridian


func _ready():
    if hour - 12 < 0:
        meridian = "am"
    else:
        meridian = "pm"

    var timeanddate12hr = str(nameweekday[dayofweek]) + " " + str("%02d" % [day]) + " " + str(namemonth[month-1]) + ", " + str("%02d" % [abs(hour - 12)]) + ":" + str("%02d" % [minute] + " " + meridian)
    print(timeanddate12hr)
by (46 points)
selected by

This reply looks working i will try it. Thanks

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 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 webmaster@godotengine.org with your username.