Convert Duration, in milliseconds, to Days, Hours, Mins, Sec

sfriedmansfriedman Member Posts: 8
edited 2010-06-14 in NAV Tips & Tricks
Convert a Duration, in milliseconds, to Days, Hours, Minutes, and Seconds
(without complicated FORMAT commands)

The code below has several assumptions:

1. You want to have easy access to the Days, Hours, Minutes, and Seconds between two DateTime variables.
2. Your start date is a DateTime variable is called vFirstDT.
3. Your end date is a DateTime variable is called vSecondDT;
// Init all vars
// vFirstDT                 // DATETIME
// vSecondDT                // DATETIME
lvDuration := 0;            // DECIMAL
lvSeconds := 0;             // DECIMAL
lvMinutes := 0;             // DECIMAL
lvHours := 0;               // DECIMAL
lvDays := 0;                // DECIMAL
datestr := '';              // TEXT
roundup := '';              // TEXT
rounddown := '';            // TEXT
durstr := '';               // TEXT

// Set our rounding variables
roundup := '>';
rounddown := '<';

// Find the duration between two DateTime objects, in milliseconds
lvDuration := vSecondDT - vFirstDT;

// Convert the Duration to Days, Hours, Minutes, and Seconds. Milliseconds? Fuggetaboutit!
lvDuration := lvDuration / 1000;
lvDays := ROUND((lvDuration / 86400), 1, rounddown);
lvHours := ROUND(((lvDuration - (lvDays * 86400)) / 3600), 1, rounddown);
lvMinutes := ROUND(((lvDuration - (lvDays * 86400) - (lvHours * 3600)) / 60), 1, rounddown);
lvSeconds := ROUND(((lvDuration - (lvDays * 86400) - (lvHours * 3600) - (lvMinutes * 60))), 1, rounddown);

// Set some output in a human-readable format
datestr := FORMAT(lvDays) + 'D ';
datestr := datestr + FORMAT(lvHours) + 'H ';
datestr := datestr + FORMAT(lvMinutes) + 'M ';
datestr := datestr + FORMAT(lvSeconds) + 'S';

// return the duration string
durstr := datestr;


Enjoy!
-Sam

Comments

  • kinekine Member Posts: 12,562
    And have you try to use the duration variable data type? ;-) Try it...
      duration := DateTime1 - DateTime2;
      MESSAGE(''Duration: %1',duration);
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    kine wrote:
    And have you try to use the duration variable data type? ;-) Try it...
      duration := DateTime1 - DateTime2;
      MESSAGE(''Duration: %1',duration);
    

    but if you get paid per line of code then its not a good solution :mrgreen:
    David Singleton
  • sfriedmansfriedman Member Posts: 8
    Haha, good enough point

    I wrote this because I was pretty confused by the formatting documentation and I wanted control over how my days, hours, minutes, and seconds display. Also, it seemed like others were looking for similar things but not quite getting the control they wanted.

    Sam
  • sfriedmansfriedman Member Posts: 8
    Although--

    If anyone knows or can post the FORMAT command to achieve the same output, I'd be grateful:

    Like:
    0D 00:00:00

    Thanks!
    Sam
  • David_SingletonDavid_Singleton Member Posts: 5,479
    sfriedman wrote:
    Haha, good enough point

    I wrote this because I was pretty confused by the formatting documentation and I wanted control over how my days, hours, minutes, and seconds display. Also, it seemed like others were looking for similar things but not quite getting the control they wanted.

    Sam

    I was joking, thus the :mrgreen:

    One thing you will find often in Navision is that you can get almost what you need maybe 99% with a couple of lines of code, but that other 1% might take another 25 lines.
    David Singleton
  • sfriedmansfriedman Member Posts: 8
    Haha, got it.

    I am very new to Navision, so it's good to know that more experienced users might look at this in a similar way. : )

    -Sam
  • krikikriki Member, Moderator Posts: 9,094
    [Topic moved from 'NAV/Navision Classic Client' forum to 'NAV Tips & Tricks' forum]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.