LoginSignup
0
0

More than 1 year has passed since last update.

入力した一年後の日付を別のフィールドに自動入力する

Posted at

やりたいこと:date_fieldに入力した値から一年マイナス1日の日付を別のdate_fieldに自動表示させる。
Ex applied_date columnに2021-01-01と入力したなら
expired_date columnに2022-12-31と自動表示させる。

qiita.js
$(document).ready(() => {
  var applied = document.getElementById("applied_date");
  applied.addEventListener("change", (event) => { 
    var appliedDateInput = new Date(event.target.value);
    appliedDateInput.setFullYear(appliedDateInput.getFullYear()+1);
    appliedDateInput.setDate(appliedDateInput.getDate() - 1);
    var expiredDateInput = document.getElementById("expired_date");
    expiredDateInput.value = appliedDateInput.toISOString().substring(0,10);
  })
})
0
0
1

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