0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【jQuery / JavaScript】日付の取得(0埋め)

Last updated at Posted at 2023-03-06

日付の取得

本日の日付の取得(0埋め)

$(function(){
    let date = new Date();
    let year = date.getFullYear();
    let monthRow = date.getMonth() + 1;
    let month = ('0'+monthRow).slice(-2);
    let day = ('0'+date.getDate()).slice(-2);
    let today = year+'-'+month+'-'+day;
    $('セレクタ').val(today);
});

日付AのX日後を日付Bに表示(0埋め)

$('セレクタA').on("change",function(){
    let date_A = $('セレクタA').val();
    let dt = new Date(date_A);
    // 各年月日の増減
    dt.setFullYear(dt.getFullYear() + X年)
    dt.setMonth(dt.getMonth() + Xか月);
    dt.setDate(dt.getDate() + X日);
    // 加減後の日付の取得
    let year = dt.getFullYear();
    let month = dt.getMonth() + 1;
    let date = dt.getDate();
    //0埋め
    month = ('0'+month).slice(-2);
    date = ('0'+date).slice(-2);
    let today = year+'-'+month+'-'+date;
    $('セレクタB').val(today);
});
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?