# 選択内容によって日付を加算したい

**URL:** <https://community.cybozu.dev/t/topic/5844>\
**Category:** kintone 開発相談\
**Created:** [2022 年 3 月 29 日午前 8:27 UTC](https://community.cybozu.dev/t/topic/5844 "2022-03-29T08:27:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account1583](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@Legacy\_Account1583](https://community.cybozu.dev/u/Legacy_Account1583)\
**Post date:** [2022 年 3 月 29 日午前 8:27 UTC](https://community.cybozu.dev/t/topic/5844/1 "2022-03-29T08:27:55Z")

</div>

初心者です・・・！

過去の投稿やいろいろな参考サイトを見て書いてみたのですが、なかなかうまくいきません。

お知恵をお貸し頂きたいです＞＜

\*\*\*\*\*

①掲載開始日に任意の日付を入力

②期間を選択「6ヶ月」「12ヶ月」「24ヶ月」

③支払方法を選択「通常」「一括」

6ヶ月×通常なら 掲載開始日の6か月後の末日を掲載終了日に自動反映

6ヶ月×一括なら 掲載開始日の7か月後（1ヶ月延長）の末日を掲載終了日に自動反映

12ヶ月×通常なら 掲載開始日の12か月後の末日を掲載終了日に自動反映

12ヶ月×一括なら 掲載開始日の13か月後（1ヶ月延長）の末日を掲載終了日に自動反映

24ヶ月×通常なら 掲載開始日の24か月後の末日を掲載終了日に自動反映

24ヶ月×一括なら 掲載開始日の25か月後（1ヶ月延長）の末日を掲載終了日に自動反映

したいです。

\*\*\*\*\*\*\*

(function() {  
&nbsp; &nbsp;“use strict”;  
&nbsp; &nbsp;kintone.events.on([“app.record.create.change.期間”,“app.record.edit.change.期間”,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; “app.record.create.change.支払方法”,“app.record.edit.change.支払方法”,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; “app.record.create.change.掲載開始日”,“app.record.edit.change.掲載開始日”], function(event) {  
&nbsp; &nbsp; &nbsp; &nbsp;var record = event.record;  
&nbsp; &nbsp; &nbsp; &nbsp;var date = record.掲載開始日.value;  
&nbsp; &nbsp; &nbsp; &nbsp;var Drop = record.期間.value;  
&nbsp; &nbsp; &nbsp; &nbsp;var pay = record.支払方法.value;

&nbsp; &nbsp; &nbsp; &nbsp;if (Drop === “6ヶ月” && pay === “通常” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var month6 = date.plus({months:6}).endOf(‘month’).toFormat(“YYYY-MM-DD”);;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;record.掲載終了日.value = month6.toFormat(‘YYYY-MM-DD’);  
&nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp;if (Drop === “6ヶ月” && pay === “一括” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; var month7 = date.plus({months:7}).endOf(‘month’).toFormat(“YYYY-MM-DD”);;  
&nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = month7.toFormat(‘YYYY-MM-DD’);  
&nbsp; &nbsp; }  
&nbsp; &nbsp; if (Drop === “12ヶ月” && pay === “通常” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; var month12 = date.plus({months:12}).endOf(‘month’).toFormat(“YYYY-MM-DD”);;  
&nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = month12.toFormat(‘YYYY-MM-DD’);  
&nbsp; &nbsp; }  
&nbsp; &nbsp; if (Drop === “12ヶ月” && pay === “一括” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; var month13 = date.plus({months:13}).endOf(‘month’).toFormat(“YYYY-MM-DD”);;  
&nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = month13.toFormat(‘YYYY-MM-DD’);  
&nbsp; &nbsp; }  
&nbsp; &nbsp; if (Drop === “24ヶ月” && pay === “通常” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; var month24 = date.plus({months:24}).endOf(‘month’).toFormat(“YYYY-MM-DD”);;  
&nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = month24.toFormat(‘YYYY-MM-DD’);  
&nbsp; &nbsp; }  
&nbsp; &nbsp; if (Drop === “24ヶ月” && pay === “一括” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; var month25 = date.plus({months:25}).endOf(‘month’).toFormat(“YYYY-MM-DD”);;  
&nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = month25.toFormat(‘YYYY-MM-DD’);  
&nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp;return event;  
&nbsp; &nbsp;});  
})();

---

<div class="post-metadata">

**Author:** ![Legacy\_Account980](https://avatars.discourse-cdn.com/v4/letter/l/a183cd/32.png) [@Legacy\_Account980](https://community.cybozu.dev/u/Legacy_Account980)\
**Post date:** [2022 年 4 月 2 日午後 2:38 UTC](https://community.cybozu.dev/t/topic/5844/2 "2022-04-02T14:38:07Z")

</div>

パッと見で試していないので間違っているかもしれませんが（＞＜）

チェックボックスだと複数選択できることもあり、取得した値が配列の形になっちゃうので、  
payには[“一括”]とか[“通常”]とか[“通常”, “一括”]とか・・・のような配列が入っていると思います。

なので、payが配列であることを考慮した条件文にするとか、

もし通常と一括が両方とも選ばれることが無いのであれば、ラジオボタンに変更してみてはいかがでしょうか👀！？

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1583](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@Legacy\_Account1583](https://community.cybozu.dev/u/Legacy_Account1583)\
**Post date:** [2022 年 4 月 4 日午前 3:13 UTC](https://community.cybozu.dev/t/topic/5844/3 "2022-04-04T03:13:32Z")

</div>

376177001012

コメントありがとうございます＞＜

ラジオボタンに直してみたのですがうまくいかず(´;ω;｀)

toFormat(‘YYYY-MM-DD’)形式がダメなのかなと思い、toFormat(‘yyyy-MM-dd’)に変更もしてみたのですがうまくいかず・・・

↓このようなエラーがでたのですが、どう直してよいかもわからず・・・初心者過ぎてすみません・・・

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1559](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Legacy\_Account1559](https://community.cybozu.dev/u/Legacy_Account1559)\
**Post date:** [2022 年 4 月 6 日午前 6:32 UTC](https://community.cybozu.dev/t/topic/5844/4 "2022-04-06T06:32:19Z")

</div>

kintoneカスタマイズの勉強を始めてまだ間もない者です。自身の勉強のために本件調べさせていただきました。

画面から取得した「掲載開始日」をluxonを使って日付処理できるようにするとよいのではと思います。  
（「掲載開始日」フィールドの値にそのまま月数を追加するとエラーになってしまうようです）

luxonを使用するには事前に、アプリの設定の「JavaScript / CSSでカスタマイズ」画面にてカスタマイズファイルとして指定する必要があります。  
luxonの追加方法は下記ページの「kintone カスタマイズでの導入方法」にあります。  
[Luxon を使って kintone の日付や日時フィールドのフォーマットをカスタマイズする](https://community.cybozu.dev/hc/ja/articles/900000985463-Luxon-%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6-kintone-%E3%81%AE%E6%97%A5%E4%BB%98%E3%82%84%E6%97%A5%E6%99%82%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89%E3%81%AE%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%83%E3%83%88%E3%82%92%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%9E%E3%82%A4%E3%82%BA%E3%81%99%E3%82%8B)

luxonを使用したサンプルコードを記載します。私の開発環境では下記コードで動きました。  
（支払方法はラジオボタンにしています）

(function() {  
&nbsp; &nbsp;“use strict”;  
&nbsp; &nbsp;kintone.events.on([“app.record.create.change.期間”,“app.record.edit.change.期間”,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; “app.record.create.change.支払方法”,“app.record.edit.change.支払方法”,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; “app.record.create.change.掲載開始日”,“app.record.edit.change.掲載開始日”], function(event) {  
&nbsp; &nbsp; &nbsp; &nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp;const record = event.record;  
&nbsp; &nbsp; &nbsp; &nbsp;const date = luxon.DateTime.fromISO(record.掲載開始日.value).startOf(‘day’); &nbsp;//luxonを使って日付処理できるようにする  
&nbsp; &nbsp; &nbsp; &nbsp;const Drop = record.期間.value;  
&nbsp; &nbsp; &nbsp; &nbsp;const pay = record.支払方法.value;

&nbsp; &nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp;if (Drop === “6ヶ月” && pay === “通常” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; // month6などの変数は使わずに、直接レコードに入れるように変更しています（以下同様）  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;record.掲載終了日.value = date.plus({months:6}).endOf(‘month’).toFormat(‘yyyy-MM-dd’);  
&nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp;if (Drop === “6ヶ月” && pay === “一括” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = date.plus({months:7}).endOf(‘month’).toFormat(‘yyyy-MM-dd’);  
&nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp;if (Drop === “12ヶ月” && pay === “通常” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = date.plus({months:12}).endOf(‘month’).toFormat(‘yyyy-MM-dd’);  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; if (Drop === “12ヶ月” && pay === “一括” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = date.plus({months:13}).endOf(‘month’).toFormat(‘yyyy-MM-dd’);  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; if (Drop === “24ヶ月” && pay === “通常” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = date.plus({months:24}).endOf(‘month’).toFormat(‘yyyy-MM-dd’);  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; if (Drop === “24ヶ月” && pay === “一括” && date !== undefined) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; record.掲載終了日.value = date.plus({months:25}).endOf(‘month’).toFormat(‘yyyy-MM-dd’);  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp;return event;  
&nbsp; &nbsp;});  
})();

よろしければ参考までに。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1583](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@Legacy\_Account1583](https://community.cybozu.dev/u/Legacy_Account1583)\
**Post date:** [2022 年 4 月 6 日午前 7:57 UTC](https://community.cybozu.dev/t/topic/5844/5 "2022-04-06T07:57:48Z")

</div>

4605066565017

&nbsp;

ありがとうございます！×10000

こちらでも稼働確認取れました！

とてもシンプルなコードになるんですね。

&nbsp;&nbsp; &nbsp; &nbsp;const date = luxon.DateTime.fromISO(record.掲載開始日.value).startOf(‘day’); &nbsp;//luxonを使って日付処理できるようにする

ここが肝だったのですね。勉強になります！！！
