例えば、今日の日付が 2025/4/14 だとしよう。
以下の JScriptコードは false が返る。
WScript.Echo(new Date() === new Date("2025/4/14"));
new Date() は現在時刻まで返すのに対し、new Date("2025/4/14") は時刻が 00:00:00 になるので時刻不一致で false になるのだ。
JScript は日付部分だけを比較する関数やメソッドを持たないため、この場合は自分で年、月、日をそれぞれ比較する必要がある。
このように、JScript での日付の扱いには結構苦労する。なので、以下の機能を持ったモジュールファイルを作っておくだけでもそこそこ重宝する。
- 日付フォーマット関数(Dateformat)
- 日付一致判定(DateMatch)
- 祝日判定(IsHoliday)
- 営業日取得(GetWorkday)
- 営業日数取得(GetNetWorkDay)
<DateModule.js>
WScript.Echo(DateMatch(new Date(), new Date("2025/4/14")));
WScript.Echo(IsHoliday(new Date("2025/4/29")));
WScript.Echo(Dateformat(new Date("1904/4/14 9:12:34"), "y年m月d日(aaa)h時n分s秒"));
WScript.Echo(GetNetWorkDay(new Date("2025/4/11"),new Date("2025/4/16")));
WScript.Echo(GetWorkday(new Date("2025/4/11"), 3));
// 日付(tgt_date)を fmt 形式の文字列で返す
// "yyyy"は西暦4桁、"yy"は西暦下2桁、"y"は和暦、"0y"は和暦0付き、gは元号(明治以前は西暦)
// "m"、"d"、"h"、"n"、"s" は 月、日、時、分、秒(二文字の場合は0つき2桁)
// "aaa"は曜日、"a"はその略字
function Dateformat(tgt_date, fmt){
var yyyy = tgt_date.getFullYear();
var m = tgt_date.getMonth()+1;
var d = tgt_date.getDate();
var h = tgt_date.getHours();
var n = tgt_date.getMinutes();
var s = tgt_date.getSeconds();
var dayAry = ["日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"];
aaa = dayAry[tgt_date.getDay()];
var yy = String(yyyy).slice(-2);
var mm = ("0" + m).slice(-2);
var dd = ("0" + d).slice(-2);
var hh = ("0" + h).slice(-2);
var nn = ("0" + n).slice(-2);
var ss = ("0" + s).slice(-2);
var a = aaa.slice(0,1);
fmt = fmt.replace("yyyy", yyyy);
fmt = fmt.replace("yy", yy);
if (tgt_date < new Date("1868/9/4")) {g = ""; y = yyyy;}
if (tgt_date >= new Date("1868/9/4")) {g = "明治"; y = yyyy - 1867;}
if (tgt_date >= new Date("1912/7/31")) {g = "大正"; y = yyyy - 1911;}
if (tgt_date >= new Date("1926/12/27")) {g = "昭和"; y = yyyy - 1925;}
if (tgt_date >= new Date("1989/1/8")) {g = "平成"; y = yyyy - 1988;}
if (tgt_date >= new Date("2019/5/1")) {g = "令和"; y = yyyy - 2018;}
fmt = fmt.replace("0y", g + ("0" + y).slice(-2));
if (y === 1) {y = "元";}
fmt = fmt.replace("y", g + y);
fmt = fmt.replace("mm", mm);
fmt = fmt.replace("dd", dd);
fmt = fmt.replace("hh", hh);
fmt = fmt.replace("nn", nn);
fmt = fmt.replace("ss", ss);
fmt = fmt.replace("aaa", aaa);
fmt = fmt.replace("m", m);
fmt = fmt.replace("d", d);
fmt = fmt.replace("h", h);
fmt = fmt.replace("n", n);
fmt = fmt.replace("s", s);
fmt = fmt.replace("a", a);
return fmt;
}
// 開始日(start_date)から終了日(end_date)までの営業日数を返す
function GetNetWorkDay(start_date, end_date){
var n = 0;
while (start_date < end_date) {
if (!IsHoliday(start_date)) {n++;}
start_date.setDate(start_date.getDate() + 1);
}
return n;
}
// 起算日(start_date)から d 営業日後の日付を返す
function GetWorkday(start_date, d){
if (d === 0) {return start_date;}
var n = 0;
if (d > 0) {
// d が正なら d 営業日後の日付を返す
while (n < d) {
start_date.setDate(start_date.getDate() + 1);
if (!IsHoliday(start_date)) {n++;}
}
} else {
// d が負なら d 営業日前の日付を返す
while (n < d) {
start_date.setDate(start_date.getDate() - 1);
if (!IsHoliday(start_date)) {n++;}
}
}
return start_date;
}
// date1 と date2 の年月日が同じなら true を返す
function DateMatch(date1, date2){
if (date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()) {
return true;
}
return false;
}
// 与えられた日付が土日祝日なら true を返す
function IsHoliday(tgt_date){
if (tgt_date.getDay() === 0) {return true;}
if (tgt_date.getDay() === 6) {return true;}
var holidays = GetHolidays();
for (var i=0; i < holidays.length; i++) {
if (DateMatch(holidays[i], tgt_date)) {return true;}
}
return false;
}
// 祝日の配列を返す
function GetHolidays(){
var holidays = [new Date("2025/1/1")];
holidays.push(new Date("2025/1/2"));
holidays.push(new Date("2025/1/3"));
holidays.push(new Date("2025/1/13"));
holidays.push(new Date("2025/2/11"));
holidays.push(new Date("2025/2/23"));
holidays.push(new Date("2025/2/24"));
holidays.push(new Date("2025/3/20"));
holidays.push(new Date("2025/4/29"));
holidays.push(new Date("2025/5/3"));
holidays.push(new Date("2025/5/4"));
holidays.push(new Date("2025/5/5"));
holidays.push(new Date("2025/5/6"));
holidays.push(new Date("2025/7/21"));
holidays.push(new Date("2025/8/11"));
holidays.push(new Date("2025/9/15"));
holidays.push(new Date("2025/9/23"));
holidays.push(new Date("2025/10/13"));
holidays.push(new Date("2025/11/3"));
holidays.push(new Date("2025/11/23"));
holidays.push(new Date("2025/11/24"));
holidays.push(new Date("2025/12/29"));
holidays.push(new Date("2025/12/30"));
holidays.push(new Date("2025/12/31"));
holidays.push(new Date("2026/1/1"));
holidays.push(new Date("2026/1/2"));
holidays.push(new Date("2026/1/3"));
holidays.push(new Date("2026/1/12"));
holidays.push(new Date("2026/2/11"));
holidays.push(new Date("2026/2/23"));
holidays.push(new Date("2026/3/20"));
holidays.push(new Date("2026/4/29"));
holidays.push(new Date("2026/5/3"));
holidays.push(new Date("2026/5/4"));
holidays.push(new Date("2026/5/5"));
holidays.push(new Date("2026/5/6"));
holidays.push(new Date("2026/7/20"));
holidays.push(new Date("2026/8/11"));
holidays.push(new Date("2026/9/21"));
holidays.push(new Date("2026/9/22"));
holidays.push(new Date("2026/9/23"));
holidays.push(new Date("2026/10/12"));
holidays.push(new Date("2026/11/3"));
holidays.push(new Date("2026/11/23"));
holidays.push(new Date("2026/12/29"));
holidays.push(new Date("2026/12/30"));
holidays.push(new Date("2026/12/31"));
holidays.push(new Date("2027/1/1"));
holidays.push(new Date("2027/1/2"));
holidays.push(new Date("2027/1/3"));
return holidays;
}