명령형
setInterval(logClockTime, 1000);
function logClockTime(){
var time = getClockTime();
console.clear();
console.log(time);
}
function getClockTime(){
// 현재 시각을 얻는다.
var date = new Date();
var time = "";
//시각을 직렬화 한다.
var time ={
hours : date.getHours(),
minutes : date.getMinutes(),
seconds: date.getSeconds(),
ampm: "AM"
};
//상용시로 변환한다.
if(time.hours ==12){
time.ampm = "PM";
}else if ( time.hours > 12){
time.ampm = "PM";
time.hour -= 12;
}
//시간을 2글자로 만들기 위해 앞에 0을 붙인다.
if(time.hours <10){
time.hours = "0" + time.hours;
}
//분을 2글자로 만들기 위해 앞에 0을 붙인다.
if(time.minutes < 10){
time.hours = "0"+ time.minutes;
}
//초를 2글자로 만들기 위해 앞에0을 붙인다.
if(time.seconds <10){
time.seconds = "0" + time.seconds;
}
//" hh:mm:ss tt " 형식의 문자열을 만든다.
return time.hours + ":"
+time.minutes+":"
+time.seconds + " "
+time.ampm;
}
선언형
const abstractClockTime = date => ({
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds()
})
const civilianHours = clockTime => ({
...clockTime,
hours: (clockTime.hours > 12)? clockTime.hours - 12: clockTime.hours
})
const appendAMPM = clockTime => ({
...clockTime,
ampm: (clockTime.hours >= 12 ) ? "PM":"AM"
})
const display = target => time => target(time)
const formatClock = format => time => format.replace("hh", time.hours)
.replace("mm", time.minutes)
.replace("ss", time.seconds)
.replace("tt", time.ampm)
const prependZero = key => clockTime =>
({
...clockTime,
[key]:(clockTime[key] < 10)? "0" + clockTime[key]:clockTime[key]
})
const convertToCivilianTime = clockTime =>
compose(appendAMPM, civilianHours)(clockTime)
const doubleDigits = civilianTime =>
compose(
prependZero("hours"),
prependZero("minutes"),
prependZero("seconds")
)(civilianTime)
const startTicking =() =>
setInterval(
compose(
clear,
getCurrentTime,
abstractClockTime,
convertToCivilianTime,
doubleDigits,
formatClock("hh:mm:ss tt"),
display(log)
),
oneSecond()
)
startTicking()
댓글 없음:
댓글 쓰기