From 5e238b42f697dd6b87aeb2aee190f43caf994dcf Mon Sep 17 00:00:00 2001 From: jungmyunggi Date: Sat, 8 Feb 2025 10:34:08 +0900 Subject: [PATCH] [LeetCode - Easy] Convert Date to Binary --- jungmyunggi/Convert Date to Binary.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 jungmyunggi/Convert Date to Binary.js diff --git a/jungmyunggi/Convert Date to Binary.js b/jungmyunggi/Convert Date to Binary.js new file mode 100644 index 0000000..6e9ba6e --- /dev/null +++ b/jungmyunggi/Convert Date to Binary.js @@ -0,0 +1,20 @@ +/** + * @param {string} date + * @return {string} + */ + +function convertBinary(num){ + const stack = []; + while(num > 0){ + stack.push(num%2); + num = Math.floor(num/2); + } + return stack.reverse().join(""); +} +var convertDateToBinary = function(date) { + const [year, month, day] = date.split("-"); + const y = convertBinary(Number(year)) + const m = convertBinary(Number(month)) + const d = convertBinary(Number(day)); + return `${y}-${m}-${d}` +};