how to convert seconds into DD:HH:MM:SS format

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

Hi I have recently tried to create a countdown timer to show the time remaining for the action to occur but my way seems to be lengthy and inefficient.

var time_total = 0
var time_dict = {"D" : 0, "H" : 0, "M" : 0, "S" : 0}


func start_time(given_time):
	time_total = given_time * 3600 # into secs
	convert_time()

func convert_time():
	var time = time_total
	
	while time >= 60: # get minutes
		time -= 60
		time_dict["M"] += 1
	
	time_dict["S"] = time # rest is seconds
	
	while time_dict["M"] >= 60:
		time_dict["M"] -= 60
		time_dict["H"] += 1
	
	while time_dict["H"] >= 24:
		time_dict["H"] -= 24
		time_dict["D"] += 1
	
	print(time_dict)

is there any better way

:bust_in_silhouette: Reply From: Felipe

I tried to do something more readable:

var start_time
var duration

func set_start_time():
	start_time = OS.get_unix_time() #the time at the moment in seconds

func time_convert(time_in_sec):
	var seconds = time_in_sec%60
	var minutes = (time_in_sec/60)%60
	var hours = (time_in_sec/60)/60
	
	#returns a string with the format "HH:MM:SS"
	return "%02d:%02d:%02d" % [hours, minutes, seconds]

func elapsed_time():
	var elapsed_time = OS.get_unix_time() - start_time #actual time - start time

func get_remainnging_time():
	return duration - elapsed_time()