Last active
March 16, 2016 05:38
-
-
Save BlessYAHU/5d8c9a3418ec26796eb3 to your computer and use it in GitHub Desktop.
system time using Bacon.js (http://jsbin.com/kigulex/4/edit?js,output)
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
((global,Bacon)=> { | |
const pollingInterval = 1000; | |
const getCurrentDateAndTime = ()=> new Bacon.Next(new Date()); | |
const getTime = (currentDate)=> ({ | |
hour: currentDate.getHours ( ), | |
minutes: currentMinutes = currentDate.getMinutes ( ), | |
seconds: currentDate.getSeconds ( ) | |
}); | |
const convertMinutesAndSeconds = (currentTime)=> ({ | |
hour : currentTime.hour, | |
minutes: (currentTime.minutes < 10 ? '0' : '') + currentTime.minutes, | |
seconds: (currentTime.seconds < 10 ? '0' : '') + currentTime.seconds | |
}); | |
const convertHourAndTimeOfDay = (currentTime)=> { | |
const timeOfDay = currentTime.hour < 12 ? 'AM' : 'PM'; | |
let convertedHour = currentTime.hour; | |
if(currentTime.hour > 12) convertedHour = currentTime.hour - 12; | |
if(currentTime.hour === 0 ) convertedHour = 12; | |
return { | |
hour: convertedHour, | |
minutes: currentTime.minutes, | |
seconds: currentTime.seconds, | |
timeOfDay: timeOfDay | |
}; | |
}; | |
const convertToDisplayTime = (currentTime)=> `${currentTime.hour}:${currentTime.minutes}:${currentTime.seconds} ${currentTime.timeOfDay}`; | |
const renderTimeToDom = (displayTime)=> global.document.querySelector('body').innerHTML = displayTime; | |
Bacon.fromPoll(pollingInterval,getCurrentDateAndTime) | |
.map(getTime) | |
.map(convertMinutesAndSeconds) | |
.map(convertHourAndTimeOfDay) | |
.map(convertToDisplayTime) | |
.onValue(renderTimeToDom); | |
})(window,Bacon) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment