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
Related
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 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);
}
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
I've got a String variable for my time :
var $myTime:String;
//Some calculation
trace(myTime);
// Result is : 14:25 (for example)
I'm looking for a way to convert this string ("14:25" in this example) to a simple AM / PM format.
So in this example it would be, for example, 2 PM
Any idea how I can simply do that ?
You can use DateFormatter class to set your wished pattern.
You can write your code as follow:
var df:DateFormatter = new DateFormatter();
df.formatString = "YYYY-MM-DD L:NN:SS";
df.format(myTime);
Where L is HOUR with PM/AM; NN are the minutes SS seconds.
You can see the complete guide, about DateFormatter pattern, here
var myTime:Date = new Date();
var timeString:String;
if (myTime.getHours() > 12)
{
timeString = String((myTime.getHours() - 12) + ":" + myTime.getMinutes() + " PM");
}
if (myTime.getHours() == 12)
{
timeString = String(myTime.getHours() + ":" + myTime.getMinutes() + " PM");
}
if (myTime.getHours() < 12)
{
timeString = String(myTime.getHours() + ":" + myTime.getMinutes() + " AM");
}
trace(timeString);
That should do the trick.
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.