node.js perf_hooks time output - javascript

I was reading up on the Performance Hooks now available in node and when I try to use one of their timing outputs like performanceEntry.duration, it says the output is in milliseconds. When I console.log out their code examples on that page like console.log(items.getEntries()[0].duration), the time output looks like this:
0.306935 (start mark)
3.656406 (end mark)
then when i use their startTime output items.getEntries()[0].startTime, I get time like this:
5346.841204
All time output on that documentation page says it is in milliseconds. However, I've never seen milliseconds with decimal places in it before. Are these actually seconds being output instead? If so, what would 5346.841204 correlate to exactly?

Related

Moment.js formatted time incorrect

I'am using moment.js for formatting date, time and duration.
From api I get e.g. this string "1540034040". It should be converted to 13:14
In my React app I have a function which returns return moment(time, 'HH:mm').format('HH:mm');, but then I get another time 15:40? I see there is a package moment timezone. But I don't want to install another package.
Is there somehow a way to fix this with just moment or javascript, without installing another package?
The time string you are passing ("1540034040") is having 10 digit long. the moment package is expecting this kind of string in millisecond format( 13 digit long string ).
For example "1540034040000".
For this given string the time will get converting to some other past date with different time because you have passed second instead of milliseconds.
For debugging you should try to print whole date and time of the given string ("1540034040") instead of just hours and minutes. it will make things more clear.
You should try passing the milliseconds. i am sure it will fix the issue.
I got this working by:
first transform my string to a number so I am able to use unix() and then format with moment().format()
const timeString = time;
const timeNumber = parseInt(timeString, 10);
return moment.unix(timeNumber).format('HH:mm');

How to parse "1h30m" style durations into a Moment.js duration

I would like to try and convert time duration strings to a moment.js duration object. An example of the string format is as follows: "1h30m", which should correspond to 1 hour, 30 minutes, and 0 seconds.
My first thought was to use regex so that I could pull the hours, minutes and seconds but I have a feeling that there's a more efficient way to handle it - the end goal is to use these to calculate how long until a command is run - I saw there was a library called momentjs that I feel could possibly handle this, but the docs don't give a clear way on handling duration formatting in the format that I have in mind. I can provide the code I have written so far, though I don't imagine it would be of much help.
Yes, you can use moment.js to do this - but I'm not sure if it's strictly necessary as it can also be accomplished through simple string manipulation.
String Manipulation Approach:
function parseTimeSpan(timeString) {
let parts = timeString.split("h");
return {
hours: Number(parts[0]),
minutes: Number(parts[1].slice(0, -1))
};
}
JsFiddle Here
Note this will only work with strings that contain both the hour and minute component, and does not support seconds.
Moment.JS Approach:
function parseTimeSpan(timeString) {
return moment.duration("PT" + timeString.toUpperCase());
}
JsFiddle Here
This approach is more robust and handles far more use cases, but is slower and requires an external library.

How can I format a string with moment js to suppress minutes if the time ends with ":00"?

I need to format two moment objects to display a time range to users, eg:
6:45PM-9:30PM
I know I can get this with
`${time1.format('h:mmA')}-${time2.format('h:mmA')}`
but I want to suppress minutes when either the start or end time is on the hour, eg:
6:45PM-10PM (instead of 6:45PM-10:00PM)
7PM-9:30PM (instead of 7:00PM-9:30PM)
7PM-10PM (instead of 7:00PM-10:00PM)
I can check to see if the minutes are 0 before and change my formatting string based on that, but that seems inelegant I'd like to do this only using .format if possible. Haven't found anything in the formatting docs about this.
.format does not provide that you want inbuilt. Rather you can use string replace function instead.
`${time1.format('h:mmA').replace(":00","")}-${time2.format('h:mmA').replace(":00","")}`

Moment time difference won't use moment timezone but users computer time

I'm attempting to display a duration ticker for something. The start time is always in London time. It works perfectly for people in England/the same timezone, however when people in other time zones look at the duration it displays the wrong value (If you're in a timezone behind England => negative values/too small values, timezone ahead => value too large).
My solution to this was to use moment-timezone. I added the moment timezone data correctly I've attempted to use this timezone data (code simplified and separated into individual lines for easier readability):
let londonTimeNow = moment().tz('Europe/London'),
jobStartTime = moment(job.start, 'DD/MM/YYYY HH:mm:ss'),
diff = londonTimeNow.diff(jobStartTime);
duration = moment.duration(diff).format('HH:mm:ss', {trim: false});
I was hoping this would then get the current time in London and compare to the start time no matter where you are in the world. However, it seems the diff function converts the time to the user's computer time. I tried formatting the londonTimeNow to be a string, but then the diff function doesn't work.
Note, I've debugged and moment().tz() is working correctly, I've tried with other time zones and it gets the correct time in the zone specified.
Any ideas?
EDIT:
It seems I can get it working by manually setting the offset property of 'londonTimeNow' to 0. However this doesn't feel quite right to me. I'd prefer a solution that seems less like a hack.
You should specify the jobstart time in the same way you declare london time using the same timezone:
jobStartTime = moment.tz(jobStart, 'DD/MM/YYYY HH:mm:ss','Europe/London'),
This will set the job start time using the same timezone.
Could you add expected output or specify what you mean by 'diff converts the time'?
The result of diff is a duration and is not in any timezone.
Other than that, the problem seems to be in not using timezone for task start.
Try this:
let londonTimeNow = moment().tz('Europe/London'),
jobStartTime = moment(job.start, 'DD/MM/YYYY HH:mm:ss').tz('Europe/London'),
diff = londonTimeNow.diff(jobStartTime);
duration = moment.duration(diff).format('HH:mm:ss', {trim: false});

JavaScript + Format duration with Moment.js

I have some JavaScript running in Node. This code dynamically gets a number of seconds. For the sake of this example, I'll choose 180 to represent 3 minutes. I'm trying to format the number of seconds like "3:00" using Moment.js. I'm trying to format the number of seconds using the Moment duration and Moment duration format plugin. My code looks like this:
var duration = 180;
var text = moment.duration(duration, 'seconds').format('mm:ss');
console.log(text);
When I run this, I see text is printed as 00. It's not formatted as I'd expect and I do not understand why. Can someone please explain to me what I'm doing wrong?
Thank you!

Categories

Resources