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

sfriedman
Member Posts: 8
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;
Enjoy!
-Sam
(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
0
Comments
-
And have you try to use the duration variable data type? ;-) Try it...
duration := DateTime1 - DateTime2; MESSAGE(''Duration: %1',duration);
0 -
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 solutionDavid Singleton0 -
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.
Sam0 -
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!
Sam0 -
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
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 Singleton0 -
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. : )
-Sam0 -
[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!0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions