LoginSignup
0
0

More than 3 years have passed since last update.

○○まで後何日?アプリを作成してみた

Posted at

jsのDateオブジェクトは使いにくいのでmoment.jsというライブラリを使って作成する。

日付はドラえもんが生まれる日に設定した。

var timer //タイマー設定

const will = moment("2112-09-03 12:00")//日付選択

const doraemon = document.createElement("p")//要素作成
const body = document.querySelector('body');//親ノード取得
body.appendChild(doraemon)//子ノード追加




function count() {
    let date = moment();
    let secondLeft =(will.diff(date,"second")%60)
    let minuteLeft=(will.diff(date, "minute") % 60)
    let hourLeft=(will.diff(date, "hour") % 24)
    let dayLeft=(will.diff(date, "days") % 24)
    let monthLeft=(will.diff(date, "month") % 12)
    let yearLeft=(will.diff(date, "year"))
    doraemon.textContent = `ドラえもんが生まれるまで後${yearLeft}${monthLeft}${dayLeft}${hourLeft}${minuteLeft}${secondLeft}秒`
}

count()
timer = setInterval("count()", 1000)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0