For the game I am trying to make I often have to compare dates and as such function or command does not exist in Godot I built one and I am sharing it here if anyone need it.
You have to pass 3 parameters. All tree are strings, first two are dates you are comparing and the last is the compare string in the form of "<","<=","==",">=" or ">". The dates should be formatted like "dd.mm.yyyy".
I don't check for proper formatting so this could be improved.
func compare_dates(firstDate, secondDate, compareSign ):
var firstDay = firstDate.substr(0,2).to_int()
var firstMonth = firstDate.substr(3,2).to_int()
var firstYear = firstDate.substr(6,4).to_int()
var secondDay = secondDate.substr(0,2).to_int()
var secondMonth = secondDate.substr(3,2).to_int()
var secondYear = secondDate.substr(6,4).to_int()
match compareSign:
">":
if firstYear > secondYear:
return true
elif firstYear < secondYear:
return false
else: #ako years are equal compare months
if firstMonth > secondMonth:
return true
elif firstMonth < secondMonth:
return false
else: # if months are equal compare days
if firstDay > secondDay:
return true
elif firstDay < secondDay:
return false
else: #if days are equal
return false
">=":
if firstYear > secondYear:
return true
elif firstYear < secondYear:
return false
else: #ako years are equal compare months
if firstMonth > secondMonth:
return true
elif firstMonth < secondMonth:
return false
else: # if months are equal compare days
if firstDay > secondDay:
return true
elif firstDay < secondDay:
return false
else: #if days are equal
return true
"==":
if firstYear > secondYear:
return false
elif firstYear < secondYear:
return false
else: #ako years are equal compare months
if firstMonth > secondMonth:
return false
elif firstMonth < secondMonth:
return false
else: # if months are equal compare days
if firstDay > secondDay:
return false
elif firstDay < secondDay:
return false
else: #if days are equal
return true
"<=":
if firstYear > secondYear:
return false
elif firstYear < secondYear:
return true
else: #ako years are equal compare months
if firstMonth > secondMonth:
return false
elif firstMonth < secondMonth:
return true
else: # if months are equal compare days
if firstDay > secondDay:
return false
elif firstDay < secondDay:
return true
else: #if days are equal
return true
"<":
if firstYear > secondYear:
return false
elif firstYear < secondYear:
return true
else: #if months are equal compare days
if firstMonth > secondMonth:
return false
elif firstMonth < secondMonth:
return true
else: # if months are equal compare days
if firstDay > secondDay:
return false
elif firstDay < secondDay:
return true
else: #if days are equal
return false