输入秒数,计算出时分秒

 需求: 用户输入秒数,可以自动转换为时分秒
 分析:
 ①:用户输入总秒数 (注意默认值)
 ②:计算时分秒(封装函数) 里面包含数字补0
 ③:打印输出 
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /*  需求: 用户输入秒数,可以自动转换为时分秒
     分析:
     ①: 用户输入总秒数 (注意默认值)
     ②:计算时分秒(封装函数) 里面包含数字补0
     ③:打印输出 */

    const num = +prompt('请输入秒数:')
    function str(tiem) {
      let t = parseInt(tiem / 24 / 60 / 60 % 24)
      let h = parseInt(tiem / 60 / 60 % 24)
      let m = parseInt(tiem / 60 % 60)
      let s = parseInt(tiem % 60)
      t = t < 10 ? '0' + t : t
      h = h < 10 ? '0' + h : h
      m = m < 10 ? '0' + m : m
      s = s < 10 ? '0' + s : s
      return `输入的时间是:${t}${h}小时${m}分钟${s}`
    }
    const res = str(num)
    document.write(res)

    // 1.准备一个输入对话框
    /*  const str = +prompt('请输入秒数:')
     // 2.封装函数
     function content(tiem) {
       let h = parseInt(tiem / 60 / 60 % 24)
       let m = parseInt(tiem / 60 % 60)
       let s = parseInt(tiem % 60)
       h = h < 10 ? '0' + h : h
       m = m < 10 ? '0' + m : m
       s = s < 10 ? '0' + s : s
       return `输出的时间为:${h}小时${m}分钟${s}秒`
     }
     // 3.打印输出
     const res = content(str)
     document.write(res) */

  </script>
</body>

</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值