BrightCove Video Duration Data type - javascript

How we convert Bright Cove video duration to HH:MM:SSS in Javascript?

Brightcove's API returns the duration in milliseconds. Divide by 1000 then apply one of the answers for formatting seconds as HH:MM:SS e.g. JavaScript seconds to time string with format hh:mm:ss

Related

How to time duration calculate in react js

I want to calculate the time duration with the current time and another time. In my problem the current time and all ready time in data base time format are different, this format is given format: 2020-11-07 , 22:52
but now time format is 30/10/2020 , 20:50:34 . So I have a problem with this diffrent format.
You can convert both values to timestamps (in milliseconds) from their own formats and then calculate the difference in milliseconds with simple math.
If you are having trouble parsing the values, you can check Date documentation. An example can be:
let birthday = new Date('1995-12-17T03:24:00')

How to get unix timestamp in nanoseconds with JavaScript?

I'm using moment and I have a datetime like 2020-11-04 21:01:54.434 in a moment object. I would like to obtain the unix timestamp in nanoseconds. I'm trying things like moment.valueOf() but they don't seem to be working up to nanosecond precision. Given that the moment object has information about the nanoseconds, is there anyway I can extract it as the number of seconds since unix epoch?
Math.round(moment.getTime() * 1000)
Because moment.getTime will return you time in milliseconds, so you have to convert it in nanoseconds

Javascript: Calculating time duration on ajax response

I am trying to calculate the time duration of a tasks, that I get from an ajax response.
Following are my table values:
Jobid techid, techtype, notes, starttime, stoptime
1 1 Brakes Break disc needed to be changed 2020-07-16 13:00:00 2020-07-16 13:40:00
1 2 Oil Change Replaced oil 2020-07-17 08:00:00 2020-07-17 09:00:00
1 3 Cleaning Cleaned the vehicle 2020-07-17 10:00:00 2020-07-17 10:30:00
On my ajax response, in the above case, I am getting 3 objects each having the start time, and stop time. I want to calculate total time spent in hours and minutes.
Is there an easy way to calculate the total duration?
With a string like 2020-07-16 13:00:00 you can construct a JS Date and get the milliseconds since the UNIX epoch with getTime() like so
new Date('2020-07-16 13:00:00').getTime()
Or, if you prefer, as pointed out by #Yousaf in the comments you can actually just use the - operator with Dates directly and get the millisecond difference
// resolves to 3600000, or 1 hour in milliseconds
new Date('2020-07-16 13:00:00') - new Date('2020-07-16 12:00:00')
Using that, you can get the difference in milliseconds between any two dates, and convert that to hours / minutes / whatever with straightforward arithmetic.
You can simply use Date to construct a date and then minus the start time from the end time.
Here I use getTime to get the millisecond difference, divide by 1000 to get seconds and divide by 60 to get minutes.
You could also use getMonth and such if you have bigger differences.
const starttime = '2020-07-16 13:00:00'
const stoptime = '2020-07-16 13:40:00'
const duration = new Date(stoptime) - new Date(starttime)
console.log(duration / 1000 / 60)
[UPDATE]
I think you can check this answer, but basically you should convert each date to js Date, get the milliseconds and just calculate endtime - startime.
const timelapse = new Date(endtime).getTime() - new Date(startime).getTime();
From there, you transform that in the unit you need (e.g: seconds = milliseconds/1000);
Sorry, my bad for writing fast.

Moment.js returning 2 days if duration is 24 hours

I need to have a function where the user can insert a certain timeframe (e.g. 1 week or 5 days and 12 hours). Duration from Moment.js looked the most promising.
The code below returns 2 00:00, where 2 equals the numbers of days. This should be 1 because there are only 24 hours in there.
moment.utc(moment.duration(24, 'hours').as("milliseconds")).format("D HH:mm");
What am I doing wrong here?
You are setting 24 hours as a millisecond offset from 1970-01-01 (Unix epoch) by calling moment.utc(...). This means that your moment is holding the date 1970-01-02 00:00 and you are then printing the day part.
If the time frames are fixed and set by yourself you could always manually put in the amount of time in milliseconds to start with. e.g. 24 hours is 86400000 milliseconds.
You have to format the milliseconds in moment duration not in moment. I think the below line gives your expected value.
moment.duration(moment.duration(24, 'hours').as("milliseconds")).format("D HH:mm");
Answer : 1 00:00
I got it fixed with the following code and the plugin:
moment.duration(moment().diff(moment().subtract(1, 'days')));

MomentJS Converting EPOC millisecond to date

I receive two types of date, seconds and milliseconds for which i am trying to convert to a date.
Seconds: 1458820878062
Milliseconds: 1458823059491000
I can convert seconds via:
function convertToDate() {
var date = 1458820878062;
var m = moment.utc(date).utcOffset(moment().format('ZZ'));
return m.format('DD-MM-YY HH:mm:ss.SSS');
}
How can i convert milliseconds in to a date? And apply the necessary conversion to whatever date may be?
Something along the lines of:
if(date.length = 13) {
// Do seconds conversion
if(date.length > 13) {
// Do milliseconds conversion
You can try this:
moment("/Date(1458823059491000/1000)/")
ie., you need to divide the milliseconds by 1000 to get the time in seconds and then you can use it.
Moment supports both operations out of the box.
If you have seconds, use moment.unix:
moment.unix(1458855925).format()
"2016-03-24T16:45:25-05:00"
If you have milliseconds, just use the moment constructor directly:
moment(1458856019742).format()
"2016-03-24T16:46:59-05:00"
To get the unix seconds from a moment, use:
moment().unix()
1458856086
To get the unix milliseconds from a moment, use:
moment().valueOf()
1458856019742
How about just check if(data > 1e13) ?

Categories