アプリ版:「スタンプのみでお礼する」機能のリリースについて

いつも大変お世話になっております。
「コピって楽々JavaScript! 「出荷日田中ちゃん」ver 1.01」
http://www.ebisuweb.jp/rakuraku/index.cgi
というツールを使いテキストで受注からの出荷日を設定したいのですがお教えください。

【求めたい結果】

11/16(水)11:00までにに注文いただくと 11月16日(水) 中の出荷が可能です。

11:00以降は

11/17(木)11:00までにに注文いただくと 11月17日(水) 中の出荷が可能です。

併せて、土日祝を挟む場合

11/18(金)11:00までにに注文いただくと 11月18日(金) 中の出荷が可能です。

11:00以降は

11/21(月)11:00までにに注文いただくと 11月21日(月) 中の出荷が可能です。


また、土日祝の休日設定をJAVAスクリプト内でされておりますが、来年度の土日祝の設定や
年末年始・お盆などカレンダー以外の休日もありますので、任意で休日を設定する方法をお教えください。

2016年年末~2017年カレンダー
※土日も休みです。土日に祝日が被る場合は土日扱いでいいです。
12/29→1/4(弊社指定の休業日)
1/9
3/20
5/3→5/5
7/17
8/11→8/16
9/18
10/9
11/3
11/23
12/29→12/31

【現在のJAVAソース】

<SCRIPT language="JavaScript">
thisDay = new Date();
timeStamp = thisDay.getTime();
myMonth = thisDay.getMonth() + 1;
myDate = thisDay.getDate();
myHours = thisDay.getHours();
myDay = thisDay.getDay();
myWeekTbl = new Array( "日","月","火","水","木","金","土" );

function isHoliday (year, month, date, nday) {
nofw = Math.floor((date - 1) / 7) + 1;
shunbun = Math.floor(20.8431+0.242194*(year-1980)-Math.floor((year-1980)/4));
syubun = Math.floor(23.2488+0.242194*(year-1980)-Math.floor((year-1980)/4));
if (month == 1 && date == 1) { return 1; } // 元旦
if (month == 1 && nday == 1 && nofw == 2) { return 1; } // 成人の日
if (month == 2 && date == 11) { return 1; } // 建国記念の日
if (month == 3 && date == shunbun) { return 1; } // 春分の日
if (month == 4 && date == 29) { return 1; } // みどりの日
if (month == 5 && date >= 3 && date <= 5) { return 1; } // 憲法記念日~こどもの日
if (month == 7 && nday == 1 && nofw == 3) { return 1; } // 海の日
if (month == 9 && nday == 1 && nofw == 3) { return 1; } // 敬老の日
if (month == 9 && date == syubun) { return 1; } // 秋分の日
if (month == 9 && nday == 2 && nofw == 3
&& date+1 == syubun) { return 1; } // 9月第3火曜の翌日が秋分の日→国民の休日
if (month == 10 && nday == 1 && nofw == 2) { return 1; } // 体育の日
if (month == 11 && date == 3) { return 1; } // 文化の日
if (month == 11 && date == 23) { return 1; } // 勤労感謝の日
if (month == 12 && date == 23) { return 1; } // 天皇誕生日
if (nday == 0) { return 2; } // 日曜
if (nday == 6) { return 2; } // 土曜
return 0;
}

function dispDateW () {
return dispDate(1);
}

function dispDate1W (h) {
return dispDate1(h, 1);
}

function dispDate2W (n, h) {
return dispDate2(n, h, 1);
}

function dispDate (w) {
return dateFormat(myMonth,myDate,myDay,w);
}

function dispDate1 (h, w) {
return dispDate2(0, h, w);
}

function dispDate2 (n, h, w) {
var i = 0;
while (i <= n) {
thisDay.setTime(timeStamp + (1000*60*60*24 * i));
myYear2 = thisDay.getFullYear();
myMonth2 = thisDay.getMonth() + 1;
myDate2 = thisDay.getDate();
myDay2 = thisDay.getDay();
if (isHoliday(myYear2,myMonth2,myDate2,myDay2) == 0 && i == 0 && h <= myHours) { n++; } // 翌日扱い
if (isHoliday(myYear2,myMonth2,myDate2,myDay2) >= 1){ n++; } // 休日
if (isHoliday(myYear2,myMonth2,myDate2,myDay2) == 1 && myDay2 == 0){ n++; } // 振替休日
i++;
}
return dateFormat(myMonth2,myDate2,myDay2,w);
}

function dateFormat (month, date, week, w) {
if (w == 1) { return month+"月"+date+"日("+myWeekTbl[week]+")"; }
else { return month+"月"+date+"日"; }
}
</SCRIPT>

【HTMLソース】

<span style="color:blue;font-weight:bold;">
<script language="JavaScript"><!--
document.write(dispDate1W(11));
// --></script> AM11:00</span><b>までに注文いただくと</b>

<span style="color:red;font-weight:bold;">
<script language="JavaScript"><!--
document.write(dispDate1W(11));
// --></script></span> <b>中の出荷が可能です。</b>

長くなりましたが、どうぞお教え下さいませ。
宜しくお願い致します。

質問者からの補足コメント

  • ご返答ありがとうございます。
    計算式では出来ませんよね。
    出来ましたら、上記に記載した休日設定(2017年度)のjavaでの設定方法をお教え頂けませんか?

    出荷予定に関しましては、もちろんお客様の目安としてHP上に記載します。

    ご指摘ありがとうございます。
    おわかりであれば、休日設定方法をお教え下さいませ。
    宜しくお願い致します。

    No.1の回答に寄せられた補足コメントです。 補足日時:2016/11/16 20:21

A 回答 (2件)

こんにちは



商取引での時間の処理はANo1様のご指摘の通りですし、通常、スクリプトで利用している時刻は、閲覧者のマシン設定に依存していますので正確である保証もありません。

というだけでは回答にならないので、簡単な例を作成してみました。

祝日に関してはいろいろなライブラリが作成されていますので、それを利用する方が簡単かと思います。
例えば、googleカレンダーから休日データを取得するなどがありますが、どうやら正確性に欠けるという情報もあるので、以下のものを利用してみました。
https://www.npmjs.com/package/japanese-holidays

同じ作者でもっと便利そうなものもあり、こちらであれば個別に指定休日を追加できる機能なども備えているようです。
https://hrdaya.github.io/UltraDate.js/
https://cybozudev.zendesk.com/hc/ja/articles/207 …
(CDN)
https://cybozudev.zendesk.com/hc/ja/articles/202 …

内容をきちんと把握するのが面倒だったので(←無精者)、前者を利用することにして、追加休日の指定と判定に関しては個別に作成するという考えです。
追加の休日の指定方法はいろいろ考えられると思いますが、ひとまず、ご質問文にあるような『月/日』での指定としています。(同月内の連続日は「-」で連続指定可能)
ご質問文ではかなりの日が祝日とかぶっているようですが、重複指定の必要は無いように思います。


以下、ご参考までに。
(この掲示板では、5行目のsrc属性のURLがリンクになって省略表示されてしまいますので、ご注意ください)

<!DOCTYPE HTML>
<html lang="ja">
<head><title>Sample</title>

<script src="https://cdn.rawgit.com/osamutake/japanese-holida …

<script>
window.onload = function(){
//追加指定休日の設定
var special_holidays = "1/1-4, 8/11-16, 11/16, 11/18, 12/29-31";

//表示用文字列の設定
var html = "※m/※d(※w) 11:00までに注文いただくと ※m月※d日(※w)中の出荷が可能です。";


function nextDay(d){ d.setDate(d.getDate() + 1); }
var isHoliday = holidayChecker();
//表示処理
var today = new Date();
if(today.getHours() > 10) nextDay(today);
while( isHoliday(today) ){ nextDay(today); }

var m = today.getMonth() + 1, d = today.getDate();
var w = "日月火水木金土".split("")[today.getDay()];

html = html.replace(/※m/g, m).replace(/※d/g, d).replace(/※w/g, w);
document.querySelector("#order_shipping").innerHTML = html;


//休日判定用関数作成
function holidayChecker(){
/* 指定休日を配列データ化 */
var SH = [], sh = special_holidays.split(",");
var i = 0, n, h, m, d, start, end;
while(h = sh[i++]){
h = h.split("/"), m = h[0] | 0, d = h[1].split("-")
start = d[0] | 0, end = d[1]?(d[1] | 0):start;
for(n = start; n<=end; n++) SH.push({ month: m, date: n });
}

/* 休日判定関数 */
return function(dd){
var w = dd.getDay(), m = dd.getMonth() + 1, d = dd.getDate();
// 土・日曜日判定
if(w==0 || w==6) return true;
// 追加指定休日判定
for(var i = 0; i<SH.length; i++){
if(SH[i].month == m && SH[i].date == d) return true
}
// 国民の祝日判定
if( JapaneseHolidays.isHoliday(dd) ) return true;
return false;
}
}
}
</script>
</head>
<body>

<div>
<span id="order_shipping"></span>
</div>

</body>
</html>
    • good
    • 0

まず祝日は計算式では求められません


年によってころころ計算根拠がかわるからです。
手動で祝日カレンダーをつくっておいて、それに合致するかどうかで判断するしかありません。
くわえて振替休日や定休日、臨時休業などを考慮すればなおさらです

またjavascriptで出荷予定日を計算するのもお勧めしません
配送情報などはサーバー側で管理するものなので、手順として
ユーザーが注文情報を入れた際にサーバーにその情報を送り出荷日を受取り
再度ユーザー側に表示する必要があります。
もしjavascriptでどうしても計算をしたいという場合でも、
あくまでも目安である旨記載しないと実作業とのずれが信用問題につながりますし
場合によっては注文キャンセルされても仕方ありません
この回答への補足あり
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!