1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Datetime Functions?

Discussion in 'Programming' started by Chris Wilfong, Jun 9, 2019.

  1. Chris Wilfong

    Chris Wilfong Member

    Joined:
    Jan 31, 2017
    Posts:
    9
    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!
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,851
    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: Jun 9, 2019
    Chris Wilfong likes this.
  3. Chris Wilfong

    Chris Wilfong Member

    Joined:
    Jan 31, 2017
    Posts:
    9
    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.
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,851
    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.
     
  5. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    463
    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;
     
  6. Chris Wilfong

    Chris Wilfong Member

    Joined:
    Jan 31, 2017
    Posts:
    9
    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.
     
  7. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    Assuming they taught you to read in elementary school, you can also find the explanations in the manual ;)
     

Share This Page