How do I convert milliseconds to dd:hh:mm using moment.js? - momentjs

I'm trying to get the days, hours and minutes of 9000000 milliseconds, but moment.js is returning 0 days. I'm using Format plugin for the Moment Duration object. https://github.com/jsmreese/moment-duration-format
moment.duration(9000000, "milliseconds").format("dd:hh:mm");
returns "02:30"
How did I get 9000000?
var ms = moment.duration({
days: 1,
hours: 2,
minutes: 30,
})
console.log(ms._milliseconds);
// 9000000

Sounds like humanizeduration is what you are looking for:
humanizeDuration(97320000) // '1 day, 3 hours, 2 minutes'
Here is the github link:
https://github.com/EvanHahn/HumanizeDuration.js

1000 x 60 x 60 x 24 = 86'400'000 milliseconds.
Of course 9 mil is 0 days.
9'000'000 / (1000 x 60 x 60) = 2.5h = 2 hours 30 min
I hope I know how to use calculator
Check this place

Related

how to write this if else statement in R?

Does anyone know how to write an if else statement in R where if the departure delay is more than 15 minutes then the airline has to pay $75 for every minute delayed and if the departure delay is less than 15 minutes then there is no charge?
This is what I wrote but its throwing an error
mutate(`Departure Delay charges`= if_else(`departure_delay`>= '16'|`DEP_DELAY`<='15',75*`departure_delay`, "0" ))
If you need > 15 mins, then the second dep_delay is not necessary. If true it will multiply departure_delay by 75, it it is FALSE - it will stay 0.
DepartureDelayCharges = ifelse(departure_delay >= 16, 75*departure_delay, 0)

momentjs calculates date difference incorrectly

In my angular web application, I want to compare two dates to see if a person is less than 18 years old when she/he entered the company. Here is the code I use to do this:
const dayOfBirth = moment(formControl.value, this.dateFormat, true).startOf('day');
const entranceDateControl = this.wizardFormGroup.get('entranceDate');
const entranceDate = moment(entranceDateControl.value, this.dateFormat, true).startOf('day');
// Check validation rule R3: Age is less than 18 compared to entrance date
const difference = moment.duration(Math.abs(entranceDate.diff(dayOfBirth)));
if (difference.years() < 18) {
const validationMessage = this.getValidationMessage('R3', formControlName);
return validationMessage ? validationMessage.message : null;
}
As you can see, I am using startOf('day') to get rid of any time component so that I only handle dates. I use diff() to get the difference between two dates and then duration() to convert the difference to years, months, days, etc. Using this code, the validation message should NOT show when the person is turning 18 years old on the day when she/he entered the company.
Upon testing this, I came across what is, in my opinion, strange behavior. Depending on months and years used, it gave different results. For instance, for these dates it was Ok:
dayOfBirth = 1998-03-01, 1998-04-01, ..., 2000-02-01
entranceDate = 2016-03-01, 2016-04-01, ..., 2018-02-01
But the following dates returned the validation message:
dayOfBirth = 2000-03-01, 2000-04-01, ..., 2002-02-01
entranceDate = 2018-03-01, 2000-04-01, ..., 2020-02-01
After these dates, i.e. using 2002-03-01 and onward, it works again. I also got wrong result for the dates preceding 1998-03-01.
Now, I had a closer look at the Duration object and I noticed that for the times where it was less than 18 years, it had calculated 864 milliseconds less then when it came to the right conclusion that it was 18 years between the dates.
Correct duration
----------------
dayOfBirth = 1998-03-01, 1998-04-01, ..., 2000-02-01
entranceDate = 2016-03-01, 2016-04-01, ..., 2018-02-01
Duration = 568080000000 ms
Wrong duration
--------------
dayOfBirth = 2000-03-01, 2000-04-01, ..., 2002-02-01
entranceDate = 2018-03-01, 2000-04-01, ..., 2020-02-01
Duration = 567993600000 ms
Duration difference
-------------------
568080000000 - 567993600000 = 86400000 ms = 24 hours = 1 day
Has anyone an explanation for this? Can it be considered a bug in momentjs? Any viable workaround for this?
I didn't go into details in moment source code but it seems duration() is playing tricks with you. Simplify the code and rely only on diffas follow and you should be good (at least it seems to work for the samples you provided). And it's easier on the eyes :)
const moment = require('moment')
const dayOfBirth = moment('2000-03-01').startOf('day');
const entranceDate = moment('2018-03-01').startOf('day');
const difference = entranceDate.diff(dayOfBirth, 'years')
if (difference < 18) {
console.log( '<18')
} else {
console.log( '>=18')
}
will output >=18

Json Format for a TimeSpan that can be bound using Microsoft.Extensions.Configuration

