first commit

This commit is contained in:
kaj van schalkwijk
2023-02-15 19:32:46 +01:00
commit 0403dee3ed
3 changed files with 92 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
there a four different function all who do nealry the same but return something different.
there it ToSec, ToMin, ToHour, ToDay
you give it an input like 4d5h7m18s and it calculates it to Seconds, Minutes, Hours or Days
if your input does not does not look like dhms it is no problem as long as you use one of the letters it works
so ToSec('7h5m') retuns 25500
the programm is not case sensitive so 7H5m returns the same as 7h5M or 7h5m
+69
View File
@@ -0,0 +1,69 @@
function parseVal(input){
//check if string contains d,h,m,s and if not containts one of those add them to the string and set the value to 0 and place the value in the correct place
input = input.toLowerCase();
if(!input.includes("d") && !input.includes("h") && !input.includes("m") && !input.includes("s")){
return "you need to add a value"
}
if(!input.includes("d")){
input = "0d" + input;
}
if(!input.includes("h")){
data = input.split("d");
input = data[0] + "d0h" + data[1];
}
if(!input.includes("m")){
data = input.split("h");
input = data[0] + "h0m" + data[1];
}
if(!input.includes("s")){
data = input.split("m");
input = data[0] + "m0s" + data[1];
}
return input;
}
function splitDate(input) {
input = parseVal(input);
let days = input.split("d")[0];
let hours = input.split("d")[1].split("h")[0];
let minutes = input.split("d")[1].split("h")[1].split("m")[0];
let seconds = input.split("d")[1].split("h")[1].split("m")[1].split("s")[0];
return [days, hours, minutes, seconds];
}
function ToSec(input){
let data = splitDate(input);
let total = data[0] * 86400 + data[1] * 3600 + data[2] * 60 + data[3] * 1;
return total;
}
function ToMin(input){
let data = splitDate(input);
let total = data[0] * 1440 + data[1] * 60 + data[2] * 1 + data[3] / 60;
return total;
}
function ToHour(input){
let data = splitDate(input);
let total = data[0] * 24 + data[1] * 1 + data[2] / 60 + data[3] / 3600;
return total;
}
function ToDay(input){
let data = splitDate(input);
let total = data[0] * 1 + data[1] / 24 + data[2] / 1440 + data[3] / 86400;
return total;
}
module.exports = {
ToSec,
ToMin,
ToHour,
ToDay
}
+12
View File
@@ -0,0 +1,12 @@
{
"name": "time-convert",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC"
}