0
0

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.

プリザンターのサーバースクリプトを使用し、日付を加算して別の項目に転記する

Last updated at Posted at 2023-01-17

はじめに

日付Cに日付を入力すると日付Dに180日後の日付を自動で入力したい。

ソースコード


//DateC項目が変更されたときの関数
$p.on('change', 'DateC', function () {
    //DateC項目に入力された値を取得・格納
    let dateC = new Date($p.getControl('DateC').val());
    //取得されたDateC項目の値に180日を加算
    dateC = dateC.setDate(dateC.getDate() + 180);
    //DateD項目に180日加算したDateC項目の値を設定
    $p.set($p.getControl('DateD'), $p.shortDateString(new Date(dateC)));
})

工夫

180日を加算するところは少し苦労しました。また、慣れないサーバースクリプトでの実装でしたので、その点も今までとのギャップを感じ、大変でした。ちなみに、今回は日付に180日加算したが下記ソースコードのように月でも加算することができる。


$p.on('change', 'DateC', function () {
    let dateC = new Date($p.getControl('DateC').val());
    dateC = dateC.setMonth(dateC.getMonth() + 7);
    $p.set($p.getControl('DateD'), $p.shortDateString(new Date(dateC)));
})

もともとのソースコードの「取得されたDateC項目の値に180日を加算」した行のコードを修正ました。具体的には下記参照


dateC = dateC.setDate(dateC.getDate() + 180);
//↓
dateC = dateC.setMonth(dateC.getMonth() + 7);

set~メソッドとget~メソッドを修正すれば正常に動作する。以下の記事を参照。
https://www.tohoho-web.com/js/date.htm

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?