From 0403dee3ed67c679b1604094295ccab3095ed97c Mon Sep 17 00:00:00 2001 From: kaj van schalkwijk Date: Wed, 15 Feb 2023 19:32:46 +0100 Subject: [PATCH] first commit --- README.md | 11 +++++++++ index.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 12 +++++++++ 3 files changed, 92 insertions(+) create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d3d915 --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..a28b409 --- /dev/null +++ b/index.js @@ -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 +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..c26db11 --- /dev/null +++ b/package.json @@ -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" +}