Created
May 26, 2020 02:12
-
-
Save AstroCB/cdfe0541752b640576026544d9cd2e8b to your computer and use it in GitHub Desktop.
Script to get wake-up times based on sleep cycles adapted from sleepyti.me site's source code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/node | |
// Get wake-up times based on sleep cycles if going to sleep now | |
// Code adapted from sleepyti.me source (surprisingly not minified) | |
const N_TIMES = 6; // Number of wake-up times to provide | |
let dates = [new Date(new Date().getTime() + 104 * 60000)]; | |
for (let i = 1; i < N_TIMES; i++) { | |
dates.push(new Date(dates[i - 1].getTime() + 90 * 60000)); | |
} | |
function retDate(dateObj) { | |
var formatted = ''; | |
var pm = false; | |
if (dateObj.getHours() > 12) { | |
formatted = dateObj.getHours() - 12; | |
pm = true; | |
} else if (dateObj.getHours() < 12 && dateObj.getHours() != 0) { | |
formatted = dateObj.getHours(); | |
} else if (dateObj.getHours() == 0) { | |
formatted = "12"; | |
} else if (dateObj.getHours() == 12) { | |
formatted = "12"; | |
pm = true; | |
} | |
if (dateObj.getMinutes() < 10) { | |
formatted = formatted + ":0" + dateObj.getMinutes(); | |
} else { | |
formatted = formatted + ":" + dateObj.getMinutes(); | |
} | |
if (pm == true) { | |
formatted = formatted + " PM"; | |
} else { | |
formatted = formatted + " AM"; | |
} | |
return formatted; | |
} | |
const times = dates.map(d => retDate(d)); | |
console.log(times); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment