Skip to content

Instantly share code, notes, and snippets.

@BlessYAHU
Last active March 16, 2016 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlessYAHU/5d8c9a3418ec26796eb3 to your computer and use it in GitHub Desktop.
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)
((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