Created
September 26, 2021 14:33
-
-
Save avosa/db75b63b8301b8a461da6f27bc69210a to your computer and use it in GitHub Desktop.
A Nodejs implementation of time in strings
This file contains 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
var now = new Date(); | |
var seconds = now.getSeconds(); | |
var minutes = now.getMinutes(); | |
var hours = now.getHours(); | |
var day = now.getDay(); | |
var month = now.getMonth(); | |
var year = now.getFullYear(); | |
var days = [ | |
"Sunday", | |
"Monday", | |
"Tuesday", | |
"Wednesday", | |
"Thursday", | |
"Friday", | |
"Saturday", | |
]; | |
var months = [ | |
"January", | |
"February", | |
"March", | |
"April", | |
"May", | |
"June", | |
"July", | |
"August", | |
"September", | |
"October", | |
"November", | |
"December", | |
]; | |
var time = ""; | |
if (seconds === 0) { | |
time = "just now"; | |
} else if (seconds < 60) { | |
time = seconds + " seconds ago"; | |
} else if (minutes === 0) { | |
time = hours + " hours ago"; | |
} else if (minutes < 60) { | |
time = minutes + " minutes ago"; | |
} else if (hours === 0) { | |
time = "today at " + now.getHours() + ":" + now.getMinutes(); | |
} else if (hours < 24) { | |
time = hours + " hours ago"; | |
} else if (day === 0) { | |
time = "yesterday at " + now.getHours() + ":" + now.getMinutes(); | |
} else if (day === 1) { | |
time = "Monday at " + now.getHours() + ":" + now.getMinutes(); | |
} else if (day === 2) { | |
time = "Tuesday at " + now.getHours() + ":" + now.getMinutes(); | |
} else if (day === 3) { | |
time = "Wednesday at " + now.getHours() + ":" + now.getMinutes(); | |
} else if (day === 4) { | |
time = "Thursday at " + now.getHours() + ":" + now.getMinutes(); | |
} else if (day === 5) { | |
time = "Friday at " + now.getHours() + ":" + now.getMinutes(); | |
} else if (day === 6) { | |
time = "Saturday at " + now.getHours() + ":" + now.getMinutes(); | |
} else if (day === 7) { | |
time = "Sunday at " + now.getHours() + ":" + now.getMinutes(); | |
} else { | |
time = "on " + days[day] + " at " + now.getHours() + ":" + now.getMinutes(); | |
} | |
// var timeNode = document.createElement('div'); | |
// timeNode.innerHTML = time; | |
// document.body.appendChild(timeNode); | |
console.log(time); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment