Skip to content

Instantly share code, notes, and snippets.

@MaxVRAM
Created March 8, 2021 20:09
Show Gist options
  • Save MaxVRAM/875cf70b417a197e84f128860fe7e0cf to your computer and use it in GitHub Desktop.
Save MaxVRAM/875cf70b417a197e84f128860fe7e0cf to your computer and use it in GitHub Desktop.
Convert a timestamp to time since
var timeStamp = msg.payload;
var date = timeStamp.substring(0, timeStamp.lastIndexOf("T") - 1);
var time = timeStamp.substring(timeStamp.lastIndexOf("T") + 1, timeStamp.lastIndexOf("+") - 1);
var bootDateTime = Date.parse(msg.payload);
var currentDateTime = Date.parse(new Date());
var difference = (currentDateTime - bootDateTime) / 1000;
var outputValue;
if (difference < 10)
outputValue = "Just now"
else if (difference < 60)
outputValue = Math.floor(difference) + " seconds";
else
{
difference /= 60;
if (difference < 60)
if (Math.floor(difference) == 1)
outputValue = "1 minute";
else
outputValue = Math.floor(difference) + " minutes";
else
{
difference /= 60;
if (difference < 24)
if (Math.floor(difference) == 1)
outputValue = "1 hour";
else
outputValue = Math.floor(difference) + " hours";
else
{
difference /= 24;
if (Math.floor(difference) == 1)
outputValue = "1 day";
else
outputValue = Math.floor(difference) + " days";
}
}
}
return { payload : outputValue };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment