补上之前的上周五的笔记
给出一个数字 单位是秒
62 就是 1 minute and 2 seconds
3662 就是 1 hour, 1 minute and 2 seconds
以此类推
难度:4kyu
思路
1、 1minute 对应 60s
2、 1h 对应 3600s
3、 1day 对应 86400s
4、 1year 对应 31536000(365天)
这些都是临界值、判断临界值,分别采取不同的函数
function formatDuration(seconds) {
// 60s 1min
// 3600s 1h
// 86400s 1day
if (seconds < 60 && seconds >= 1) {
return seconds > 1 ? seconds + ' seconds' : seconds + ' second'
} else if (seconds >= 60 && seconds < 3600) {
return getMinutes(seconds)
} else if (seconds >= 3600 && seconds < 86400) {
return getHours(seconds)
} else if (seconds >= 86400 && seconds < 31536000) {
return getDays(seconds)
}else {
return getYears(seconds)
}
}
function getMinutes(seconds) {
const yushu = seconds % 60
const min = Math.floor(seconds / 60)
const sec = yushu ? yushu > 1 ? yushu + ' seconds' : yushu + ' second' : ''
const result = (min > 1 ? min + ' minutes' : min + ' minute') + (sec ? ' and ' + sec : '')
return result
}
function getHours(seconds) {
// 秒
const yushu = seconds % 60;
// 分钟
const min = Math.floor(seconds / 60) % 60
// 时
const hour = Math.floor(seconds / 3600)
const sec = yushu ? yushu > 1 ? yushu + ' seconds' : yushu + ' second' : ''
const result = (hour > 1 ? hour + ' hours ' : hour + ' hour ') + (min > 1 ? min + ' minutes' : min + ' minute') + (sec ? ' and ' + sec : '')
return result;
}
function getDays(seconds){
// 秒
const yushu = seconds % 60;
// 分钟
const min = Math.floor(seconds / 60) % 60
// 时
const hour = Math.floor(seconds / 3600) % 24
// 天
const day = Math.floor(seconds / 86400)
const sec = yushu ? yushu > 1 ? yushu + ' seconds' : yushu + ' second' : ''
const result = (day > 1 ? day + ' days ' : day + ' day ') + (hour > 1 ? hour + ' hours ' : hour + ' hour ') + (min > 1 ? min + ' minutes' : min + ' minute') + (sec ? ' and ' + sec : '')
return result;
}
function getYears(seconds){
// 秒
const yushu = seconds % 60;
// 分钟
const min = Math.floor(seconds / 60) % 60
// 时
const hour = Math.floor(seconds / 3600) % 24
// 天
const day = Math.floor(seconds / 86400) % 365
// 年
const year = Math.floor(seconds / 31536000)
const sec = yushu ? yushu > 1 ? yushu + ' seconds' : yushu + ' second' : ''
const result = (year > 1 ? year + ' years ' : year + ' year ') + (day > 1 ? day + ' days ' : day + ' day ') + (hour > 1 ? hour + ' hours ' : hour + ' hour ') + (min > 1 ? min + ' minutes' : min + ' minute') + (sec ? ' and ' + sec : '')
return result;
}
这样写下去太复杂 重复代码极多 而且发现了一个相对来说比较简单的思路
直接取余数 如果取的余数和目前的值相等 说明小于取余的值 大于的话 还需要与临界值进行比较
min = Math.floor(seconds / 60) % 60
hour = Math.floor(seconds / 3600) % 24
day = Math.floor(seconds / 86400) % 365
year = Math.floor(seconds / 31536000)
代码精简、清晰易懂
function formatDuration(seconds) {
if(seconds == 0) return 'now'
var timeObj = {
// 年
year: Math.floor(seconds / 31536000),
// 天
day: Math.floor(seconds / 86400) % 365,
// 小时
hour: Math.floor(seconds / 3600) % 24,
// 分钟
minute: Math.floor(seconds / 60) % 60,
// 秒
second: seconds % 60
}
// Object.entries 把对象的键值以数组形式遍历出来 [["year",0],["day",1],["hour",0],["minute",0],["second",1]] 然后数组 filter 去掉 值为 0 的,接着拼接成需要的字符串
var timeArr = Object.entries(timeObj).filter(val => val[1] != 0).map(([ele, val]) => `${val} ${ele}${val > 1 ? 's' : ''}`)
// 当 timeArr 长度大于1说明 最后一个需要 and 连接符 否则不需要
const lianjie = timeArr.length > 1 ? ' and ' : ''
// slice 截取第一个到最后一个之前 以 ', ' 连接
return timeArr.slice(0, timeArr.length - 1).join(', ') + lianjie + timeArr[timeArr.length - 1]
}
formatDuration(1)
// , "1 second"
// formatDuration(62)
// , "1 minute and 2 seconds"
// formatDuration(120)
// , "2 minutes"
// formatDuration(3600)
// , "1 hour"
// formatDuration(3662)
// , "1 hour, 1 minute and 2 seconds"