NPM library: Converting Milliseconds to Timecode help needed? - node.js

Can someone help with a quick script that will convert Milliseconds to Timecode (HH:MM:SS:Frame) with this NPM library?
https://www.npmjs.com/package/ms-to-timecode
I just need to pass a miliseconds number (ie 7036.112) to it and output the converted timecode. I've installed npm and the ms-to-timecode library on my debian server. I know perl/bash, and never coded with these library modules.
Any help is greatly appreciated.
-Akak

You need to write a javascript code to use this npm module.
const msToTimecode = require('ms-to-timecode');
const ms = 6000;
const fps = 30
const result = msToTimecode(ms, fps)
console.log(result) // Print 00:00:06:00

If you are using the npm module ms-to-timecode then:
const msToTimecode = require('ms-to-timecode');
const timeCode = msToTimecode(7036, 112);
console.log(timeCode); // displays 00:00:07:04
If you don't want to use the npm module then:
function msToTimeCode(ms, fps) {
let frames = parseInt((ms/1000 * fps) % fps)
let seconds = parseInt((ms/1000)%60)
let minutes = parseInt((ms/(1000*60))%60)
let hours = parseInt((ms/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
frames = (frames < 10) ? "0" + frames : frames;
return hours + ":" + minutes + ":" + seconds + ":" + frames;
};
console.log(msToTimeCode(7036, 112)); // displays 00:00:07:04

Related

Is there any way to display time format with am/pm in mongodb

time :
{ $dateToString: { format: "%H:%M:%S:%L%z", date: "$generated_on", timezone: "+05:30" }}
with the help of this time is generated but I want time in AM/PM format}\
So basically, for time being (version 5), Mongo DB does not support the AM/PM format on the date object, you have two options:
option one: Store the correct format in the first place
option two(heavy operation): store a JS function inside the MongoDB for this custom format and call that in your aggregation pipeline, js function would be like this:
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
Add MongoDB custom JS function myCustomDate:
db.system.js.insertOne(
{
_id: "myCustomDate",
value: function (date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
}
);
and use this based on your requirements on mapReduce command

I am trying to get the time difference between the execution of 2 commands, one is !on and the other !off (Discord.JS))

I have the following code. It works to an extent, but when I do the !off command, it shows me a 0 instead of the actual time difference.
Index Code
global.time = getDateTime()
global.oldTime = getNewDateTime().getTime
global.finalTime = parseFloat(time) - parseFloat(oldTime)
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour + ":" + min;
}``
console.log(parseFloat(getDateTime))
function getNewDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour + ":" + min;
}
And here is the off command, same with on almost
exports.run = async (client, message, args, tools) => {
const embed = new Discord.MessageEmbed()
.setColor("BLUE")
.addField("Timer ended. Total Time Online", `**: ${finalTime}**`)
message.channel.send(embed);
}

Color console.log timestamps?

So, I'm using this code from 6 years ago and would like to color the timestamps however I don't know where to put the colors.
var log = console.log;
console.log = function () {
var first_parameter = arguments[0];
var other_parameters = Array.prototype.slice.call(arguments, 1);
function formatConsoleDate (date) {
var hour = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
return '[' +
((hour < 10) ? '0' + hour: hour) +
':' +
((minutes < 10) ? '0' + minutes: minutes) +
':' +
((seconds < 10) ? '0' + seconds: seconds) +
'] ';
}
log.apply(console, [formatConsoleDate(new Date()) + first_parameter].concat(other_parameters));
};
Any help would be appreciated.
Edit: I was able to color the timestamps without any modules or anything by putting a color at the END of every console.log, and it colors the next line. I would assume there is a better way to do this however.
console.log('\x1b[36m%s\x1b[0m', 'colored word',' \x1b[32m\x1b[0');That for example, would color the consoles timestamps green.
Check the article on the Node.js knowledgebase: How to get colors on the command line

Retrieve decimals only from a variable

I'm making a distance/time used calculator where i'm trying to separate whole hours and minutes.
Code:
if (hours >= 1)
{
minutes = Number((hours-int.(hours))*60);
timeTxt.text = String(int.(hours)+" hours & "+(minutes)+" minutes");
}
else
{
timeTxt.text = String((hours*60).toFixed(0)+" minutes");
}
the "else" one is working and prints minutes only. But I can't get the first if statement to work. Trying to print "x hours & x minutes".
Getting this message in output:
TypeError: Error #1123: Filter operator not supported on type class int.
at Rutekalkulator2_fla::MainTimeline/calculate()
Couldn't you do something like:
(pseudocode)
double tmp = math.floor(yournumber);
yournumber = yournumber - tmp;
And that'll make your number be just the decimal part.
Besides that, I'm not really sure what your question is.
Convert everything to minutes then try the following:
timeTxt.text = String( totMinutes / 60 + ":" + totMinutes % 60);
// output 06:36
timeTxt.text = String( totMinutes / 60 + " hours and " + totMinutes % 60 + " minutes");
// output 6 hours and 36 minutes
I'm not fluent in Actionscript but if the modulo works the same as it does in other languages this should work for you.

Wrong hour difference between 2 timestamps (hh:mm:ss)

Using moment.js, I want to get the time difference between 2 timestamps.
Doing the following,
var prevTime = moment('23:01:53', "HH:mm:SS");
var nextTime = moment('23:01:56', "HH:mm:SS");
var duration = moment(nextTime.diff(prevTime)).format("HH:mm:SS");
I get this result :
01:00:03
Why do I have a 1 hour difference? seconds and minutes seem to work well.
After doing that, I tried the following :
function time_diff(t1, t2) {
var parts = t1.split(':');
var d1 = new Date(0, 0, 0, parts[0], parts[1], parts[2]);
parts = t2.split(':');
var d2 = new Date(new Date(0, 0, 0, parts[0], parts[1], parts[2]) - d1);
return (d2.getHours() + ':' + d2.getMinutes() + ':' + d2.getSeconds());
}
var diff = time_diff('23:01:53','23:01:56');
output is : 1:0:3
The problem you are having here is that when putting the nextTime.diff() in a moment constructor, you are effectively feeding milliseconds to moment() and it tries to interpret it as a timestamp, which is why you don't get the expected result.
There is no "nice way" of getting the result you want apart from getting a time and manually reconstructing what you are looking for :
var dur = moment.duration(nextTime.diff(prevTime));
var formattedDuration = dur.get("hours") +":"+ dur.get("minutes") +":"+ dur.get("seconds");
And a more elegant version that will give you zero padding in the output :
var difference = nextTime.diff(prevTime);
var dur = moment.duration(difference);
var zeroPaddedDuration = Math.floor(dur.asHours()) + moment.utc(difference).format(":mm:ss");
Should make you happier!
EDIT : I have noticed you use HH:mm:SS for your format, but you should instead use HH:mm:ss. 'SS' Will give you fractional values for the seconds between 0 and 999, which is not what you want here.

Resources