Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

js时间以10分钟向上取整

时间 2020-10-10 11:01 变为 2020-10-10 11:10
时间 2020-10-10 11:59 变为 2020-10-10 12:00

让时间分钟不满10分钟 以10分钟向上取整


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
demo('2020-10-10 11:01') // => 2020-10-10 11:10
demo('2020-10-10 11:20') // => 2020-10-10 11:20
demo('2020-10-10 11:59') // => 2020-10-10 12:00
demo('2020-12-31 23:50') // => 2020-12-31 23:50
demo('2020-12-31 23:59') // => 2021-01-01 00:00


function demo (timeStr) {
  timeStr = timeStr.replace(/-/g, '/')
  var oDate = new Date(timeStr)
  var stamp = oDate.getTime()
  var minute = oDate.getMinutes()
  var last = minute%10
  if(last) {
    stamp += (10-last) * 60 * 1000
  }
  oDate = new Date(stamp)

  var t = {
    year: pad_2_0(oDate.getFullYear()),
    month: pad_2_0(oDate.getMonth() + 1),
    day: pad_2_0(oDate.getDate()),
    hour: pad_2_0(oDate.getHours()),
    minute: pad_2_0(oDate.getMinutes()),
    second: pad_2_0(oDate.getSeconds())
  }

  var  res = t.year + '-' + t.month + '-' + t.day + ' ' + t.hour + ':' + t.minute;

  console.log(timeStr, '=>', res)
  return res;
}

function pad_2_0 (num) {
  return num >= 10 ? num : '0' + num
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...