In a project I need to configure some third party library via the Micorosoft.Extensions.Configuration.
The library gives an options class and I used the configurationSection.Bind(optionsClassInstance) method to bind the values.
It works well except the nested TimeSpan value.
I can't figure out what the json structure of a timespan is so it could be bound.
There are no errors. The values from the json are simply not bound.
So far I just used "timespan": { "Days": 0, "Hours": 1, "Minutes": 0 }
Thanks to the answer I tested successfully the given values with the given results:
1.02:03:04.567 = 1 day, 2 hours, 3 minutes, 4 seconds, 567 milliseconds
1.02:03:04 = 1 day, 2 hours, 3 minutes, 4 seconds, 0 milliseconds
02:03:04 = 0 days, 2 hours, 3 minutes, 4 seconds, 0 milliseconds
03:04 = 0 days, 3 hours, 4 minutes, 0 seconds, 0 milliseconds
04 = 4 days, 0 hours, 0 minutes, 0 seconds, 0 milliseconds
Timespan format in .net core is D.HH:mm:nn (so "1.02:03:04" is 1 day, 2 hours, 3 mins, 4 seconds).
javascript wont be able to read that (we use a custom JsonConverter for timespan objects for that reason), but .Net can.
{"timespan":"1.02:03:04"}

Calculating time difference in sas (military time)

I am having trouble figuring out how to calculate duration of a time variable
Any thoughts on how to tackle this?
A military time value encoded as a integer number h,hmm can be processed by converting the number to a SAS time value and then performing delta computations using certain assumptions.
data sleep_log;
input name $ boots_down boots_up;
datalines;
Joe 2000 0600 slept over midnight
Joe 1000 1230 slept into lunch
Joe 1630 1700 30 winks
Joe 0100 0100 out cold!
run;
data sleep_data;
set sleep_log;
down = hms(
int(boots_down / 100) /* extract hours */
, mod(boots_down , 100) /* extract minutes */
, 0 /* seconds not logged, use zero */
);
up = hms(
int(boots_up / 100) /* extract hours */
, mod(boots_up , 100) /* extract minutes */
, 0 /* seconds not logged, use zero */
);
* SAS time values are linear and simple arithmetic can apply;
if up <= down
then delta = '24:00't + up - down; /* presume roll over midnight */
else delta = up - down;
format down up delta time5.;
run;
A more robust log would also record the day, eliminating presumptions and providing a proper time dimension.
You can extract the Hours and Minutes from your numeric military time HHMM , then create a SAS time using HMS() function.
Extract Hours: Divide your HHMM by 100 and save as integer to get hours,
Extract Minutes: get the Remainder (MOD) of HHMM by 100 to get the minutes,
Create a new time variable using HMS(Hour,Minute,Second),
Create a new Datetime for each using DHMS(date,hour,minute,second)
Full Code:
data have;
input sleep awake date_s date_w;
informat date_s date9. date_w date9.;
format sleep z4. awake z4. date_s date9. date_w date9.;
datalines;
2300 0500 12feb2018 13feb2018
2000 0300 11feb2018 12feb2018
0530 1230 10feb2018 10feb2018
;
run;
data want;
set have;
new_sleep_time=hms(int(sleep/100),int(mod(sleep,100)),0);
new_awake_time=hms(int(awake/100),int(mod(awake,100)),0);
dt_awake=dhms(date_w,hour(new_awake_time),minute(new_awake_time),0);
dt_sleep=dhms(date_s,hour(new_sleep_time),minute(new_sleep_time),0);
diff=dt_awake-dt_sleep;
keep new_sleep_time new_awake_time dt_awake dt_sleep diff;
format new_sleep_time time8. new_awake_time time8. diff time8. dt_awake datetime21. dt_sleep datetime21.;
run;
Output:
new_sleep_time=23:00:00 new_awake_time=5:00:00 diff=6:00:00 dt_awake=13FEB2018:05:00:00 dt_sleep=12FEB2018:23:00:00
new_sleep_time=20:00:00 new_awake_time=3:00:00 diff=7:00:00 dt_awake=12FEB2018:03:00:00 dt_sleep=11FEB2018:20:00:00
new_sleep_time=5:30:00 new_awake_time=12:30:00 diff=7:00:00 dt_awake=10FEB2018:12:30:00 dt_sleep=10FEB2018:05:30:00

VBScript echo every 30 seconds

The following code writes to the screen every iteration. Based on my understanding of the DateDiff documentation, it should only write every 30 seconds. What did I do wrong?
lasttime = Now
Do While Not data.eof
'looping through database records
if DateDiff(s,lasttime,Now) >= 30 Then
lasttime = Now
WScript.Echo "It's been 30 seconds..."
End if
Loop
Change this line:
if DateDiff(s,lasttime,Now) >= 30 Then
To this (note the quotes around "s")
if DateDiff("s",lasttime,Now) >= 30 Then

Resources