LoginSignup
0
1

More than 3 years have passed since last update.

~JavaScript~残り何日?をやりたくて

Last updated at Posted at 2020-10-12

はじめに

Todoリストを作成中に残り何日?という期限をつけたくてそれの表示のために
new Date()を使って、今日の日付から残り何日かを表示させたかった。

コード

getLimit(limit) {// 2020/10/05
      const d = new Date()
      const nowDate = new Date(d.getFullYear(), d.getMonth(), d.getDate())//今の日付
      const year = Number(limit.slice(0, 4))
      const month = Number(limit.slice(5, 7)) - 1
      const date = Number(limit.slice(8, 10))
      const limitDate = new Date(year, month, date)//期限日
      const day = 1000 * 60 * 60 * 24//一日のミリ秒の値
      return (limitDate - nowDate) / day
    },

引数のlimitには2020/10/05というよな形で期限の日が入ります。
const month-1しているのは引数として取った時に既に+1された値が入ってくるため
limitDate - nowDateで日数分のミリ秒がでるのでそれをdayで割ると結果がでる。

0
1
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
1