Datetime Functions?

C

Chris Wilfong

Guest
Let's say I wanted to make a countdown timer for a deadline, and that deadline is February 4th, 2020 at 6:30 PM. I want it to be displayed like, "You have __ months, __ days, __ hours, and __ minutes."

What's the most efficient way to do this?
Thanks!
 

FrostyCat

Redemption Seeker
One way is to use date_minute_span() and then using div and mod to convert the units.

Create:
Code:
current_datetime = date_current_datetime();
target_datetime = date_create_datetime(2020, 2, 4, 18, 30, 0);
Step:
Code:
current_datetime = date_current_datetime();
Draw:
Code:
var mins = date_minute_span(current_datetime, target_datetime);
draw_text(x, y, "You have " + string(mins div 1440) + " days, " + string((mins div 60) mod 24) + " hours, and " + string(round(mins mod 60)) + " minutes.");
I decided not to use months as a unit as there are long months and short months in a year, and that would just cause confusion at a UX level. But if you want to use 30 days as a standardized month, then use mins div 43200 for months and (mins div 1440) mod 30 for days.
 
Last edited:
C

Chris Wilfong

Guest
Word! This worked perfectly, ty.
Can you help me understand div and and mod? I get how days work but confused about hours and minutes.
 

FrostyCat

Redemption Seeker
Word! This worked perfectly, ty.
Can you help me understand div and and mod? I get how days work but confused about hours and minutes.
div is integer division and mod is the remainder after integer division. The expressions should all be self-explanatory for anyone who remembers third grade math.
 
div is integer division and mod is the remainder after integer division. The expressions should all be self-explanatory for anyone who remembers third grade math.
To add on to this, GMS2 added the modulo operator (%), so you don't have to use the mod keyword.
Code:
var foo = 5 mod 2;
//Functionally identical to
var foo = 5%2;
 
C

Chris Wilfong

Guest
div is integer division and mod is the remainder after integer division. The expressions should all be self-explanatory for anyone who remembers third grade math.
Yeahhh I don't know what elementary school you went to, but no third graders where I live would understand this. But I get it now.
 

rIKmAN

Member
Yeahhh I don't know what elementary school you went to, but no third graders where I live would understand this. But I get it now.
Assuming they taught you to read in elementary school, you can also find the explanations in the manual ;)
div, mod (%) - Division and modulo, where div gives you the amount a value can be divided into producing only an integer quotient, while mod gives you only the remainder of a division.
Note that you can only div or mod using integer values.
 
Top