dateValue関数

本ページには広告が含まれています。

日付文字列をシリアル値に変換します。

構文
  1. Double = dateValue( str )
引数
str 必須
日付文字列
戻り値
シリアル値

プログラム

UWSC
//////////////////////////////////////////////////
// 【引数】
//   str : 日付文字列 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION dateValue(str)
	CONST cutOffYear = 2049
	DIM Months[] = "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
	
	HASHTBL Pattern
	Pattern["year"] = "(19[0-9]{2}|[2-9][0-9]{3})"
	Pattern["year2"] = "([0-9]{2})"
	Pattern["era"] = "(明治?|大正?|昭和?|平成?|令和?|[MTSHR])(元|\d+)"
	Pattern["month"] = "(1[0-2]|0?[1-9])"
	Pattern["month2"] = "(0[1-9]|1[0-2])"
	Pattern["month_en"] = "(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Ap(?:ril)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)"
	Pattern["day"] = "([1-2][0-9]|3[0-1]|0?[1-9])"
	Pattern["day2"] = "(0[1-9]|[1-2][0-9]|3[0-1])"
	Pattern["day_en"] = "(1(?:st)|2?2(?:nd)|2?3(?:rd)|[1-3]?[04-9](?:th)|1[1-3](?:th)|[1-3]1(?:st))"
	Pattern["separator"] = "([./\-])"
	
	HASHTBL era
	era["明治"] = 1867;	era["明"] = 1867;	era["M"] = 1867;
	era["大正"] = 1911;	era["大"] = 1911;	era["T"] = 1911;
	era["昭和"] = 1925;	era["昭"] = 1925;	era["S"] = 1925;
	era["平成"] = 1988;	era["平"] = 1988;	era["H"] = 1988;
	era["令和"] = 2018;	era["令"] = 2018;	era["R"] = 2018;

	SELECT TRUE
		CASE reTest(str, "\b" + Pattern["year"] + Pattern["separator"] + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
			Matches = reExecute(str, "\b(?:" + Pattern["year"] + Pattern["separator"] + ")?" + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + text(.SubMatches(2), "00") + "/" + text(IIF(.SubMatches(3)="", "01", .SubMatches(3)), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
			WITH Matches.Item(0)
				date = .SubMatches(3) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(3) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
			ENDWITH
		CASE reTest(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
			Matches = reExecute(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
			WITH Matches.Item(0)
				date = (era[.SubMatches(0)] + VAL(.SubMatches(1))) + "/" + text(.SubMatches(3), "00") + "/" + IIF(.SubMatches(4)="", "01", text(.SubMatches(4), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(0))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(3), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(3))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(3))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(0), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(1), "st", ""), "nd", ""), "rd", ""), "th", "")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(1), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(0), "st", ""), "nd", ""), "rd", ""), "th", "")
			ENDWITH
		CASE reTest(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			Matches = reExecute(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			WITH Matches.Item(0)
				date = era[.SubMatches(0)] + VAL(.SubMatches(1)) + "/" + text(.SubMatches(2), "00") + "/" + IIF(.SubMatches(3)="", "01", text(.SubMatches(3), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			Matches = reExecute(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + text(.SubMatches(1), "00") + "/" + IIF(.SubMatches(2)="", "01", text(.SubMatches(2), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
			Matches = reExecute(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
			WITH Matches.Item(0)
				date = getYear(today()) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(1), "00")
			ENDWITH
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
			Matches = reExecute(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(0))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
			ENDWITH
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
			Matches = reExecute(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(2))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
			ENDWITH					
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
			Matches = reExecute(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(2))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
			ENDWITH
		DEFAULT
			RESULT = ERR_VALUE
			EXIT
	SELEND
	RESULT = uwscToSerial(GETTIME(0, date))
FEND

//////////////////////////////////////////////////
// 【引数】
//   needle : 検索する値 
//   haystack : 配列 
// 【戻り値】
//   needleが見つかった場合に配列のキー 
//////////////////////////////////////////////////
FUNCTION arraySearch(needle, haystack[])
	DIM i = 0
	FOR item IN haystack
		IFB item = needle THEN
			RESULT = i
			EXIT
		ENDIF
		i = i + 1
	NEXT
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 加算する時間間隔を表す文字列式(yyyy:年、m:月、d:日、ww:週、h:時、n:分、s:秒) 
//   num : dateに加算する値。未来は正、過去は負で指定 
//   date : 時間間隔を加算する日付 
// 【戻り値】
//   日時(date)に、指定した単位(interval)の時間(num)を加算して返します 
//////////////////////////////////////////////////
FUNCTION dateAdd(interval, num, date)
	DIM year, month, day, d
	GETTIME(0, date)
	DIM time = G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2
	SELECT interval
		CASE "yyyy"
			d = (G_TIME_YY + num) + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "m"
			IFB num > 0 THEN
				year = G_TIME_YY + INT((G_TIME_MM + num) / 12)
				month = REPLACE(FORMAT(((G_TIME_MM + num) MOD 12), 2), " ", "0")
			ELSE
				year = G_TIME_YY + CEIL((G_TIME_MM + num) / 12 - 1)
				month = REPLACE(FORMAT(G_TIME_MM - (ABS(num) MOD 12), 2), " ", "0")
			ENDIF
			IF month = "00" THEN month = 12
			day = G_TIME_DD2
			d = "" + year + month + day
			IFB !isDate(d) THEN
				d = year + "/" + month + "/" + "01"
				d = getEndOfMonth(d)
			ELSE
				d = year + "/" + month + "/" + day
			ENDIF
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "d"
			t = GETTIME(num, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "ww"
			t = GETTIME(num * 7, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "h"
			t = GETTIME(num / 24, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "n"
			t = GETTIME(num / 1440, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "s"
			t = GETTIME(num / 86400, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 時間単位(yyyy︰年、q:四半期、m︰月、d︰日、w:週日、ww:週、h:時、n:分、s:秒) 
//   date1 : 日時1 
//   date2 : 日時2 
// 【戻り値】
//   date2からdate1を引いた時間間隔を求めます。 
//////////////////////////////////////////////////
FUNCTION dateDiff(interval, date1, date2)
	DIM y1, y2, m1, m2, d1, d2, d
	SELECT interval
		CASE "yyyy"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			GETTIME(0, date2)
			y2 = G_TIME_YY
			d = y2 - y1
		CASE "q"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = y2 * 4 + CEIL(m2/3) - (y1 * 4 + CEIL(m1/3))
		CASE "m"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = (y2 - y1) * 12 + m2 - m1
		CASE "d"
			d1 = GETTIME(0, date1)
			d2 = GETTIME(0, date2)
			d = (d2 - d1) / 86400
		CASE "w"
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "ww"
			date1 = dateAdd("d", -1 * getWeekday(date1), date1)
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "h"
			d = dateDiff("d", date1, date2) * 24
		CASE "n"
			d = dateDiff("d", date1, date2) * 1440
		CASE "s"
			d = dateDiff("d", date1, date2) * 86400
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   array : 最大公約数を求める数値を格納した配列 
// 【戻り値】
//   最大公約数 
//////////////////////////////////////////////////
FUNCTION GCD(array[])
	DIM c = LENGTH(array)
	DIM rem = array[c-1] MOD array[c-2]
	IFB rem = 0 THEN
		IFB LENGTH(array) = 2 THEN
			RESULT = array[c-2]
			EXIT
		ENDIF
		RESIZE(array, c-2)
		RESULT = GCD(array)
		EXIT
	ENDIF
	array[c-1] = array[c-2]
	array[c-2] = rem
	RESULT = GCD(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”) 
//   m : 第一引数の指定日からプラスマイナスm月とする 
// 【戻り値】
//   dateからm月後の月末の日付 
//////////////////////////////////////////////////
FUNCTION getEndOfMonth(date, m = 0)
	date = dateAdd("m", m + 1, date)
	GETTIME(0, date)
	GETTIME(-G_TIME_DD, date)
	RESULT = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付文字列(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”)もしくはシリアル値 
//   type : 取得する曜日番号の種類を示す0〜3または11〜17の値。1と17は日曜日を1、2と11は月曜日を1とカウントします。11以降はExcel2010で追加された値で、互換性を保つために重複した値があります。 
// 【戻り値】
//   typeで指定した種類によって以下の値を返します。 : (0 : 0(日曜)〜6(土曜)、1 : 1(日曜)~7(土曜)、2 : 1(月曜)~7(日曜)、3 : 0(月曜)〜6(日曜)、11 : 1(月曜)~7(日曜)、12 : 1(火曜)~7(月曜)、13 : 1(水曜)~7(火曜)、14 : 1(木曜)~7(水曜)、15 : 1(金曜)~7(木曜)、16 : 1(土曜)~7(金曜)、17 : 1(日曜)~7(土曜)) 
//////////////////////////////////////////////////
FUNCTION getWeekday(date, type = 1)
	IF VARTYPE(date) <> 258 THEN date = text(date, "yyyy/mm/dd")
	GETTIME(0, date)
	DIM w = G_TIME_WW
	SELECT TRUE
		CASE type = 0
			RESULT = w
		CASE type = 1
			RESULT = w + 1
		CASE type = 2
			RESULT = IIF(w=0, 7, w)
		CASE type = 3
			RESULT = (w+6) MOD 7
		CASE type >= 11
			RESULT = ((getWeekday(date, 2) + 17 - type) MOD 7) + 1
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”) 
// 【戻り値】
//   指定した日付の「年」 
//////////////////////////////////////////////////
FUNCTION getYear(date)
	GETTIME(0, date)
	RESULT = G_TIME_YY4
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値もしくは時刻文字列 
// 【戻り値】
//   時刻から時間を表す0〜23の範囲の値 
//////////////////////////////////////////////////
FUNCTION Hour(serial)
	IF VARTYPE(serial) = 258 THEN serial = timeValue(serial)
	RESULT = INT(serial * 24) MOD 24
FEND

//////////////////////////////////////////////////
// 【引数】
//   expr : 評価する式 
//   truepart : 評価した式がTrueのときに返す値 
//   falsepart : 評価した式がFalseのときに返す値 
// 【戻り値】
//   truepart : 評価した式がTrueのとき、falsepart : 評価した式がFalseのとき 
//////////////////////////////////////////////////
FUNCTION IIF(expr, truepart, falsepart)
	IFB EVAL(expr) THEN
		RESULT = truepart
	ELSE
		RESULT = falsepart
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 存在するかを調べる日付文字列。YYYYMMDD or YYYY/MM/DD or YYYY-MM-DDのいずれかの形式。 
// 【戻り値】
//   TRUE : 日付として認識できる、FALSE : 日付として認識できない 
//////////////////////////////////////////////////
FUNCTION isDate(date)
	TRY
		GETTIME(0, date)
		RESULT = TRUE
	EXCEPT
		RESULT = FALSE
	ENDTRY
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現で検索した結果をMatchesコレクションとして返します。 
//////////////////////////////////////////////////
FUNCTION reExecute(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Execute(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現にマッチするかどうかを示すブール値 
//////////////////////////////////////////////////
FUNCTION reTest(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Test(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   数値を表示書式に基づいて変換した文字列 
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   
//////////////////////////////////////////////////
FUNCTION text(serial, format, hour12 = FALSE)
	HASHTBL startDate
	startDate["明治"] = "1868/01/25"
	startDate["大正"] = "1912/07/30"
	startDate["昭和"] = "1926/12/25"
	startDate["平成"] = "1989/01/08"
	startDate["令和"] = "2019/05/01"
	
	DIM baseDate = "1899/12/30"
	serial = VAL(serial)
	SELECT TRUE
		CASE reTest(format, "\[h+\]")
			Matches = reExecute(format, "\[(h+)\]")
			DIM hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "^h+$")
			Matches = reExecute(format, "^(h+)$")
			hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour MOD 24, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "\[m+\]")
			Matches = reExecute(format, "\[(m+)\]")
			RESULT = text(serial * 1440, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE format = "m"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_MM, "0")
		CASE format = "mm"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_MM2
		CASE format = "n"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN
		CASE format = "nn"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN2
		CASE format = "s"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_SS, "0")
		CASE format = "ss"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_SS2
		CASE format = "yyyy"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_YY4
		CASE format = "yy"
			GETTIME(serial, baseDate)
			RESULT = COPY(G_TIME_YY4, 3, 2)
		CASE format = "e"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 2018
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1988
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1925
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1911
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1867
			SELEND
		CASE format = "ee"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 2018, "00")
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1988, "00")
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1925, "00")
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1911, "00")
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1867, "00")
			SELEND
		CASE format = "g"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "R"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "H"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "S"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "T"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "M"
			SELEND
		CASE format = "gg"
			RESULT = COPY(text(serial, "ggg"), 1, 1)
		CASE format = "ggg"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "令和"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "平成"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "昭和"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "大正"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "明治"
			SELEND
		CASE format = "mmmmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 1)
		CASE format = "mmmm"
			DIM month[] = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
			RESULT = month[text(serial, "m") - 1]
		CASE format = "mmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 3)
		CASE format = "dd"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD2, "00")
		CASE format = "d"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD, "0")
		CASE reTest(format, "^[ad]{3,4}$")
			Matches = reExecute(format, "([ad]{3,4})")
			GETTIME(serial, baseDate)
			DIM aaa[] = "日", "月", "火", "水", "木", "金", "土"
			DIM aaaa[] = "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"
			DIM ddd[] = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
			DIM dddd[] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday";
			RESULT = EVAL(Matches.Item(0).SubMatches(0) + "[" + getWeekday(G_TIME_WW, 1) + "]")
		CASE reTest(format, "(0+\.?0+)?%")
			Matches = reExecute(format, "(0+\.?0+)?%")
			RESULT = text(serial * 100, Matches.Item(0).SubMatches(0)) + "%"
		CASE reTest(format, "^\[DBNum\d{1,4}\](.*?)$")
			Matches = reExecute(format, "^\[DBNum(\d{1,4})\](.*?)$")
			DIM value = VAL(Matches.Item(0).SubMatches(0))
			DIM sss = text(serial, Matches.Item(0).SubMatches(1))
			Matches = reExecute(sss, "(\D+)?(\d+)(\D+)?")
			DIM res = ""
			FOR m = 0 TO Matches.Count - 1
				serial = Matches.Item(m).SubMatches(1)
				SELECT value
					CASE 1, 2
						DIM n[][9] = "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", + _
										"", "壱", "弐", "参", "四", "伍", "六", "七", "八", "九"
						DIM a[][3] = "", "十", "百", "千", + _
										"", "拾", "百", "阡"
						DIM b[][3] = "", "万", "億", "兆", + _
										"", "萬", "億", "兆"
						DIM r = ""
						DIM j = 0
						type = value - 1
						REPEAT
							DIM str = ""
							DIM n4 = serial MOD 10000
							FOR i = LENGTH(n4) TO 1 STEP -1
								s = COPY(n4, i, 1)
								IFB s = 1 AND a[type][LENGTH(n4)-i] <> "" THEN
									str = IIF(s, a[type][LENGTH(n4)-i], "") + str
								ELSE	
									str = n[type][s] + IIF(s, a[type][LENGTH(n4)-i], "") + str
								ENDIF
							NEXT
							IF str <> "" THEN r = str + b[type][j] + r
							j = j + 1
							serial = INT(serial / 10000)
						UNTIL serial = 0
						res = res + Matches.Item(m).SubMatches(0) + r + Matches.Item(m).SubMatches(2)
					CASE 3
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_FULLWIDTH) + Matches.Item(m).SubMatches(2)
					CASE 4
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_HALFWIDTH) + Matches.Item(m).SubMatches(2)
				SELEND
			NEXT
			RESULT = res
		CASE reTest(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			Matches = reExecute(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			DIM array = SPLIT(Matches.Item(0).SubMatches(1), "/")
			ampm = array[IIF(serial - INT(serial) >= 0.5, 1, 0)]
			hour12 = TRUE
			res = ""
			WITH Matches.Item(0)
				res = text(serial, .SubMatches(0), hour12) + ampm + text(serial, .SubMatches(2), hour12)
			ENDWITH
			RESULT = res
		CASE reTest(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			Matches = reExecute(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			FOR n = 0 TO Matches.Count - 1
				IF n = 0 THEN res = Matches.Item(n).SubMatches(0)
			NEXT
			FOR n = 0 TO Matches.Count - 1
				WITH Matches.Item(n)
					res = res + text(serial, .SubMatches(1), hour12) + .SubMatches(3)
				ENDWITH
			NEXT
			RESULT = res
		CASE format = "0/0"
			DIM separator = POS(".", serial)
			DIM g = 0
			IFB separator <> 0 THEN
				DIM keta = LENGTH(serial)
				DIM shift = POWER(10, keta - separator)
				IFB shift >= POWER(10, 15) THEN
					DIM position = 0
					FOR i = 0 TO 14
						IFB serial * POWER(10, i) - serial >= 1 THEN
							position = i
							BREAK
						ENDIF
					NEXT
					tmp = serial * POWER(10, position)
					FOR i = 1 TO 15
						r = (tmp * POWER(10, i)) / serial - (tmp / serial)
						a1 = tmp * POWER(10, i) - tmp
						IF a1 = INT(a1) THEN BREAK 
					NEXT
					DIM frac[] = a1, r
					g = GCD(frac)
					RESULT = (a1/g) + "/" + (r/g)
				ELSE
					DIM molecule = serial * shift	// 分子
					DIM denominator = shift		// 分母
					DIM nums[] = molecule, denominator
					g = GCD(nums)
					molecule = molecule / g
					denominator = denominator / g
					RESULT = molecule + "/" + denominator
				ENDIF
			ELSE
				RESULT = serial + "/1"
			ENDIF
		CASE reTest(format, "(0+)\.?(0+)?") AND UBound(SPLIT(format, ".")) <= 1 
			Matches = reExecute(format, "(0+)\.?(0+)?")
			len1 = LENGTH(Matches.Item(0).SubMatches(0))
			len2 = LENGTH(Matches.Item(0).SubMatches(1))
			DIM arr[] = LENGTH(INT(serial)), len1
			IFB POS(".", format) THEN
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX) + len2 + 1, len2), " ", "0")
			ELSE
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX)), " ", "0")
			ENDIF
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 時刻文字列。hh:nn:ss AM/PM、hh:nn AM/PM、hh AM/PM、hh:nn:ss、hh:nn、hh時nn分ss秒、hh時nn分のいずれかの形式を指定。 
// 【戻り値】
//   シリアル値 (例)0…00:00:00、0.5…12:00:00、0.999988425925926…23:59:59 
//////////////////////////////////////////////////
FUNCTION timeValue(str)
	DIM serial = 0
	DIM Matches
	DIM pattern = "(\d+)"
	DIM hh = "(0?[0-9]|1[0-2])"
	DIM ampm = "([AP]M|[ap]m)"
	SELECT TRUE
		CASE reTest(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(3)) + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(2)) + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + hh + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0) MOD 12) + IIF(reTest(.SubMatches(1), "AM|am"), 0, 12)
				serial = serial / 24
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		DEFAULT
			serial = ERR_VALUE
	SELEND
	RESULT = serial - INT(serial)
FEND

//////////////////////////////////////////////////
// 【引数】
//   
// 【戻り値】
//   今日の日付 
//////////////////////////////////////////////////
FUNCTION today()
	GETTIME()
	RESULT = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
FEND

//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND

//////////////////////////////////////////////////
// 【引数】
//   uwscTime : UWSC時間 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION uwscToSerial(uwscTime)
	uwscDate = uwscTime / 86400
	RESULT = 36526 + uwscDate
FEND

解説

  1. 2行目
    UWSC
    	CONST cutOffYear = 2049
    カットオフ年の終了年を指定します。西暦を2桁で指定したときcutOffYearで指定された年の値までの100年間で西暦を決めます。デフォルトでは2049年までの100年間(1950〜2049年の範囲)で年を決定します。例えば年を25と指定した場合は2025年86と指定した場合は1986年として扱います。
  2. 3行目
    UWSC
    	DIM Months[] = "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    英語月名を格納した配列。日付文字列として英語月名が指定されたときに数字に変換するときに使用します。
  3. 5行目
    UWSC
    	HASHTBL Pattern
    正規表現を格納する連想配列。
  4. 6行目
    UWSC
    	Pattern["year"] = "(19[0-9]{2}|[2-9][0-9]{3})"
    西暦を表す正規表現。19009999年の範囲の値。
  5. 7行目
    UWSC
    	Pattern["year2"] = "([0-9]{2})"
    2桁の年を表す正規表現。0099の範囲の値。
  6. 8行目
    UWSC
    	Pattern["era"] = "(明治?|大正?|昭和?|平成?|令和?|[MTSHR])(元|\d+)"
    元号を格納した連想配列。明治大正昭和平成令和MTSHRにマッチします。
  7. 9行目
    UWSC
    	Pattern["month"] = "(1[0-2]|0?[1-9])"
    月を表す正規表現。112の範囲の値。0埋めの有無に関係なくマッチします。
  8. 10行目
    UWSC
    	Pattern["month2"] = "(0[1-9]|1[0-2])"
    2桁の月を表す正規表現。0112の範囲の数値。
  9. 11行目
    UWSC
    	Pattern["month_en"] = "(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Ap(?:ril)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)"
    英語月名を表す正規表現。JanuaryDecember、短縮形のJanDecSeptにマッチします。
  10. 12行目
    UWSC
    	Pattern["day"] = "([1-2][0-9]|3[0-1]|0?[1-9])"
    日を表す正規表現。131の範囲の値。0埋めの有無に関係なくマッチします。
  11. 13行目
    UWSC
    	Pattern["day2"] = "(0[1-9]|[1-2][0-9]|3[0-1])"
    2桁の日を表す正規表現。0131の範囲の数値。
  12. 14行目
    UWSC
    	Pattern["day_en"] = "(1(?:st)|2?2(?:nd)|2?3(?:rd)|[1-3]?[04-9](?:th)|1[1-3](?:th)|[1-3]1(?:st))"
    英語の日付を表す序数表記。1st31stの範囲。
  13. 15行目
    UWSC
    	Pattern["separator"] = "([./\-])"
    年月日を区切る文字を表す正規表現。例えば2024/10/24/の部分。./-にマッチします。
  14. 17-22行目
    UWSC
    	HASHTBL era
    	era["明治"] = 1867;	era["明"] = 1867;	era["M"] = 1867;
    	era["大正"] = 1911;	era["大"] = 1911;	era["T"] = 1911;
    	era["昭和"] = 1925;	era["昭"] = 1925;	era["S"] = 1925;
    	era["平成"] = 1988;	era["平"] = 1988;	era["H"] = 1988;
    	era["令和"] = 2018;	era["令"] = 2018;	era["R"] = 2018;
    元号から西暦に変換するための値を格納する連想配列。8行目>>>でマッチする文字列に対応しています。例えば令和6年の場合、キー名が令和の値20186を足すことで西暦2024年と求められます。
  15. 24,129行目
    UWSC
    	SELECT TRUE
    		…
    strで指定された日付形式にマッチする正規表現のCASE句を実行します。いずれのCASE句でも行っている処理はすべてマッチした日付文字列からを取り出してyyyy/mm/ddの形式でdateに代入することです。
  16. 25-29行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["year"] + Pattern["separator"] + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
    			Matches = reExecute(str, "\b(?:" + Pattern["year"] + Pattern["separator"] + ")?" + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
    			WITH Matches.Item(0)
    				date = .SubMatches(0) + "/" + text(.SubMatches(2), "00") + "/" + text(IIF(.SubMatches(3)="", "01", .SubMatches(3)), "00")
    			ENDWITH
    yyyy/mm/ddyyyy-mm-ddyyyy.mm.ddもしくはyyyy/mmyyyy-mmyyyy.mmの形式の日付文字列にマッチします。\2は区切り文字を表していての区切り文字との区切り文字が同じ場合にマッチします。例えば2024/10/23にはマッチしますが、2024/10-23にはマッチしません。dateにはyyyy/mm/ddの形式に変換した日付文字列が代入されます。日が省略された場合はその年月の1日として扱い、例えば2024/10と指定した場合は2024/10/01として扱います。IIF関数 (自作関数)の箇所がその処理をしています。
  17. 30-34行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
    			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
    			WITH Matches.Item(0)
    				date = .SubMatches(3) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
    			ENDWITH
    mm/dd/yyyymm-dd-yyyymm.dd.yyyyの形式の日付文字列にマッチします。dateにはyyyy/mm/ddの形式に変換した日付文字列が代入されます。
  18. 35-39行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
    			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
    			WITH Matches.Item(0)
    				date = .SubMatches(3) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
    			ENDWITH
    dd/mm/yyyydd-mm-yyyydd.mm.yyyyの形式の日付文字列。dateにはyyyy/mm/ddの形式に変換した日付文字列が代入されます。
  19. 40-44行目
    UWSC
    		CASE reTest(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
    			Matches = reExecute(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
    			WITH Matches.Item(0)
    				date = (era[.SubMatches(0)] + VAL(.SubMatches(1))) + "/" + text(.SubMatches(3), "00") + "/" + IIF(.SubMatches(4)="", "01", text(.SubMatches(4), "00"))
    			ENDWITH
    ggge/mm/ddggge-mm-ddggge.mm.ddgge/mm/ddgge-mm-ddgge.mm.ddge/mm/ddge-mm-ddge.mm.ddの形式の日付文字列にマッチします。
  20. 45-50行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
    			Matches = reExecute(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
    			WITH Matches.Item(0)
    				y = VAL(.SubMatches(0))
    				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(3), "00")
    			ENDWITH
    2桁年/月/日2桁年-月-日2桁年.月.日の形式の日付文字列。yにはマッチした2桁年を代入し計算に使うためVAL関数 (スクリプト関数)で数値に変換します。2行目>>>で指定されたカットオフ年の範囲に収まるような4桁年を求めます。dateには年/月/日の形式に変換した日付文字列が代入されます。
  21. 51-56行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
    			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
    			WITH Matches.Item(0)
    				y = VAL(.SubMatches(3))
    				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
    			ENDWITH
    月/日/2桁年月-日-2桁年月.日.2桁年の形式の日付文字列。年月日の順序が違うだけで処理は38行目>>>と同じです。
  22. 57-62行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
    			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
    			WITH Matches.Item(0)
    				y = VAL(.SubMatches(3))
    				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
    			ENDWITH
    日/月/2桁年日-月-2桁年日.月.2桁年の形式の日付文字列。処理は38行目>>>と同じです。
  23. 63-67行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
    			Matches = reExecute(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
    			WITH Matches.Item(0)
    				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(0), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(1), "st", ""), "nd", ""), "rd", ""), "th", "")
    			ENDWITH
    アメリカ式の月 日, 年の形式の日付文字列。
  24. 68-72行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
    			Matches = reExecute(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
    			WITH Matches.Item(0)
    				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(1), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(0), "st", ""), "nd", ""), "rd", ""), "th", "")
    			ENDWITH
    イギリス式の日 月 年の形式の日付文字列。
  25. 73-77行目
    UWSC
    		CASE reTest(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
    			Matches = reExecute(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
    			WITH Matches.Item(0)
    				date = era[.SubMatches(0)] + VAL(.SubMatches(1)) + "/" + text(.SubMatches(2), "00") + "/" + IIF(.SubMatches(3)="", "01", text(.SubMatches(3), "00"))
    			ENDWITH
    和暦年月日和暦年月の形式の日付文字列。
  26. 78-82行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
    			Matches = reExecute(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
    			WITH Matches.Item(0)
    				date = .SubMatches(0) + "/" + text(.SubMatches(1), "00") + "/" + IIF(.SubMatches(2)="", "01", text(.SubMatches(2), "00"))
    			ENDWITH
    年月日年月の形式の日付文字列。
  27. 83-87行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
    			Matches = reExecute(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
    			WITH Matches.Item(0)
    				date = getYear(today()) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(1), "00")
    			ENDWITH
    月日の形式の日付文字列。
  28. 88-93行目
    UWSC
    		CASE LENGTH(str) = 6 AND reTest(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
    			Matches = reExecute(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
    			WITH Matches.Item(0)
    				y = VAL(.SubMatches(0))
    				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
    			ENDWITH
    年月日を6桁で表した形式の日付文字列。
  29. 94-99行目
    UWSC
    		CASE LENGTH(str) = 6 AND reTest(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
    			Matches = reExecute(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
    			WITH Matches.Item(0)
    				y = VAL(.SubMatches(2))
    				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
    			ENDWITH
    月日年を6桁で表した形式の日付文字列。
  30. 100-105行目
    UWSC
    		CASE LENGTH(str) = 6 AND reTest(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
    			Matches = reExecute(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
    			WITH Matches.Item(0)
    				y = VAL(.SubMatches(2))
    				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
    			ENDWITH
    日月年を6桁で表した形式の日付文字列。
  31. 106-110行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
    			Matches = reExecute(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
    			WITH Matches.Item(0)
    				date = .SubMatches(0) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
    			ENDWITH
    年月日の形式の日付文字列。
  32. 111-115行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
    			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
    			WITH Matches.Item(0)
    				date = .SubMatches(2) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
    			ENDWITH
    月日年の形式の日付文字列。
  33. 116-120行目
    UWSC
    		CASE reTest(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
    			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
    			WITH Matches.Item(0)
    				date = .SubMatches(2) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
    			ENDWITH
    日月年の形式の日付文字列。
  34. 121-123行目
    UWSC
    		DEFAULT
    			RESULT = ERR_VALUE
    			EXIT
    いずれの日付文字列の形式にもマッチしなかった場合はERR_VALUEを返します。
  35. 125行目
    UWSC
    	RESULT = uwscToSerial(GETTIME(0, date))
    日付文字列dateuwscToSerial関数 (自作関数)でシリアル値に変換した値を戻り値として返します。

日付文字列の形式

以下はdateValue関数で使える日付書式指定子です。

日付書式指定子 意味
yyyy
yy 西暦の下2桁
mm
mmm 英語月(Jan, Feb, Mar, …)
mmmm 英語月(January, February, March, …)
dd
g 元号をアルファベットで表記
MTSHR
gg 元号を漢字の頭文字で表記
ggg 元号を漢字で表記
明治大正昭和平成令和
e 元号の年。令和6年ならば 6

以下はdateValue関数でシリアル値に変換することができる日付文字列の形式です。普段使わない形式もありますが、プログラムが複雑になるのでとりあえず日付と認識できるものとして記載しています。

を省略した日付文字列を指定した場合は今年、を省略した場合はその月の月初日(1日)として扱われます。

dateValue関数は日付文字列をシリアル値に変換する関数なので時刻が含まれる場合、時刻部分は無視(0時扱い)となります。

以下の例は2024年に実行したときの結果です。

日付文字列のパターン
yyyy/mm/dd1990/04/01
yyyy-mm-dd1990-04-01
yyyy.mm.dd1990.04.01
yyyy/mm1990/04
yyyy-mm1990-04
yyyy.mm1990.04
mm/dd/yyyy04/01/1990
mm-dd-yyyy04-01-1990
mm.dd.yyyy04.01.1990
dd/mm/yyyy01/04/1990
dd-mm-yyyy01-04-1990
dd.mm.yyyy01.04.1990
ggge/mm/dd平成02/04/01
gge/mm/dd平02/04/01
ge/mm/ddH02/04/01
ggge-mm-dd平成02-04-01
gge-mm-dd平02-04-01
ge-mm-ddH02-04-01
ggge.mm.dd平成02.04.01
gge.mm.dd平02.04.01
ge.mm.ddH02.04.01
ggge/mm平成02/04
gge/mm平02/04
ge/mmH02/04
ggge-mm平成02-04
gge-mm平02-04
ge-mmH02-04
ggge.mm平成02.04
gge.mm平02.04
ge.mmH02.04
yy/mm/dd90/04/01
yy-mm-dd90-04-01
yy.mm.dd90.04.01
mm/dd/yy04/01/90
mm-dd-yy04-01-90
mm.dd.yy04.01.90
dd/mm/yy01/04/90
dd-mm-yy01-04-90
dd.mm.yy01.04.90
mm/dd04/01
mm-dd04-01
mm.dd04.01
mmmm d"st", yyyy
mmmm d"nd", yyyy
mmmm d"rd", yyyy
mmmm d"th", yyyy
April 1st, 1990
mmmm d"st",yyyy
mmmm d"nd",yyyy
mmmm d"rd",yyyy
mmmm d"th",yyyy
April 1st,1990
d"st" mmmm yyyy
d"nd" mmmm yyyy
d"rd" mmmm yyyy
d"th" mmmm yyyy
1st April 1990
ggge年mm月dd日平成02年04月01日
gge年mm月dd日平02年04月01日
ge年mm月dd日H02年04月01日
ggge年mm月平成02年04月
gge年mm月平02年04月
ge年mm月H02年04月
yyyy年mm月dd日1990年04月01日
yyyy年mm月1990年04月
mm月dd日04月01日
yymmdd900401
mmddyy040190
ddmmyy010490
yyyymmdd19900401
mmddyyyy04011990
ddmmyyyy01041990

日付文字列は区切り文字を無視した場合年月日月日年日月年の順番でマッチ検索を行い最初にマッチした形式で認識します。例えば01/02/2024という日付文字列の場合は年/月/日月/日/年日/月/年の順番でマッチ検索を行っていきます。最初にマッチするのは月/日/年の形式なので01/02/20242024年01月02日として扱われます。

次に14/02/2024という日付文字列の場合、月/日/年という形式だと2024年14月02日と存在しない日付となるので日/月/年の形式にマッチし2024年02月14日として扱われます。

使い方

年月日の形式からシリアル値を求める

2024年10月28日のシリアル値を求めます。

UWSC
PRINT dateValue("2024年10月28日")
結果
プレーンテキスト
45593

年が省略された日付文字列のシリアル値

年が省略されている場合は今年として扱います。

UWSC
PRINT dateValue("10月28日")
結果
プレーンテキスト
45593

日が省略された日付文字列のシリアル値

日が省略されている場合は指定年月の月初(1日)として扱います。

以下は2024年10月01日として扱われます。

UWSC
PRINT dateValue("2024年10月")
結果
プレーンテキスト
45566

今日のシリアル値

today関数 (自作関数)で取得した今日の日付文字列からシリアル値を求めます。

UWSC
PRINT dateValue(today())
結果
プレーンテキスト
45593

他の日付形式に変換

違う形式に変換する場合はtext関数 (自作関数)を使います。

以下は2024年10月28日をシリアル値に変換してからtext関数 (自作関数)ggge年mm月dd日の形式に変換しています。

UWSC
PRINT text(dateValue("2024年10月28日"), "ggge年mm月dd日")
結果
プレーンテキスト
令和6年10月28日

終了日を過ぎた和暦表記の日付文字列を対応する元号に変換

以下は昭和100年1月1日を対応する元号の日付文字列に変換します。

UWSC
PRINT text(dateValue("昭和100年1月1日"), "ggge年mm月dd日")
結果
プレーンテキスト
令和7年01月01日

色々な形式の日付文字列の変換

dateValue関数で変換できる日付文字列の形式を一通り実行しています。

UWSC
DIM array[] = "1995/01/17", "1995-01-17", "1995.01.17", _			// yyyy/mm/dd, yyyy-mm-dd, yyyy.mm.dd
				"01/17", "01-17", "01.17", _						// mm/dd, mm-dd, mm.dd
				"17/01/1995", "17-01-1995", "17.01.1995", _			// dd/mm/yyyy, dd-mm-yyyy, dd.mm.yyyy
				"平成7/01/17", "平7/01/17", "H7/01/17", _			// ggge/mm/dd, gge/mm/dd ge/mm/dd
				"平成7-01-17", "平7-01-17", "H7-01-17", _			// ggge-mm-dd, gge-mm-dd, ge.mm.dd
				"平成7.01.17", "平7.01.17", "H7.01.17", _			// ggge.mm.dd, gge.mm.dd, ge.mm.dd
				"平成7/01", "平7/01", "H7/01", _						// ggge/mm, gge/mm, ge/mm
				"平成7-01", "平7-01", "H7-01", _						// ggge-mm, gge-mm, ge-mm
				"平成7.01", "平7.01", "H7.01", _						// ggge.mm, gge.mm, ge.mm
				"95/01/17", "95-01-17", "95.01.17", _				// yy/mm/dd, yy-mm-dd, yy.mm.dd
				"01/17/95", "01-17-95", "01.17.95", _				// mm/dd/yy, mm-dd-yy, mm.dd.yy
				"17/01/95", "17-01-95", "17.01.95", _				// dd/mm/yy, dd-mm-yy, dd.mm.yy
				"01/17", "01-17", "01.17", _						// mm/dd, mm-dd, mm.dd
				"January 17th, 1995", "Jan 17th, 1995", _			// mmmmm d"st", yyyy; mmmm d"st", yyyy
				"17th January 1995", "17th Jan 1995", _				// d"st" mmmmm yyyy; d"st" mmmm yyyy
				"平成7年1月17日", "平成7年1月", _						// ggge年mm月dd日, ggge年mm月
				"平7年1月17日", "平7年1月", _						// gge年mm月dd日, gge年mm月
				"H7年1月17日", "H7年1月", _							// ge年mm月dd日, ge年mm月
				"1995年1月17日", "1995年1月", "1月1日", _			// yyyy年mm月dd日, yyyy年mm月, mm月dd日
				"950117", "011795", "170195", _						// yymmdd, mmddyy, ddmmyy
				"19950117", "01171995", "17011995"					// yyyymmdd, mmddyyyy, ddmmyyyy

PRINT "日付文字列;シリアル値;日付に再変換"

FOR item IN array
	PRINT item + ";" + dateValue(item) + ";" + text(dateValue(item), "yyyy年mm月dd日")
NEXT
結果
CSV
日付文字列;           シリアル値;   日付に再変換
1995/01/17;           34716;        1995年01月17日
1995-01-17;           34716;        1995年01月17日
1995.01.17;           34716;        1995年01月17日
01/17;                45308;        2024年01月17日
01-17;                45308;        2024年01月17日
01.17;                45308;        2024年01月17日
17/01/1995;           34716;        1995年01月17日
17-01-1995;           34716;        1995年01月17日
17.01.1995;           34716;        1995年01月17日
平成7/01/17;          34716;        1995年01月17日
平7/01/17;            34716;        1995年01月17日
H7/01/17;             34716;        1995年01月17日
平成7-01-17;          34716;        1995年01月17日
平7-01-17;            34716;        1995年01月17日
H7-01-17;             34716;        1995年01月17日
平成7.01.17;          34716;        1995年01月17日
平7.01.17;            34716;        1995年01月17日
H7.01.17;             34716;        1995年01月17日
平成7/01;             34700;        1995年01月01日
平7/01;               34700;        1995年01月01日
H7/01;                34700;        1995年01月01日
平成7-01;             34700;        1995年01月01日
平7-01;               34700;        1995年01月01日
H7-01;                34700;        1995年01月01日
平成7.01;             34700;        1995年01月01日
平7.01;               34700;        1995年01月01日
H7.01;                34700;        1995年01月01日
95/01/17;             34716;        1995年01月17日
95-01-17;             34716;        1995年01月17日
95.01.17;             34716;        1995年01月17日
01/17/95;             34716;        1995年01月17日
01-17-95;             34716;        1995年01月17日
01.17.95;             34716;        1995年01月17日
17/01/95;             34716;        1995年01月17日
17-01-95;             34716;        1995年01月17日
17.01.95;             34716;        1995年01月17日
01/17;                45308;        2024年01月17日
01-17;                45308;        2024年01月17日
01.17;                45308;        2024年01月17日
January 17th, 1995;   34716;        1995年01月17日
Jan 17th, 1995;       34716;        1995年01月17日
17th January 1995;    34716;        1995年01月17日
17th Jan 1995;        34716;        1995年01月17日
平成7年1月17日;       34716;        1995年01月17日
平成7年1月;           34700;        1995年01月01日
平7年1月17日;         34716;        1995年01月17日
平7年1月;             34700;        1995年01月01日
H7年1月17日;          34716;        1995年01月17日
H7年1月;              34700;        1995年01月01日
1995年1月17日;        34716;        1995年01月17日
1995年1月;            34700;        1995年01月01日
1月1日;               45292;        2024年01月01日
950117;               34716;        1995年01月17日
011795;               34716;        1995年01月17日
170195;               34716;        1995年01月17日
19950117;             34716;        1995年01月17日
01171995;             34716;        1995年01月17日
17011995;             34716;        1995年01月17日

プログラム実行例

写真の撮影日時毎に枚数を取得

選択したフォルダにある写真の撮影日時を取得・カウントします。

UWSC
DIM Shell = CREATEOLEOBJ("Shell.Application")
Folder = Shell.BrowseForFolder(0, "フォルダを選択してください。", 0)

HASHTBL iColumn

FOR i = 0 TO 350
	iColumn[Folder.GetDetailsOf(EMPTYPARAM, i)] = i
NEXT

HASHTBL dates

FOR i = 0 TO Folder.Items.Count - 1	
	FUKIDASI(i + "/" + Folder.Items.Count)
	DIM FolderItem = Folder.Items.Item(i)
	DIM datetime = Folder.GetDetailsOf(FolderItem, iColumn["撮影日時"])
	IFB datetime = "" THEN
		dates["0000/00/00"] = dates["0000/00/00"] + 1
	ELSE
		deleteControlCharacter(datetime)
		DIM date = text(datevalue(datetime), "yyyy/mm/dd")
		dates[date] = dates[date] + 1
	ENDIF
NEXT

FOR i = 0 TO LENGTH(dates) - 1
	PRINT dates[i, HASH_KEY] + "<#TAB>" + dates[i, HASH_VAL]
NEXT

// 制御文字を削除する
FUNCTION deleteControlCharacter(Var str)
	DIM array[] = CHR(8206), CHR(8207)
	FOR item IN array
		str = REPLACE(str, item, "")
	NEXT
	RESULT = str
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 加算する時間間隔を表す文字列式(yyyy:年、m:月、d:日、ww:週、h:時、n:分、s:秒) 
//   num : dateに加算する値。未来は正、過去は負で指定 
//   date : 時間間隔を加算する日付 
// 【戻り値】
//   日時(date)に、指定した単位(interval)の時間(num)を加算して返します 
//////////////////////////////////////////////////
FUNCTION dateAdd(interval, num, date)
	DIM year, month, day, d
	GETTIME(0, date)
	DIM time = G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2
	SELECT interval
		CASE "yyyy"
			d = (G_TIME_YY + num) + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "m"
			IFB num > 0 THEN
				year = G_TIME_YY + INT((G_TIME_MM + num) / 12)
				month = REPLACE(FORMAT(((G_TIME_MM + num) MOD 12), 2), " ", "0")
			ELSE
				year = G_TIME_YY + CEIL((G_TIME_MM + num) / 12 - 1)
				month = REPLACE(FORMAT(G_TIME_MM - (ABS(num) MOD 12), 2), " ", "0")
			ENDIF
			IF month = "00" THEN month = 12
			day = G_TIME_DD2
			d = "" + year + month + day
			IFB !isDate(d) THEN
				d = year + "/" + month + "/" + "01"
				d = getEndOfMonth(d)
			ELSE
				d = year + "/" + month + "/" + day
			ENDIF
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "d"
			t = GETTIME(num, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "ww"
			t = GETTIME(num * 7, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "h"
			t = GETTIME(num / 24, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "n"
			t = GETTIME(num / 1440, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "s"
			t = GETTIME(num / 86400, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 時間単位(yyyy︰年、q:四半期、m︰月、d︰日、w:週日、ww:週、h:時、n:分、s:秒) 
//   date1 : 日時1 
//   date2 : 日時2 
// 【戻り値】
//   date2からdate1を引いた時間間隔を求めます。 
//////////////////////////////////////////////////
FUNCTION dateDiff(interval, date1, date2)
	DIM y1, y2, m1, m2, d1, d2, d
	SELECT interval
		CASE "yyyy"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			GETTIME(0, date2)
			y2 = G_TIME_YY
			d = y2 - y1
		CASE "q"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = y2 * 4 + CEIL(m2/3) - (y1 * 4 + CEIL(m1/3))
		CASE "m"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = (y2 - y1) * 12 + m2 - m1
		CASE "d"
			d1 = GETTIME(0, date1)
			d2 = GETTIME(0, date2)
			d = (d2 - d1) / 86400
		CASE "w"
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "ww"
			date1 = dateAdd("d", -1 * getWeekday(date1), date1)
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "h"
			d = dateDiff("d", date1, date2) * 24
		CASE "n"
			d = dateDiff("d", date1, date2) * 1440
		CASE "s"
			d = dateDiff("d", date1, date2) * 86400
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   array : 最大公約数を求める数値を格納した配列 
// 【戻り値】
//   最大公約数 
//////////////////////////////////////////////////
FUNCTION GCD(array[])
	DIM c = LENGTH(array)
	DIM rem = array[c-1] MOD array[c-2]
	IFB rem = 0 THEN
		IFB LENGTH(array) = 2 THEN
			RESULT = array[c-2]
			EXIT
		ENDIF
		RESIZE(array, c-2)
		RESULT = GCD(array)
		EXIT
	ENDIF
	array[c-1] = array[c-2]
	array[c-2] = rem
	RESULT = GCD(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”) 
//   m : 第一引数の指定日からプラスマイナスm月とする 
// 【戻り値】
//   dateからm月後の月末の日付 
//////////////////////////////////////////////////
FUNCTION getEndOfMonth(date, m = 0)
	date = dateAdd("m", m + 1, date)
	GETTIME(0, date)
	GETTIME(-G_TIME_DD, date)
	RESULT = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付文字列(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”)もしくはシリアル値 
//   type : 取得する曜日番号の種類を示す0〜3または11〜17の値。1と17は日曜日を1、2と11は月曜日を1とカウントします。11以降はExcel2010で追加された値で、互換性を保つために重複した値があります。 
// 【戻り値】
//   typeで指定した種類によって以下の値を返します。 : (0 : 0(日曜)〜6(土曜)、1 : 1(日曜)~7(土曜)、2 : 1(月曜)~7(日曜)、3 : 0(月曜)〜6(日曜)、11 : 1(月曜)~7(日曜)、12 : 1(火曜)~7(月曜)、13 : 1(水曜)~7(火曜)、14 : 1(木曜)~7(水曜)、15 : 1(金曜)~7(木曜)、16 : 1(土曜)~7(金曜)、17 : 1(日曜)~7(土曜)) 
//////////////////////////////////////////////////
FUNCTION getWeekday(date, type = 1)
	IF VARTYPE(date) <> 258 THEN date = text(date, "yyyy/mm/dd")
	GETTIME(0, date)
	DIM w = G_TIME_WW
	SELECT TRUE
		CASE type = 0
			RESULT = w
		CASE type = 1
			RESULT = w + 1
		CASE type = 2
			RESULT = IIF(w=0, 7, w)
		CASE type = 3
			RESULT = (w+6) MOD 7
		CASE type >= 11
			RESULT = ((getWeekday(date, 2) + 17 - type) MOD 7) + 1
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値もしくは時刻文字列 
// 【戻り値】
//   時刻から時間を表す0〜23の範囲の値 
//////////////////////////////////////////////////
FUNCTION Hour(serial)
	IF VARTYPE(serial) = 258 THEN serial = timeValue(serial)
	RESULT = INT(serial * 24) MOD 24
FEND

//////////////////////////////////////////////////
// 【引数】
//   expr : 評価する式 
//   truepart : 評価した式がTrueのときに返す値 
//   falsepart : 評価した式がFalseのときに返す値 
// 【戻り値】
//   truepart : 評価した式がTrueのとき、falsepart : 評価した式がFalseのとき 
//////////////////////////////////////////////////
FUNCTION IIF(expr, truepart, falsepart)
	IFB EVAL(expr) THEN
		RESULT = truepart
	ELSE
		RESULT = falsepart
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 存在するかを調べる日付文字列。YYYYMMDD or YYYY/MM/DD or YYYY-MM-DDのいずれかの形式。 
// 【戻り値】
//   TRUE : 日付として認識できる、FALSE : 日付として認識できない 
//////////////////////////////////////////////////
FUNCTION isDate(date)
	TRY
		GETTIME(0, date)
		RESULT = TRUE
	EXCEPT
		RESULT = FALSE
	ENDTRY
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現で検索した結果をMatchesコレクションとして返します。 
//////////////////////////////////////////////////
FUNCTION reExecute(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Execute(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現にマッチするかどうかを示すブール値 
//////////////////////////////////////////////////
FUNCTION reTest(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Test(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   数値を表示書式に基づいて変換した文字列 
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   
//////////////////////////////////////////////////
FUNCTION text(serial, format, hour12 = FALSE)
	HASHTBL startDate
	startDate["明治"] = "1868/01/25"
	startDate["大正"] = "1912/07/30"
	startDate["昭和"] = "1926/12/25"
	startDate["平成"] = "1989/01/08"
	startDate["令和"] = "2019/05/01"
	
	DIM baseDate = "1899/12/30"
	serial = VAL(serial)
	SELECT TRUE
		CASE reTest(format, "\[h+\]")
			Matches = reExecute(format, "\[(h+)\]")
			DIM hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "^h+$")
			Matches = reExecute(format, "^(h+)$")
			hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour MOD 24, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "\[m+\]")
			Matches = reExecute(format, "\[(m+)\]")
			RESULT = text(serial * 1440, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE format = "m"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_MM, "0")
		CASE format = "mm"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_MM2
		CASE format = "n"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN
		CASE format = "nn"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN2
		CASE format = "s"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_SS, "0")
		CASE format = "ss"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_SS2
		CASE format = "yyyy"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_YY4
		CASE format = "yy"
			GETTIME(serial, baseDate)
			RESULT = COPY(G_TIME_YY4, 3, 2)
		CASE format = "e"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 2018
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1988
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1925
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1911
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1867
			SELEND
		CASE format = "ee"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 2018, "00")
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1988, "00")
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1925, "00")
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1911, "00")
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1867, "00")
			SELEND
		CASE format = "g"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "R"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "H"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "S"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "T"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "M"
			SELEND
		CASE format = "gg"
			RESULT = COPY(text(serial, "ggg"), 1, 1)
		CASE format = "ggg"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "令和"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "平成"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "昭和"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "大正"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "明治"
			SELEND
		CASE format = "mmmmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 1)
		CASE format = "mmmm"
			DIM month[] = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
			RESULT = month[text(serial, "m") - 1]
		CASE format = "mmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 3)
		CASE format = "dd"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD2, "00")
		CASE format = "d"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD, "0")
		CASE reTest(format, "^[ad]{3,4}$")
			Matches = reExecute(format, "([ad]{3,4})")
			GETTIME(serial, baseDate)
			DIM aaa[] = "日", "月", "火", "水", "木", "金", "土"
			DIM aaaa[] = "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"
			DIM ddd[] = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
			DIM dddd[] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday";
			RESULT = EVAL(Matches.Item(0).SubMatches(0) + "[" + getWeekday(G_TIME_WW, 1) + "]")
		CASE reTest(format, "(0+\.?0+)?%")
			Matches = reExecute(format, "(0+\.?0+)?%")
			RESULT = text(serial * 100, Matches.Item(0).SubMatches(0)) + "%"
		CASE reTest(format, "^\[DBNum\d{1,4}\](.*?)$")
			Matches = reExecute(format, "^\[DBNum(\d{1,4})\](.*?)$")
			DIM value = VAL(Matches.Item(0).SubMatches(0))
			DIM sss = text(serial, Matches.Item(0).SubMatches(1))
			Matches = reExecute(sss, "(\D+)?(\d+)(\D+)?")
			DIM res = ""
			FOR m = 0 TO Matches.Count - 1
				serial = Matches.Item(m).SubMatches(1)
				SELECT value
					CASE 1, 2
						DIM n[][9] = "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", + _
										"", "壱", "弐", "参", "四", "伍", "六", "七", "八", "九"
						DIM a[][3] = "", "十", "百", "千", + _
										"", "拾", "百", "阡"
						DIM b[][3] = "", "万", "億", "兆", + _
										"", "萬", "億", "兆"
						DIM r = ""
						DIM j = 0
						type = value - 1
						REPEAT
							DIM str = ""
							DIM n4 = serial MOD 10000
							FOR i = LENGTH(n4) TO 1 STEP -1
								s = COPY(n4, i, 1)
								IFB s = 1 AND a[type][LENGTH(n4)-i] <> "" THEN
									str = IIF(s, a[type][LENGTH(n4)-i], "") + str
								ELSE	
									str = n[type][s] + IIF(s, a[type][LENGTH(n4)-i], "") + str
								ENDIF
							NEXT
							IF str <> "" THEN r = str + b[type][j] + r
							j = j + 1
							serial = INT(serial / 10000)
						UNTIL serial = 0
						res = res + Matches.Item(m).SubMatches(0) + r + Matches.Item(m).SubMatches(2)
					CASE 3
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_FULLWIDTH) + Matches.Item(m).SubMatches(2)
					CASE 4
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_HALFWIDTH) + Matches.Item(m).SubMatches(2)
				SELEND
			NEXT
			RESULT = res
		CASE reTest(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			Matches = reExecute(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			DIM array = SPLIT(Matches.Item(0).SubMatches(1), "/")
			ampm = array[IIF(serial - INT(serial) >= 0.5, 1, 0)]
			hour12 = TRUE
			res = ""
			WITH Matches.Item(0)
				res = text(serial, .SubMatches(0), hour12) + ampm + text(serial, .SubMatches(2), hour12)
			ENDWITH
			RESULT = res
		CASE reTest(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			Matches = reExecute(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			FOR n = 0 TO Matches.Count - 1
				IF n = 0 THEN res = Matches.Item(n).SubMatches(0)
			NEXT
			FOR n = 0 TO Matches.Count - 1
				WITH Matches.Item(n)
					res = res + text(serial, .SubMatches(1), hour12) + .SubMatches(3)
				ENDWITH
			NEXT
			RESULT = res
		CASE format = "0/0"
			DIM separator = POS(".", serial)
			DIM g = 0
			IFB separator <> 0 THEN
				DIM keta = LENGTH(serial)
				DIM shift = POWER(10, keta - separator)
				IFB shift >= POWER(10, 15) THEN
					DIM position = 0
					FOR i = 0 TO 14
						IFB serial * POWER(10, i) - serial >= 1 THEN
							position = i
							BREAK
						ENDIF
					NEXT
					tmp = serial * POWER(10, position)
					FOR i = 1 TO 15
						r = (tmp * POWER(10, i)) / serial - (tmp / serial)
						a1 = tmp * POWER(10, i) - tmp
						IF a1 = INT(a1) THEN BREAK 
					NEXT
					DIM frac[] = a1, r
					g = GCD(frac)
					RESULT = (a1/g) + "/" + (r/g)
				ELSE
					DIM molecule = serial * shift	// 分子
					DIM denominator = shift		// 分母
					DIM nums[] = molecule, denominator
					g = GCD(nums)
					molecule = molecule / g
					denominator = denominator / g
					RESULT = molecule + "/" + denominator
				ENDIF
			ELSE
				RESULT = serial + "/1"
			ENDIF
		CASE reTest(format, "(0+)\.?(0+)?") AND UBound(SPLIT(format, ".")) <= 1 
			Matches = reExecute(format, "(0+)\.?(0+)?")
			len1 = LENGTH(Matches.Item(0).SubMatches(0))
			len2 = LENGTH(Matches.Item(0).SubMatches(1))
			DIM arr[] = LENGTH(INT(serial)), len1
			IFB POS(".", format) THEN
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX) + len2 + 1, len2), " ", "0")
			ELSE
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX)), " ", "0")
			ENDIF
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 時刻文字列。hh:nn:ss AM/PM、hh:nn AM/PM、hh AM/PM、hh:nn:ss、hh:nn、hh時nn分ss秒、hh時nn分のいずれかの形式を指定。 
// 【戻り値】
//   シリアル値 (例)0…00:00:00、0.5…12:00:00、0.999988425925926…23:59:59 
//////////////////////////////////////////////////
FUNCTION timeValue(str)
	DIM serial = 0
	DIM Matches
	DIM pattern = "(\d+)"
	DIM hh = "(0?[0-9]|1[0-2])"
	DIM ampm = "([AP]M|[ap]m)"
	SELECT TRUE
		CASE reTest(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(3)) + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(2)) + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + hh + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0) MOD 12) + IIF(reTest(.SubMatches(1), "AM|am"), 0, 12)
				serial = serial / 24
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		DEFAULT
			serial = ERR_VALUE
	SELEND
	RESULT = serial - INT(serial)
FEND

//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND
結果
プレーンテキスト
2021/05/29	2
2021/06/01	2
2021/06/02	5
2021/06/03	2
2021/06/04	16
0000/00/00	6
使用関数

Windowsの稼働時間を取得

Windowsシステムの起動時間から稼働時間を求めます。

結果はd:hh:nn:ssの形式で出力されます。

UWSC
HASHTBL INFO

DIM FSO = CREATEOLEOBJ("Scripting.FileSystemObject")

DIM filename = uniqueFilename(FSO.BuildPath(GET_CUR_DIR, "SYSTEMINFO.csv"))
DIM FID = FOPEN(filename, F_READ OR F_WRITE)
FPUT(FID, DOSCMD("systeminfo /FO CSV"))
FCLOSE(FID)

FID = FOPEN(filename, F_READ OR F_WRITE)
DIM col = 1

WHILE FGET(FID, 1, col) <> ""
	INFO[FGET(FID, 1, col)] = FGET(FID, 2, col)
	col = col + 1
WEND

FCLOSE(FID)

FSO.DeleteFile(filename)

DIM startupTime = INFO["システム起動時間"]
DIM date1 = dateValue(startupTime) + timeValue(startupTime)
DIM date2 = uwscToSerial(GETTIME())
PRINT INT(date2 - date1) + ":" + text(date2 - date1, "hh:mm:ss")

//////////////////////////////////////////////////
// 【引数】
//   needle : 検索する値 
//   haystack : 配列 
// 【戻り値】
//   needleが見つかった場合に配列のキー 
//////////////////////////////////////////////////
FUNCTION arraySearch(needle, haystack[])
	DIM i = 0
	FOR item IN haystack
		IFB item = needle THEN
			RESULT = i
			EXIT
		ENDIF
		i = i + 1
	NEXT
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 加算する時間間隔を表す文字列式(yyyy:年、m:月、d:日、ww:週、h:時、n:分、s:秒) 
//   num : dateに加算する値。未来は正、過去は負で指定 
//   date : 時間間隔を加算する日付 
// 【戻り値】
//   日時(date)に、指定した単位(interval)の時間(num)を加算して返します 
//////////////////////////////////////////////////
FUNCTION dateAdd(interval, num, date)
	DIM year, month, day, d
	GETTIME(0, date)
	DIM time = G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2
	SELECT interval
		CASE "yyyy"
			d = (G_TIME_YY + num) + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "m"
			IFB num > 0 THEN
				year = G_TIME_YY + INT((G_TIME_MM + num) / 12)
				month = REPLACE(FORMAT(((G_TIME_MM + num) MOD 12), 2), " ", "0")
			ELSE
				year = G_TIME_YY + CEIL((G_TIME_MM + num) / 12 - 1)
				month = REPLACE(FORMAT(G_TIME_MM - (ABS(num) MOD 12), 2), " ", "0")
			ENDIF
			IF month = "00" THEN month = 12
			day = G_TIME_DD2
			d = "" + year + month + day
			IFB !isDate(d) THEN
				d = year + "/" + month + "/" + "01"
				d = getEndOfMonth(d)
			ELSE
				d = year + "/" + month + "/" + day
			ENDIF
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "d"
			t = GETTIME(num, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "ww"
			t = GETTIME(num * 7, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "h"
			t = GETTIME(num / 24, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "n"
			t = GETTIME(num / 1440, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "s"
			t = GETTIME(num / 86400, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 時間単位(yyyy︰年、q:四半期、m︰月、d︰日、w:週日、ww:週、h:時、n:分、s:秒) 
//   date1 : 日時1 
//   date2 : 日時2 
// 【戻り値】
//   date2からdate1を引いた時間間隔を求めます。 
//////////////////////////////////////////////////
FUNCTION dateDiff(interval, date1, date2)
	DIM y1, y2, m1, m2, d1, d2, d
	SELECT interval
		CASE "yyyy"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			GETTIME(0, date2)
			y2 = G_TIME_YY
			d = y2 - y1
		CASE "q"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = y2 * 4 + CEIL(m2/3) - (y1 * 4 + CEIL(m1/3))
		CASE "m"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = (y2 - y1) * 12 + m2 - m1
		CASE "d"
			d1 = GETTIME(0, date1)
			d2 = GETTIME(0, date2)
			d = (d2 - d1) / 86400
		CASE "w"
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "ww"
			date1 = dateAdd("d", -1 * getWeekday(date1), date1)
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "h"
			d = dateDiff("d", date1, date2) * 24
		CASE "n"
			d = dateDiff("d", date1, date2) * 1440
		CASE "s"
			d = dateDiff("d", date1, date2) * 86400
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 日付文字列 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION dateValue(str)
	CONST cutOffYear = 2049
	DIM Months[] = "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
	
	HASHTBL Pattern
	Pattern["year"] = "(19[0-9]{2}|[2-9][0-9]{3})"
	Pattern["year2"] = "([0-9]{2})"
	Pattern["era"] = "(明治?|大正?|昭和?|平成?|令和?|[MTSHR])(元|\d+)"
	Pattern["month"] = "(1[0-2]|0?[1-9])"
	Pattern["month2"] = "(0[1-9]|1[0-2])"
	Pattern["month_en"] = "(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Ap(?:ril)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)"
	Pattern["day"] = "([1-2][0-9]|3[0-1]|0?[1-9])"
	Pattern["day2"] = "(0[1-9]|[1-2][0-9]|3[0-1])"
	Pattern["day_en"] = "(1(?:st)|2?2(?:nd)|2?3(?:rd)|[1-3]?[04-9](?:th)|1[1-3](?:th)|[1-3]1(?:st))"
	Pattern["separator"] = "([./\-])"
	
	HASHTBL era
	era["明治"] = 1867;	era["明"] = 1867;	era["M"] = 1867;
	era["大正"] = 1911;	era["大"] = 1911;	era["T"] = 1911;
	era["昭和"] = 1925;	era["昭"] = 1925;	era["S"] = 1925;
	era["平成"] = 1988;	era["平"] = 1988;	era["H"] = 1988;
	era["令和"] = 2018;	era["令"] = 2018;	era["R"] = 2018;

	SELECT TRUE
		CASE reTest(str, "\b" + Pattern["year"] + Pattern["separator"] + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
			Matches = reExecute(str, "\b(?:" + Pattern["year"] + Pattern["separator"] + ")?" + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + text(.SubMatches(2), "00") + "/" + text(IIF(.SubMatches(3)="", "01", .SubMatches(3)), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
			WITH Matches.Item(0)
				date = .SubMatches(3) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(3) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
			ENDWITH
		CASE reTest(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
			Matches = reExecute(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
			WITH Matches.Item(0)
				date = (era[.SubMatches(0)] + VAL(.SubMatches(1))) + "/" + text(.SubMatches(3), "00") + "/" + IIF(.SubMatches(4)="", "01", text(.SubMatches(4), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(0))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(3), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(3))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(3))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(0), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(1), "st", ""), "nd", ""), "rd", ""), "th", "")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(1), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(0), "st", ""), "nd", ""), "rd", ""), "th", "")
			ENDWITH
		CASE reTest(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			Matches = reExecute(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			WITH Matches.Item(0)
				date = era[.SubMatches(0)] + VAL(.SubMatches(1)) + "/" + text(.SubMatches(2), "00") + "/" + IIF(.SubMatches(3)="", "01", text(.SubMatches(3), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			Matches = reExecute(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + text(.SubMatches(1), "00") + "/" + IIF(.SubMatches(2)="", "01", text(.SubMatches(2), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
			Matches = reExecute(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
			WITH Matches.Item(0)
				date = getYear(today()) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(1), "00")
			ENDWITH
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
			Matches = reExecute(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(0))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
			ENDWITH
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
			Matches = reExecute(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(2))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
			ENDWITH					
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
			Matches = reExecute(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(2))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
			ENDWITH
		DEFAULT
			RESULT = ERR_VALUE
			EXIT
	SELEND
	RESULT = uwscToSerial(GETTIME(0, date))
FEND

//////////////////////////////////////////////////
// 【引数】
//   array : 最大公約数を求める数値を格納した配列 
// 【戻り値】
//   最大公約数 
//////////////////////////////////////////////////
FUNCTION GCD(array[])
	DIM c = LENGTH(array)
	DIM rem = array[c-1] MOD array[c-2]
	IFB rem = 0 THEN
		IFB LENGTH(array) = 2 THEN
			RESULT = array[c-2]
			EXIT
		ENDIF
		RESIZE(array, c-2)
		RESULT = GCD(array)
		EXIT
	ENDIF
	array[c-1] = array[c-2]
	array[c-2] = rem
	RESULT = GCD(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”) 
//   m : 第一引数の指定日からプラスマイナスm月とする 
// 【戻り値】
//   dateからm月後の月末の日付 
//////////////////////////////////////////////////
FUNCTION getEndOfMonth(date, m = 0)
	date = dateAdd("m", m + 1, date)
	GETTIME(0, date)
	GETTIME(-G_TIME_DD, date)
	RESULT = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付文字列(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”)もしくはシリアル値 
//   type : 取得する曜日番号の種類を示す0〜3または11〜17の値。1と17は日曜日を1、2と11は月曜日を1とカウントします。11以降はExcel2010で追加された値で、互換性を保つために重複した値があります。 
// 【戻り値】
//   typeで指定した種類によって以下の値を返します。 : (0 : 0(日曜)〜6(土曜)、1 : 1(日曜)~7(土曜)、2 : 1(月曜)~7(日曜)、3 : 0(月曜)〜6(日曜)、11 : 1(月曜)~7(日曜)、12 : 1(火曜)~7(月曜)、13 : 1(水曜)~7(火曜)、14 : 1(木曜)~7(水曜)、15 : 1(金曜)~7(木曜)、16 : 1(土曜)~7(金曜)、17 : 1(日曜)~7(土曜)) 
//////////////////////////////////////////////////
FUNCTION getWeekday(date, type = 1)
	IF VARTYPE(date) <> 258 THEN date = text(date, "yyyy/mm/dd")
	GETTIME(0, date)
	DIM w = G_TIME_WW
	SELECT TRUE
		CASE type = 0
			RESULT = w
		CASE type = 1
			RESULT = w + 1
		CASE type = 2
			RESULT = IIF(w=0, 7, w)
		CASE type = 3
			RESULT = (w+6) MOD 7
		CASE type >= 11
			RESULT = ((getWeekday(date, 2) + 17 - type) MOD 7) + 1
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”) 
// 【戻り値】
//   指定した日付の「年」 
//////////////////////////////////////////////////
FUNCTION getYear(date)
	GETTIME(0, date)
	RESULT = G_TIME_YY4
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値もしくは時刻文字列 
// 【戻り値】
//   時刻から時間を表す0〜23の範囲の値 
//////////////////////////////////////////////////
FUNCTION Hour(serial)
	IF VARTYPE(serial) = 258 THEN serial = timeValue(serial)
	RESULT = INT(serial * 24) MOD 24
FEND

//////////////////////////////////////////////////
// 【引数】
//   expr : 評価する式 
//   truepart : 評価した式がTrueのときに返す値 
//   falsepart : 評価した式がFalseのときに返す値 
// 【戻り値】
//   truepart : 評価した式がTrueのとき、falsepart : 評価した式がFalseのとき 
//////////////////////////////////////////////////
FUNCTION IIF(expr, truepart, falsepart)
	IFB EVAL(expr) THEN
		RESULT = truepart
	ELSE
		RESULT = falsepart
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 存在するかを調べる日付文字列。YYYYMMDD or YYYY/MM/DD or YYYY-MM-DDのいずれかの形式。 
// 【戻り値】
//   TRUE : 日付として認識できる、FALSE : 日付として認識できない 
//////////////////////////////////////////////////
FUNCTION isDate(date)
	TRY
		GETTIME(0, date)
		RESULT = TRUE
	EXCEPT
		RESULT = FALSE
	ENDTRY
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現で検索した結果をMatchesコレクションとして返します。 
//////////////////////////////////////////////////
FUNCTION reExecute(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Execute(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現にマッチするかどうかを示すブール値 
//////////////////////////////////////////////////
FUNCTION reTest(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Test(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   数値を表示書式に基づいて変換した文字列 
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   
//////////////////////////////////////////////////
FUNCTION text(serial, format, hour12 = FALSE)
	HASHTBL startDate
	startDate["明治"] = "1868/01/25"
	startDate["大正"] = "1912/07/30"
	startDate["昭和"] = "1926/12/25"
	startDate["平成"] = "1989/01/08"
	startDate["令和"] = "2019/05/01"
	
	DIM baseDate = "1899/12/30"
	serial = VAL(serial)
	SELECT TRUE
		CASE reTest(format, "\[h+\]")
			Matches = reExecute(format, "\[(h+)\]")
			DIM hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "^h+$")
			Matches = reExecute(format, "^(h+)$")
			hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour MOD 24, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "\[m+\]")
			Matches = reExecute(format, "\[(m+)\]")
			RESULT = text(serial * 1440, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE format = "m"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_MM, "0")
		CASE format = "mm"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_MM2
		CASE format = "n"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN
		CASE format = "nn"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN2
		CASE format = "s"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_SS, "0")
		CASE format = "ss"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_SS2
		CASE format = "yyyy"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_YY4
		CASE format = "yy"
			GETTIME(serial, baseDate)
			RESULT = COPY(G_TIME_YY4, 3, 2)
		CASE format = "e"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 2018
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1988
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1925
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1911
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1867
			SELEND
		CASE format = "ee"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 2018, "00")
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1988, "00")
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1925, "00")
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1911, "00")
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1867, "00")
			SELEND
		CASE format = "g"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "R"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "H"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "S"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "T"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "M"
			SELEND
		CASE format = "gg"
			RESULT = COPY(text(serial, "ggg"), 1, 1)
		CASE format = "ggg"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "令和"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "平成"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "昭和"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "大正"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "明治"
			SELEND
		CASE format = "mmmmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 1)
		CASE format = "mmmm"
			DIM month[] = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
			RESULT = month[text(serial, "m") - 1]
		CASE format = "mmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 3)
		CASE format = "dd"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD2, "00")
		CASE format = "d"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD, "0")
		CASE reTest(format, "^[ad]{3,4}$")
			Matches = reExecute(format, "([ad]{3,4})")
			GETTIME(serial, baseDate)
			DIM aaa[] = "日", "月", "火", "水", "木", "金", "土"
			DIM aaaa[] = "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"
			DIM ddd[] = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
			DIM dddd[] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday";
			RESULT = EVAL(Matches.Item(0).SubMatches(0) + "[" + getWeekday(G_TIME_WW, 1) + "]")
		CASE reTest(format, "(0+\.?0+)?%")
			Matches = reExecute(format, "(0+\.?0+)?%")
			RESULT = text(serial * 100, Matches.Item(0).SubMatches(0)) + "%"
		CASE reTest(format, "^\[DBNum\d{1,4}\](.*?)$")
			Matches = reExecute(format, "^\[DBNum(\d{1,4})\](.*?)$")
			DIM value = VAL(Matches.Item(0).SubMatches(0))
			DIM sss = text(serial, Matches.Item(0).SubMatches(1))
			Matches = reExecute(sss, "(\D+)?(\d+)(\D+)?")
			DIM res = ""
			FOR m = 0 TO Matches.Count - 1
				serial = Matches.Item(m).SubMatches(1)
				SELECT value
					CASE 1, 2
						DIM n[][9] = "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", + _
										"", "壱", "弐", "参", "四", "伍", "六", "七", "八", "九"
						DIM a[][3] = "", "十", "百", "千", + _
										"", "拾", "百", "阡"
						DIM b[][3] = "", "万", "億", "兆", + _
										"", "萬", "億", "兆"
						DIM r = ""
						DIM j = 0
						type = value - 1
						REPEAT
							DIM str = ""
							DIM n4 = serial MOD 10000
							FOR i = LENGTH(n4) TO 1 STEP -1
								s = COPY(n4, i, 1)
								IFB s = 1 AND a[type][LENGTH(n4)-i] <> "" THEN
									str = IIF(s, a[type][LENGTH(n4)-i], "") + str
								ELSE	
									str = n[type][s] + IIF(s, a[type][LENGTH(n4)-i], "") + str
								ENDIF
							NEXT
							IF str <> "" THEN r = str + b[type][j] + r
							j = j + 1
							serial = INT(serial / 10000)
						UNTIL serial = 0
						res = res + Matches.Item(m).SubMatches(0) + r + Matches.Item(m).SubMatches(2)
					CASE 3
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_FULLWIDTH) + Matches.Item(m).SubMatches(2)
					CASE 4
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_HALFWIDTH) + Matches.Item(m).SubMatches(2)
				SELEND
			NEXT
			RESULT = res
		CASE reTest(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			Matches = reExecute(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			DIM array = SPLIT(Matches.Item(0).SubMatches(1), "/")
			ampm = array[IIF(serial - INT(serial) >= 0.5, 1, 0)]
			hour12 = TRUE
			res = ""
			WITH Matches.Item(0)
				res = text(serial, .SubMatches(0), hour12) + ampm + text(serial, .SubMatches(2), hour12)
			ENDWITH
			RESULT = res
		CASE reTest(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			Matches = reExecute(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			FOR n = 0 TO Matches.Count - 1
				IF n = 0 THEN res = Matches.Item(n).SubMatches(0)
			NEXT
			FOR n = 0 TO Matches.Count - 1
				WITH Matches.Item(n)
					res = res + text(serial, .SubMatches(1), hour12) + .SubMatches(3)
				ENDWITH
			NEXT
			RESULT = res
		CASE format = "0/0"
			DIM separator = POS(".", serial)
			DIM g = 0
			IFB separator <> 0 THEN
				DIM keta = LENGTH(serial)
				DIM shift = POWER(10, keta - separator)
				IFB shift >= POWER(10, 15) THEN
					DIM position = 0
					FOR i = 0 TO 14
						IFB serial * POWER(10, i) - serial >= 1 THEN
							position = i
							BREAK
						ENDIF
					NEXT
					tmp = serial * POWER(10, position)
					FOR i = 1 TO 15
						r = (tmp * POWER(10, i)) / serial - (tmp / serial)
						a1 = tmp * POWER(10, i) - tmp
						IF a1 = INT(a1) THEN BREAK 
					NEXT
					DIM frac[] = a1, r
					g = GCD(frac)
					RESULT = (a1/g) + "/" + (r/g)
				ELSE
					DIM molecule = serial * shift	// 分子
					DIM denominator = shift		// 分母
					DIM nums[] = molecule, denominator
					g = GCD(nums)
					molecule = molecule / g
					denominator = denominator / g
					RESULT = molecule + "/" + denominator
				ENDIF
			ELSE
				RESULT = serial + "/1"
			ENDIF
		CASE reTest(format, "(0+)\.?(0+)?") AND UBound(SPLIT(format, ".")) <= 1 
			Matches = reExecute(format, "(0+)\.?(0+)?")
			len1 = LENGTH(Matches.Item(0).SubMatches(0))
			len2 = LENGTH(Matches.Item(0).SubMatches(1))
			DIM arr[] = LENGTH(INT(serial)), len1
			IFB POS(".", format) THEN
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX) + len2 + 1, len2), " ", "0")
			ELSE
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX)), " ", "0")
			ENDIF
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 時刻文字列。hh:nn:ss AM/PM、hh:nn AM/PM、hh AM/PM、hh:nn:ss、hh:nn、hh時nn分ss秒、hh時nn分のいずれかの形式を指定。 
// 【戻り値】
//   シリアル値 (例)0…00:00:00、0.5…12:00:00、0.999988425925926…23:59:59 
//////////////////////////////////////////////////
FUNCTION timeValue(str)
	DIM serial = 0
	DIM Matches
	DIM pattern = "(\d+)"
	DIM hh = "(0?[0-9]|1[0-2])"
	DIM ampm = "([AP]M|[ap]m)"
	SELECT TRUE
		CASE reTest(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(3)) + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(2)) + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + hh + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0) MOD 12) + IIF(reTest(.SubMatches(1), "AM|am"), 0, 12)
				serial = serial / 24
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		DEFAULT
			serial = ERR_VALUE
	SELEND
	RESULT = serial - INT(serial)
FEND

//////////////////////////////////////////////////
// 【引数】
//   
// 【戻り値】
//   今日の日付 
//////////////////////////////////////////////////
FUNCTION today()
	GETTIME()
	RESULT = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
FEND

//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND

//////////////////////////////////////////////////
// 【引数】
//   path : ファイルのパス 
// 【戻り値】
//   重複しないファイル名 
//////////////////////////////////////////////////
FUNCTION uniqueFilename(path)
	DIM FSO = CREATEOLEOBJ("Scripting.FileSystemObject")
	IFB FSO.FileExists(path) THEN
		DIM fol = FSO.GetParentFolderName(path)
		DIM filename = FSO.GetBaseName(path)
		DIM extension = FSO.GetExtensionName(path)
		DIM i = 2
		WHILE FSO.FileExists(FSO.BuildPath(fol, filename + " (" + i + ")." + extension))
			i = i + 1
		WEND
		RESULT = FSO.BuildPath(fol, filename + " (" + i + ")." + extension)
	ELSE
		RESULT = path
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   uwscTime : UWSC時間 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION uwscToSerial(uwscTime)
	uwscDate = uwscTime / 86400
	RESULT = 36526 + uwscDate
FEND
結果
プレーンテキスト
0:08:16:46
使用関数
解説

日付が指定した範囲内にあるか

今日の日付が2022/04/01〜2022/04/05の範囲内にあるか。

日付をシリアル値に変換し、2022/04/01以降かつ2022/04/05以前かを調べる。

UWSC
IFB dateValue("2022/04/01") <= dateValue(today()) AND dateValue(today()) <= dateValue("2022/04/05") THEN
	PRINT "範囲内です"
ELSE
	PRINT "範囲外です"
ENDIF

//////////////////////////////////////////////////
// 【引数】
//   needle : 検索する値 
//   haystack : 配列 
// 【戻り値】
//   needleが見つかった場合に配列のキー 
//////////////////////////////////////////////////
FUNCTION arraySearch(needle, haystack[])
	DIM i = 0
	FOR item IN haystack
		IFB item = needle THEN
			RESULT = i
			EXIT
		ENDIF
		i = i + 1
	NEXT
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 加算する時間間隔を表す文字列式(yyyy:年、m:月、d:日、ww:週、h:時、n:分、s:秒) 
//   num : dateに加算する値。未来は正、過去は負で指定 
//   date : 時間間隔を加算する日付 
// 【戻り値】
//   日時(date)に、指定した単位(interval)の時間(num)を加算して返します 
//////////////////////////////////////////////////
FUNCTION dateAdd(interval, num, date)
	DIM year, month, day, d
	GETTIME(0, date)
	DIM time = G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2
	SELECT interval
		CASE "yyyy"
			d = (G_TIME_YY + num) + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "m"
			IFB num > 0 THEN
				year = G_TIME_YY + INT((G_TIME_MM + num) / 12)
				month = REPLACE(FORMAT(((G_TIME_MM + num) MOD 12), 2), " ", "0")
			ELSE
				year = G_TIME_YY + CEIL((G_TIME_MM + num) / 12 - 1)
				month = REPLACE(FORMAT(G_TIME_MM - (ABS(num) MOD 12), 2), " ", "0")
			ENDIF
			IF month = "00" THEN month = 12
			day = G_TIME_DD2
			d = "" + year + month + day
			IFB !isDate(d) THEN
				d = year + "/" + month + "/" + "01"
				d = getEndOfMonth(d)
			ELSE
				d = year + "/" + month + "/" + day
			ENDIF
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "d"
			t = GETTIME(num, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "ww"
			t = GETTIME(num * 7, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "h"
			t = GETTIME(num / 24, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "n"
			t = GETTIME(num / 1440, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "s"
			t = GETTIME(num / 86400, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 時間単位(yyyy︰年、q:四半期、m︰月、d︰日、w:週日、ww:週、h:時、n:分、s:秒) 
//   date1 : 日時1 
//   date2 : 日時2 
// 【戻り値】
//   date2からdate1を引いた時間間隔を求めます。 
//////////////////////////////////////////////////
FUNCTION dateDiff(interval, date1, date2)
	DIM y1, y2, m1, m2, d1, d2, d
	SELECT interval
		CASE "yyyy"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			GETTIME(0, date2)
			y2 = G_TIME_YY
			d = y2 - y1
		CASE "q"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = y2 * 4 + CEIL(m2/3) - (y1 * 4 + CEIL(m1/3))
		CASE "m"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = (y2 - y1) * 12 + m2 - m1
		CASE "d"
			d1 = GETTIME(0, date1)
			d2 = GETTIME(0, date2)
			d = (d2 - d1) / 86400
		CASE "w"
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "ww"
			date1 = dateAdd("d", -1 * getWeekday(date1), date1)
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "h"
			d = dateDiff("d", date1, date2) * 24
		CASE "n"
			d = dateDiff("d", date1, date2) * 1440
		CASE "s"
			d = dateDiff("d", date1, date2) * 86400
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 日付文字列 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION dateValue(str)
	CONST cutOffYear = 2049
	DIM Months[] = "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
	
	HASHTBL Pattern
	Pattern["year"] = "(19[0-9]{2}|[2-9][0-9]{3})"
	Pattern["year2"] = "([0-9]{2})"
	Pattern["era"] = "(明治?|大正?|昭和?|平成?|令和?|[MTSHR])(元|\d+)"
	Pattern["month"] = "(1[0-2]|0?[1-9])"
	Pattern["month2"] = "(0[1-9]|1[0-2])"
	Pattern["month_en"] = "(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Ap(?:ril)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)"
	Pattern["day"] = "([1-2][0-9]|3[0-1]|0?[1-9])"
	Pattern["day2"] = "(0[1-9]|[1-2][0-9]|3[0-1])"
	Pattern["day_en"] = "(1(?:st)|2?2(?:nd)|2?3(?:rd)|[1-3]?[04-9](?:th)|1[1-3](?:th)|[1-3]1(?:st))"
	Pattern["separator"] = "([./\-])"
	
	HASHTBL era
	era["明治"] = 1867;	era["明"] = 1867;	era["M"] = 1867;
	era["大正"] = 1911;	era["大"] = 1911;	era["T"] = 1911;
	era["昭和"] = 1925;	era["昭"] = 1925;	era["S"] = 1925;
	era["平成"] = 1988;	era["平"] = 1988;	era["H"] = 1988;
	era["令和"] = 2018;	era["令"] = 2018;	era["R"] = 2018;

	SELECT TRUE
		CASE reTest(str, "\b" + Pattern["year"] + Pattern["separator"] + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
			Matches = reExecute(str, "\b(?:" + Pattern["year"] + Pattern["separator"] + ")?" + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + text(.SubMatches(2), "00") + "/" + text(IIF(.SubMatches(3)="", "01", .SubMatches(3)), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
			WITH Matches.Item(0)
				date = .SubMatches(3) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(3) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
			ENDWITH
		CASE reTest(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
			Matches = reExecute(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
			WITH Matches.Item(0)
				date = (era[.SubMatches(0)] + VAL(.SubMatches(1))) + "/" + text(.SubMatches(3), "00") + "/" + IIF(.SubMatches(4)="", "01", text(.SubMatches(4), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(0))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(3), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(3))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(3))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(0), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(1), "st", ""), "nd", ""), "rd", ""), "th", "")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(1), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(0), "st", ""), "nd", ""), "rd", ""), "th", "")
			ENDWITH
		CASE reTest(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			Matches = reExecute(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			WITH Matches.Item(0)
				date = era[.SubMatches(0)] + VAL(.SubMatches(1)) + "/" + text(.SubMatches(2), "00") + "/" + IIF(.SubMatches(3)="", "01", text(.SubMatches(3), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			Matches = reExecute(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + text(.SubMatches(1), "00") + "/" + IIF(.SubMatches(2)="", "01", text(.SubMatches(2), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
			Matches = reExecute(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
			WITH Matches.Item(0)
				date = getYear(today()) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(1), "00")
			ENDWITH
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
			Matches = reExecute(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(0))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
			ENDWITH
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
			Matches = reExecute(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(2))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
			ENDWITH					
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
			Matches = reExecute(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(2))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
			ENDWITH
		DEFAULT
			RESULT = ERR_VALUE
			EXIT
	SELEND
	RESULT = uwscToSerial(GETTIME(0, date))
FEND

//////////////////////////////////////////////////
// 【引数】
//   array : 最大公約数を求める数値を格納した配列 
// 【戻り値】
//   最大公約数 
//////////////////////////////////////////////////
FUNCTION GCD(array[])
	DIM c = LENGTH(array)
	DIM rem = array[c-1] MOD array[c-2]
	IFB rem = 0 THEN
		IFB LENGTH(array) = 2 THEN
			RESULT = array[c-2]
			EXIT
		ENDIF
		RESIZE(array, c-2)
		RESULT = GCD(array)
		EXIT
	ENDIF
	array[c-1] = array[c-2]
	array[c-2] = rem
	RESULT = GCD(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”) 
//   m : 第一引数の指定日からプラスマイナスm月とする 
// 【戻り値】
//   dateからm月後の月末の日付 
//////////////////////////////////////////////////
FUNCTION getEndOfMonth(date, m = 0)
	date = dateAdd("m", m + 1, date)
	GETTIME(0, date)
	GETTIME(-G_TIME_DD, date)
	RESULT = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付文字列(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”)もしくはシリアル値 
//   type : 取得する曜日番号の種類を示す0〜3または11〜17の値。1と17は日曜日を1、2と11は月曜日を1とカウントします。11以降はExcel2010で追加された値で、互換性を保つために重複した値があります。 
// 【戻り値】
//   typeで指定した種類によって以下の値を返します。 : (0 : 0(日曜)〜6(土曜)、1 : 1(日曜)~7(土曜)、2 : 1(月曜)~7(日曜)、3 : 0(月曜)〜6(日曜)、11 : 1(月曜)~7(日曜)、12 : 1(火曜)~7(月曜)、13 : 1(水曜)~7(火曜)、14 : 1(木曜)~7(水曜)、15 : 1(金曜)~7(木曜)、16 : 1(土曜)~7(金曜)、17 : 1(日曜)~7(土曜)) 
//////////////////////////////////////////////////
FUNCTION getWeekday(date, type = 1)
	IF VARTYPE(date) <> 258 THEN date = text(date, "yyyy/mm/dd")
	GETTIME(0, date)
	DIM w = G_TIME_WW
	SELECT TRUE
		CASE type = 0
			RESULT = w
		CASE type = 1
			RESULT = w + 1
		CASE type = 2
			RESULT = IIF(w=0, 7, w)
		CASE type = 3
			RESULT = (w+6) MOD 7
		CASE type >= 11
			RESULT = ((getWeekday(date, 2) + 17 - type) MOD 7) + 1
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”) 
// 【戻り値】
//   指定した日付の「年」 
//////////////////////////////////////////////////
FUNCTION getYear(date)
	GETTIME(0, date)
	RESULT = G_TIME_YY4
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値もしくは時刻文字列 
// 【戻り値】
//   時刻から時間を表す0〜23の範囲の値 
//////////////////////////////////////////////////
FUNCTION Hour(serial)
	IF VARTYPE(serial) = 258 THEN serial = timeValue(serial)
	RESULT = INT(serial * 24) MOD 24
FEND

//////////////////////////////////////////////////
// 【引数】
//   expr : 評価する式 
//   truepart : 評価した式がTrueのときに返す値 
//   falsepart : 評価した式がFalseのときに返す値 
// 【戻り値】
//   truepart : 評価した式がTrueのとき、falsepart : 評価した式がFalseのとき 
//////////////////////////////////////////////////
FUNCTION IIF(expr, truepart, falsepart)
	IFB EVAL(expr) THEN
		RESULT = truepart
	ELSE
		RESULT = falsepart
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 存在するかを調べる日付文字列。YYYYMMDD or YYYY/MM/DD or YYYY-MM-DDのいずれかの形式。 
// 【戻り値】
//   TRUE : 日付として認識できる、FALSE : 日付として認識できない 
//////////////////////////////////////////////////
FUNCTION isDate(date)
	TRY
		GETTIME(0, date)
		RESULT = TRUE
	EXCEPT
		RESULT = FALSE
	ENDTRY
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現で検索した結果をMatchesコレクションとして返します。 
//////////////////////////////////////////////////
FUNCTION reExecute(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Execute(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現にマッチするかどうかを示すブール値 
//////////////////////////////////////////////////
FUNCTION reTest(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Test(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   数値を表示書式に基づいて変換した文字列 
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   
//////////////////////////////////////////////////
FUNCTION text(serial, format, hour12 = FALSE)
	HASHTBL startDate
	startDate["明治"] = "1868/01/25"
	startDate["大正"] = "1912/07/30"
	startDate["昭和"] = "1926/12/25"
	startDate["平成"] = "1989/01/08"
	startDate["令和"] = "2019/05/01"
	
	DIM baseDate = "1899/12/30"
	serial = VAL(serial)
	SELECT TRUE
		CASE reTest(format, "\[h+\]")
			Matches = reExecute(format, "\[(h+)\]")
			DIM hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "^h+$")
			Matches = reExecute(format, "^(h+)$")
			hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour MOD 24, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "\[m+\]")
			Matches = reExecute(format, "\[(m+)\]")
			RESULT = text(serial * 1440, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE format = "m"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_MM, "0")
		CASE format = "mm"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_MM2
		CASE format = "n"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN
		CASE format = "nn"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN2
		CASE format = "s"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_SS, "0")
		CASE format = "ss"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_SS2
		CASE format = "yyyy"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_YY4
		CASE format = "yy"
			GETTIME(serial, baseDate)
			RESULT = COPY(G_TIME_YY4, 3, 2)
		CASE format = "e"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 2018
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1988
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1925
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1911
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1867
			SELEND
		CASE format = "ee"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 2018, "00")
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1988, "00")
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1925, "00")
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1911, "00")
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1867, "00")
			SELEND
		CASE format = "g"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "R"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "H"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "S"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "T"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "M"
			SELEND
		CASE format = "gg"
			RESULT = COPY(text(serial, "ggg"), 1, 1)
		CASE format = "ggg"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "令和"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "平成"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "昭和"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "大正"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "明治"
			SELEND
		CASE format = "mmmmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 1)
		CASE format = "mmmm"
			DIM month[] = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
			RESULT = month[text(serial, "m") - 1]
		CASE format = "mmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 3)
		CASE format = "dd"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD2, "00")
		CASE format = "d"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD, "0")
		CASE reTest(format, "^[ad]{3,4}$")
			Matches = reExecute(format, "([ad]{3,4})")
			GETTIME(serial, baseDate)
			DIM aaa[] = "日", "月", "火", "水", "木", "金", "土"
			DIM aaaa[] = "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"
			DIM ddd[] = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
			DIM dddd[] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday";
			RESULT = EVAL(Matches.Item(0).SubMatches(0) + "[" + getWeekday(G_TIME_WW, 1) + "]")
		CASE reTest(format, "(0+\.?0+)?%")
			Matches = reExecute(format, "(0+\.?0+)?%")
			RESULT = text(serial * 100, Matches.Item(0).SubMatches(0)) + "%"
		CASE reTest(format, "^\[DBNum\d{1,4}\](.*?)$")
			Matches = reExecute(format, "^\[DBNum(\d{1,4})\](.*?)$")
			DIM value = VAL(Matches.Item(0).SubMatches(0))
			DIM sss = text(serial, Matches.Item(0).SubMatches(1))
			Matches = reExecute(sss, "(\D+)?(\d+)(\D+)?")
			DIM res = ""
			FOR m = 0 TO Matches.Count - 1
				serial = Matches.Item(m).SubMatches(1)
				SELECT value
					CASE 1, 2
						DIM n[][9] = "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", + _
										"", "壱", "弐", "参", "四", "伍", "六", "七", "八", "九"
						DIM a[][3] = "", "十", "百", "千", + _
										"", "拾", "百", "阡"
						DIM b[][3] = "", "万", "億", "兆", + _
										"", "萬", "億", "兆"
						DIM r = ""
						DIM j = 0
						type = value - 1
						REPEAT
							DIM str = ""
							DIM n4 = serial MOD 10000
							FOR i = LENGTH(n4) TO 1 STEP -1
								s = COPY(n4, i, 1)
								IFB s = 1 AND a[type][LENGTH(n4)-i] <> "" THEN
									str = IIF(s, a[type][LENGTH(n4)-i], "") + str
								ELSE	
									str = n[type][s] + IIF(s, a[type][LENGTH(n4)-i], "") + str
								ENDIF
							NEXT
							IF str <> "" THEN r = str + b[type][j] + r
							j = j + 1
							serial = INT(serial / 10000)
						UNTIL serial = 0
						res = res + Matches.Item(m).SubMatches(0) + r + Matches.Item(m).SubMatches(2)
					CASE 3
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_FULLWIDTH) + Matches.Item(m).SubMatches(2)
					CASE 4
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_HALFWIDTH) + Matches.Item(m).SubMatches(2)
				SELEND
			NEXT
			RESULT = res
		CASE reTest(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			Matches = reExecute(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			DIM array = SPLIT(Matches.Item(0).SubMatches(1), "/")
			ampm = array[IIF(serial - INT(serial) >= 0.5, 1, 0)]
			hour12 = TRUE
			res = ""
			WITH Matches.Item(0)
				res = text(serial, .SubMatches(0), hour12) + ampm + text(serial, .SubMatches(2), hour12)
			ENDWITH
			RESULT = res
		CASE reTest(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			Matches = reExecute(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			FOR n = 0 TO Matches.Count - 1
				IF n = 0 THEN res = Matches.Item(n).SubMatches(0)
			NEXT
			FOR n = 0 TO Matches.Count - 1
				WITH Matches.Item(n)
					res = res + text(serial, .SubMatches(1), hour12) + .SubMatches(3)
				ENDWITH
			NEXT
			RESULT = res
		CASE format = "0/0"
			DIM separator = POS(".", serial)
			DIM g = 0
			IFB separator <> 0 THEN
				DIM keta = LENGTH(serial)
				DIM shift = POWER(10, keta - separator)
				IFB shift >= POWER(10, 15) THEN
					DIM position = 0
					FOR i = 0 TO 14
						IFB serial * POWER(10, i) - serial >= 1 THEN
							position = i
							BREAK
						ENDIF
					NEXT
					tmp = serial * POWER(10, position)
					FOR i = 1 TO 15
						r = (tmp * POWER(10, i)) / serial - (tmp / serial)
						a1 = tmp * POWER(10, i) - tmp
						IF a1 = INT(a1) THEN BREAK 
					NEXT
					DIM frac[] = a1, r
					g = GCD(frac)
					RESULT = (a1/g) + "/" + (r/g)
				ELSE
					DIM molecule = serial * shift	// 分子
					DIM denominator = shift		// 分母
					DIM nums[] = molecule, denominator
					g = GCD(nums)
					molecule = molecule / g
					denominator = denominator / g
					RESULT = molecule + "/" + denominator
				ENDIF
			ELSE
				RESULT = serial + "/1"
			ENDIF
		CASE reTest(format, "(0+)\.?(0+)?") AND UBound(SPLIT(format, ".")) <= 1 
			Matches = reExecute(format, "(0+)\.?(0+)?")
			len1 = LENGTH(Matches.Item(0).SubMatches(0))
			len2 = LENGTH(Matches.Item(0).SubMatches(1))
			DIM arr[] = LENGTH(INT(serial)), len1
			IFB POS(".", format) THEN
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX) + len2 + 1, len2), " ", "0")
			ELSE
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX)), " ", "0")
			ENDIF
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 時刻文字列。hh:nn:ss AM/PM、hh:nn AM/PM、hh AM/PM、hh:nn:ss、hh:nn、hh時nn分ss秒、hh時nn分のいずれかの形式を指定。 
// 【戻り値】
//   シリアル値 (例)0…00:00:00、0.5…12:00:00、0.999988425925926…23:59:59 
//////////////////////////////////////////////////
FUNCTION timeValue(str)
	DIM serial = 0
	DIM Matches
	DIM pattern = "(\d+)"
	DIM hh = "(0?[0-9]|1[0-2])"
	DIM ampm = "([AP]M|[ap]m)"
	SELECT TRUE
		CASE reTest(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(3)) + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(2)) + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + hh + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0) MOD 12) + IIF(reTest(.SubMatches(1), "AM|am"), 0, 12)
				serial = serial / 24
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		DEFAULT
			serial = ERR_VALUE
	SELEND
	RESULT = serial - INT(serial)
FEND

//////////////////////////////////////////////////
// 【引数】
//   
// 【戻り値】
//   今日の日付 
//////////////////////////////////////////////////
FUNCTION today()
	GETTIME()
	RESULT = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
FEND

//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND

//////////////////////////////////////////////////
// 【引数】
//   uwscTime : UWSC時間 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION uwscToSerial(uwscTime)
	uwscDate = uwscTime / 86400
	RESULT = 36526 + uwscDate
FEND
使用関数

フォルダ内のファイルを更新日時でソートする

UWSC
DIM Shell = CREATEOLEOBJ("Shell.Application")
DIM Folder = Shell.NameSpace("C:\Program Files (x86)\UWSC")
DIM FolderItems = Folder.Items

HASHTBL iColumn

FOR i = 0 TO 350
	iColumn[Folder.GetDetailsOf(EMPTYPARAM, i)] = i
NEXT

DIM filename[-1]
DIM datetime[-1]

FOR i = 0 TO FolderItems.Count - 1
	DIM FolderItem = FolderItems.Item(i)
	IF FolderItem.isFolder THEN CONTINUE
	arrayPush(filename, Folder.GetDetailsOf(FolderItem,iColumn["名前"]))
	arrayPush(datetime, Folder.GetDetailsOf(FolderItem, iColumn["更新日時"]))
NEXT

QSORT(datetime, QSRT_NATURALA, filename)

FOR i = 0 TO UBound(filename)
	PRINT filename[i] + "<#TAB>" + datetime[i]
NEXT

//////////////////////////////////////////////////
// 【引数】
//   array : 要素を追加する配列(参照引数) 
//   values : 追加する要素をvalue1から指定 
// 【戻り値】
//   処理後の配列の要素の数 
//////////////////////////////////////////////////
FUNCTION arrayPush(var array[], value1 = EMPTY, value2 = EMPTY, value3 = EMPTY, value4 = EMPTY, value5 = EMPTY, value6 = EMPTY, value7 = EMPTY, value8 = EMPTY, value9 = EMPTY, value10 = EMPTY, value11 = EMPTY, value12 = EMPTY, value13 = EMPTY, value14 = EMPTY, value15 = EMPTY, value16 = EMPTY)
    DIM i = 1
    WHILE EVAL("value" + i) <> EMPTY
	  DIM res = RESIZE(array, UBound(array) + 1)
	  array[res] = EVAL("value" + i)
	  i = i + 1
	WEND
	RESULT = LENGTH(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND
結果
プレーンテキスト
UDebug.chm	2014/05/11 1:58
RecIE.chm	2014/05/11 1:58
Uws2Exe.chm	2015/08/13 16:12
RecUws.dll	2016/03/25 4:57
UWSC.exe	2016/10/15 13:33
Uws2Exe.exe	2016/10/15 13:34
RecIE.exe	2016/10/15 13:35
UDebug.exe	2016/10/15 13:37
XRef.exe	2016/10/15 13:38
Readme.txt	2016/10/15 13:39
uwsc.chm	2016/10/15 13:41
unins000.exe	2018/06/27 12:48
unins000.dat	2018/06/27 12:49
使用関数

写真を撮影日時毎にフォルダ分けする

UWSC
CONST ssfMYPICTURES = 39

DIM Folder[1]
DIM folderspec[1]

DIM Shell = CREATEOLEOBJ("Shell.Application")
folderspec[0] = "D:\Pictures\100ANDRO\"
Folder[0] = Shell.NameSpace(folderspec[0])

DIM FSO = CREATEOLEOBJ("Scripting.FileSystemObject")

HASHTBL iColumn

FOR i = 0 TO 350
	iColumn[Folder[0].GetDetailsOf(EMPTYPARAM, i)] = i
NEXT

DIM destination[-1]
DIM path[-1]

DIM cnt = Folder[0].Items.Count - 1

FOR i = cnt TO 0 STEP -1
	FUKIDASI(i + "/" + cnt)
	DIM FolderItem = Folder[0].Items.Item(i)
	IF FolderItem.isFolder THEN CONTINUE
	DIM filename = FolderItem.Name
	DIM datetime = deleteControlCharacter(Folder[0].GetDetailsOf(FolderItem, iColumn["撮影日時"]))
	PRINT filename
	PRINT datetime
	PRINT "-----"
	TRY
		DIM date = text(dateValue(datetime), "yyyy-mm-dd")
	EXCEPT
		date = "0000-00-00"
	ENDTRY
	folderspec[1] = FSO.BuildPath(folderspec[0], date)
	Folder[1] = Shell.NameSpace(folderspec[1])
	
	arrayPush(destination, folderspec[1])
	arrayPush(path, FolderItem.Path)
NEXT

FOR i = 0 TO UBound(destination)
	IF !FSO.FolderExists(destination[i]) THEN CreateFolders(destination[i])
	Folders = Shell.NameSpace(destination[i])
	Folders.MoveHere(path[i])
NEXT

FUNCTION deleteControlCharacter(str)
	DIM array[] = CHR(8206), CHR(8207)
	FOR item IN array
		str = REPLACE(str, item, "")
	NEXT
	RESULT = str
FEND

//////////////////////////////////////////////////
// 【引数】
//   array : 要素を追加する配列(参照引数) 
//   values : 追加する要素をvalue1から指定 
// 【戻り値】
//   処理後の配列の要素の数 
//////////////////////////////////////////////////
FUNCTION arrayPush(var array[], value1 = EMPTY, value2 = EMPTY, value3 = EMPTY, value4 = EMPTY, value5 = EMPTY, value6 = EMPTY, value7 = EMPTY, value8 = EMPTY, value9 = EMPTY, value10 = EMPTY, value11 = EMPTY, value12 = EMPTY, value13 = EMPTY, value14 = EMPTY, value15 = EMPTY, value16 = EMPTY)
    DIM i = 1
    WHILE EVAL("value" + i) <> EMPTY
	  DIM res = RESIZE(array, UBound(array) + 1)
	  array[res] = EVAL("value" + i)
	  i = i + 1
	WEND
	RESULT = LENGTH(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   needle : 検索する値 
//   haystack : 配列 
// 【戻り値】
//   needleが見つかった場合に配列のキー 
//////////////////////////////////////////////////
FUNCTION arraySearch(needle, haystack[])
	DIM i = 0
	FOR item IN haystack
		IFB item = needle THEN
			RESULT = i
			EXIT
		ENDIF
		i = i + 1
	NEXT
FEND

//////////////////////////////////////////////////
// 【引数】
//   folderspec : 作成するフォルダのパス 
// 【戻り値】
//   
//////////////////////////////////////////////////
PROCEDURE CreateFolders(folderspec)
	WITH CREATEOLEOBJ("Scripting.FileSystemObject")
		folderspec = .GetAbsolutePathName(folderspec)
		IF !.DriveExists(.GetDriveName(folderspec)) THEN EXIT
		DIM parentPath = .GetParentFolderName(folderspec)
		IF !.FolderExists(parentPath) THEN CreateFolders(parentPath)
		IF !.FolderExists(folderspec) THEN .CreateFolder(folderspec)
	ENDWITH
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 加算する時間間隔を表す文字列式(yyyy:年、m:月、d:日、ww:週、h:時、n:分、s:秒) 
//   num : dateに加算する値。未来は正、過去は負で指定 
//   date : 時間間隔を加算する日付 
// 【戻り値】
//   日時(date)に、指定した単位(interval)の時間(num)を加算して返します 
//////////////////////////////////////////////////
FUNCTION dateAdd(interval, num, date)
	DIM year, month, day, d
	GETTIME(0, date)
	DIM time = G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2
	SELECT interval
		CASE "yyyy"
			d = (G_TIME_YY + num) + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "m"
			IFB num > 0 THEN
				year = G_TIME_YY + INT((G_TIME_MM + num) / 12)
				month = REPLACE(FORMAT(((G_TIME_MM + num) MOD 12), 2), " ", "0")
			ELSE
				year = G_TIME_YY + CEIL((G_TIME_MM + num) / 12 - 1)
				month = REPLACE(FORMAT(G_TIME_MM - (ABS(num) MOD 12), 2), " ", "0")
			ENDIF
			IF month = "00" THEN month = 12
			day = G_TIME_DD2
			d = "" + year + month + day
			IFB !isDate(d) THEN
				d = year + "/" + month + "/" + "01"
				d = getEndOfMonth(d)
			ELSE
				d = year + "/" + month + "/" + day
			ENDIF
			IF time <> "00:00:00" THEN d = d + " " + time
		CASE "d"
			t = GETTIME(num, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "ww"
			t = GETTIME(num * 7, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "h"
			t = GETTIME(num / 24, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "n"
			t = GETTIME(num / 1440, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
		CASE "s"
			t = GETTIME(num / 86400, date)
			d = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2 + IIF(t MOD 86400, " " + G_TIME_HH2 + ":" + G_TIME_NN2 + ":" + G_TIME_SS2, "")
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   interval : 時間単位(yyyy︰年、q:四半期、m︰月、d︰日、w:週日、ww:週、h:時、n:分、s:秒) 
//   date1 : 日時1 
//   date2 : 日時2 
// 【戻り値】
//   date2からdate1を引いた時間間隔を求めます。 
//////////////////////////////////////////////////
FUNCTION dateDiff(interval, date1, date2)
	DIM y1, y2, m1, m2, d1, d2, d
	SELECT interval
		CASE "yyyy"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			GETTIME(0, date2)
			y2 = G_TIME_YY
			d = y2 - y1
		CASE "q"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = y2 * 4 + CEIL(m2/3) - (y1 * 4 + CEIL(m1/3))
		CASE "m"
			GETTIME(0, date1)
			y1 = G_TIME_YY
			m1 = G_TIME_MM
			GETTIME(0, date2)
			y2 = G_TIME_YY
			m2 = G_TIME_MM
			d = (y2 - y1) * 12 + m2 - m1
		CASE "d"
			d1 = GETTIME(0, date1)
			d2 = GETTIME(0, date2)
			d = (d2 - d1) / 86400
		CASE "w"
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "ww"
			date1 = dateAdd("d", -1 * getWeekday(date1), date1)
			d = INT(dateDiff("d", date1, date2) / 7)
		CASE "h"
			d = dateDiff("d", date1, date2) * 24
		CASE "n"
			d = dateDiff("d", date1, date2) * 1440
		CASE "s"
			d = dateDiff("d", date1, date2) * 86400
	SELEND
	RESULT = d
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 日付文字列 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION dateValue(str)
	CONST cutOffYear = 2049
	DIM Months[] = "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
	
	HASHTBL Pattern
	Pattern["year"] = "(19[0-9]{2}|[2-9][0-9]{3})"
	Pattern["year2"] = "([0-9]{2})"
	Pattern["era"] = "(明治?|大正?|昭和?|平成?|令和?|[MTSHR])(元|\d+)"
	Pattern["month"] = "(1[0-2]|0?[1-9])"
	Pattern["month2"] = "(0[1-9]|1[0-2])"
	Pattern["month_en"] = "(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Ap(?:ril)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)"
	Pattern["day"] = "([1-2][0-9]|3[0-1]|0?[1-9])"
	Pattern["day2"] = "(0[1-9]|[1-2][0-9]|3[0-1])"
	Pattern["day_en"] = "(1(?:st)|2?2(?:nd)|2?3(?:rd)|[1-3]?[04-9](?:th)|1[1-3](?:th)|[1-3]1(?:st))"
	Pattern["separator"] = "([./\-])"
	
	HASHTBL era
	era["明治"] = 1867;	era["明"] = 1867;	era["M"] = 1867;
	era["大正"] = 1911;	era["大"] = 1911;	era["T"] = 1911;
	era["昭和"] = 1925;	era["昭"] = 1925;	era["S"] = 1925;
	era["平成"] = 1988;	era["平"] = 1988;	era["H"] = 1988;
	era["令和"] = 2018;	era["令"] = 2018;	era["R"] = 2018;

	SELECT TRUE
		CASE reTest(str, "\b" + Pattern["year"] + Pattern["separator"] + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
			Matches = reExecute(str, "\b(?:" + Pattern["year"] + Pattern["separator"] + ")?" + Pattern["month"] + "(?:\2" + Pattern["day"] + ")?\b")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + text(.SubMatches(2), "00") + "/" + text(IIF(.SubMatches(3)="", "01", .SubMatches(3)), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "(?:\2" + Pattern["year"] + ")?\b")
			WITH Matches.Item(0)
				date = .SubMatches(3) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(3) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
			ENDWITH
		CASE reTest(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
			Matches = reExecute(str, Pattern["era"] + Pattern["separator"] + Pattern["month"] + "(?:\3" + Pattern["day"] + ")?")
			WITH Matches.Item(0)
				date = (era[.SubMatches(0)] + VAL(.SubMatches(1))) + "/" + text(.SubMatches(3), "00") + "/" + IIF(.SubMatches(4)="", "01", text(.SubMatches(4), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["year2"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["day"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(0))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(3), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["separator"] + Pattern["day"] + "\2" + Pattern["year2"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(3))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(2), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["separator"] + Pattern["month"] + "\2" + Pattern["year2"] + "\b")
			WITH Matches.Item(0)
				y = VAL(.SubMatches(3))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + text(.SubMatches(2), "00") + "/" + text(.SubMatches(0), "00")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month_en"] + " " + Pattern["day_en"] + ",\s*" + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(0), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(1), "st", ""), "nd", ""), "rd", ""), "th", "")
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day_en"] + " " + Pattern["month_en"] + " " + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + text(VAL(arraySearch(COPY(.SubMatches(1), 1, 3), Months)) + 1, "00") + "/" + REPLACE(REPLACE(REPLACE(REPLACE(.SubMatches(0), "st", ""), "nd", ""), "rd", ""), "th", "")
			ENDWITH
		CASE reTest(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			Matches = reExecute(str, Pattern["era"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			WITH Matches.Item(0)
				date = era[.SubMatches(0)] + VAL(.SubMatches(1)) + "/" + text(.SubMatches(2), "00") + "/" + IIF(.SubMatches(3)="", "01", text(.SubMatches(3), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			Matches = reExecute(str, "\b" + Pattern["year"] + "年" + Pattern["month"] + "月(?:" + Pattern["day"] + "日)?")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + text(.SubMatches(1), "00") + "/" + IIF(.SubMatches(2)="", "01", text(.SubMatches(2), "00"))
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
			Matches = reExecute(str, "\b" + Pattern["month"] + "月" + Pattern["day"] + "日")
			WITH Matches.Item(0)
				date = getYear(today()) + "/" + text(.SubMatches(0), "00") + "/" + text(.SubMatches(1), "00")
			ENDWITH
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
			Matches = reExecute(str, Pattern["year2"] + Pattern["month2"] + Pattern["day2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(0))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
			ENDWITH
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
			Matches = reExecute(str, Pattern["month2"] + Pattern["day2"] + Pattern["year2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(2))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
			ENDWITH					
		CASE LENGTH(str) = 6 AND reTest(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
			Matches = reExecute(str, Pattern["day2"] + Pattern["month2"] + Pattern["year2"])
			WITH Matches.Item(0)
				y = VAL(.SubMatches(2))
				date = (IIF(y <= cutOffYear MOD 100, INT(cutOffYear/100) * 100, INT(cutOffYear/100) * 100 - 100) + y) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["year"] + Pattern["month"] + Pattern["day"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(0) + "/" + .SubMatches(1) + "/" + .SubMatches(2)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["month"] + Pattern["day"] + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + .SubMatches(0) + "/" + .SubMatches(1)
			ENDWITH
		CASE reTest(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
			Matches = reExecute(str, "\b" + Pattern["day"] + Pattern["month"] + Pattern["year"] + "\b")
			WITH Matches.Item(0)
				date = .SubMatches(2) + "/" + .SubMatches(1) + "/" + .SubMatches(0)
			ENDWITH
		DEFAULT
			RESULT = ERR_VALUE
			EXIT
	SELEND
	RESULT = uwscToSerial(GETTIME(0, date))
FEND

//////////////////////////////////////////////////
// 【引数】
//   array : 最大公約数を求める数値を格納した配列 
// 【戻り値】
//   最大公約数 
//////////////////////////////////////////////////
FUNCTION GCD(array[])
	DIM c = LENGTH(array)
	DIM rem = array[c-1] MOD array[c-2]
	IFB rem = 0 THEN
		IFB LENGTH(array) = 2 THEN
			RESULT = array[c-2]
			EXIT
		ENDIF
		RESIZE(array, c-2)
		RESULT = GCD(array)
		EXIT
	ENDIF
	array[c-1] = array[c-2]
	array[c-2] = rem
	RESULT = GCD(array)
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”) 
//   m : 第一引数の指定日からプラスマイナスm月とする 
// 【戻り値】
//   dateからm月後の月末の日付 
//////////////////////////////////////////////////
FUNCTION getEndOfMonth(date, m = 0)
	date = dateAdd("m", m + 1, date)
	GETTIME(0, date)
	GETTIME(-G_TIME_DD, date)
	RESULT = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付文字列(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”)もしくはシリアル値 
//   type : 取得する曜日番号の種類を示す0〜3または11〜17の値。1と17は日曜日を1、2と11は月曜日を1とカウントします。11以降はExcel2010で追加された値で、互換性を保つために重複した値があります。 
// 【戻り値】
//   typeで指定した種類によって以下の値を返します。 : (0 : 0(日曜)〜6(土曜)、1 : 1(日曜)~7(土曜)、2 : 1(月曜)~7(日曜)、3 : 0(月曜)〜6(日曜)、11 : 1(月曜)~7(日曜)、12 : 1(火曜)~7(月曜)、13 : 1(水曜)~7(火曜)、14 : 1(木曜)~7(水曜)、15 : 1(金曜)~7(木曜)、16 : 1(土曜)~7(金曜)、17 : 1(日曜)~7(土曜)) 
//////////////////////////////////////////////////
FUNCTION getWeekday(date, type = 1)
	IF VARTYPE(date) <> 258 THEN date = text(date, "yyyy/mm/dd")
	GETTIME(0, date)
	DIM w = G_TIME_WW
	SELECT TRUE
		CASE type = 0
			RESULT = w
		CASE type = 1
			RESULT = w + 1
		CASE type = 2
			RESULT = IIF(w=0, 7, w)
		CASE type = 3
			RESULT = (w+6) MOD 7
		CASE type >= 11
			RESULT = ((getWeekday(date, 2) + 17 - type) MOD 7) + 1
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 日付(”YYYYMMDD” or “YYYY/MM/DD” or “YYYY-MM-DD” or “YYYYMMDDHHNNSS” or “YYYY/MM/DD HH:NN:SS”) 
// 【戻り値】
//   指定した日付の「年」 
//////////////////////////////////////////////////
FUNCTION getYear(date)
	GETTIME(0, date)
	RESULT = G_TIME_YY4
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値もしくは時刻文字列 
// 【戻り値】
//   時刻から時間を表す0〜23の範囲の値 
//////////////////////////////////////////////////
FUNCTION Hour(serial)
	IF VARTYPE(serial) = 258 THEN serial = timeValue(serial)
	RESULT = INT(serial * 24) MOD 24
FEND

//////////////////////////////////////////////////
// 【引数】
//   expr : 評価する式 
//   truepart : 評価した式がTrueのときに返す値 
//   falsepart : 評価した式がFalseのときに返す値 
// 【戻り値】
//   truepart : 評価した式がTrueのとき、falsepart : 評価した式がFalseのとき 
//////////////////////////////////////////////////
FUNCTION IIF(expr, truepart, falsepart)
	IFB EVAL(expr) THEN
		RESULT = truepart
	ELSE
		RESULT = falsepart
	ENDIF
FEND

//////////////////////////////////////////////////
// 【引数】
//   date : 存在するかを調べる日付文字列。YYYYMMDD or YYYY/MM/DD or YYYY-MM-DDのいずれかの形式。 
// 【戻り値】
//   TRUE : 日付として認識できる、FALSE : 日付として認識できない 
//////////////////////////////////////////////////
FUNCTION isDate(date)
	TRY
		GETTIME(0, date)
		RESULT = TRUE
	EXCEPT
		RESULT = FALSE
	ENDTRY
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現で検索した結果をMatchesコレクションとして返します。 
//////////////////////////////////////////////////
FUNCTION reExecute(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Execute(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 正規表現による検索の対象となる文字列 
//   Pattern : 正規表現で使用するパターンを設定 
//   IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse 
//   Global : 文字列全体を検索する場合はTrue、しない場合はFalse 
// 【戻り値】
//   正規表現にマッチするかどうかを示すブール値 
//////////////////////////////////////////////////
FUNCTION reTest(str, Pattern, IgnoreCase = TRUE, Global = TRUE)
	DIM re = CREATEOLEOBJ("VBScript.RegExp")
	re.Pattern = Pattern
	re.IgnoreCase = IgnoreCase
	re.Global = Global
	RESULT = re.Test(str)
FEND

//////////////////////////////////////////////////
// 【引数】
//   inputs : 繰り返す文字列 
//   multiplier : inputsを繰り返す回数 
// 【戻り値】
//   inputsをmultiplier回を繰り返した文字列を返します 
//////////////////////////////////////////////////
FUNCTION strRepeat(inputs, multiplier)
	DIM res = ""
	FOR n = 1 TO multiplier
		res = res + inputs
	NEXT
	RESULT = res
FEND

//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   数値を表示書式に基づいて変換した文字列 
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// 【引数】
//   serial : シリアル値 
//   format : フォーマット 
// 【戻り値】
//   
//////////////////////////////////////////////////
FUNCTION text(serial, format, hour12 = FALSE)
	HASHTBL startDate
	startDate["明治"] = "1868/01/25"
	startDate["大正"] = "1912/07/30"
	startDate["昭和"] = "1926/12/25"
	startDate["平成"] = "1989/01/08"
	startDate["令和"] = "2019/05/01"
	
	DIM baseDate = "1899/12/30"
	serial = VAL(serial)
	SELECT TRUE
		CASE reTest(format, "\[h+\]")
			Matches = reExecute(format, "\[(h+)\]")
			DIM hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "^h+$")
			Matches = reExecute(format, "^(h+)$")
			hour = iif(hour12, Hour(serial) MOD 12, Hour(serial))
			RESULT = text(hour MOD 24, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE reTest(format, "\[m+\]")
			Matches = reExecute(format, "\[(m+)\]")
			RESULT = text(serial * 1440, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
		CASE format = "m"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_MM, "0")
		CASE format = "mm"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_MM2
		CASE format = "n"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN
		CASE format = "nn"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_NN2
		CASE format = "s"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_SS, "0")
		CASE format = "ss"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_SS2
		CASE format = "yyyy"
			GETTIME(serial, baseDate)
			RESULT = G_TIME_YY4
		CASE format = "yy"
			GETTIME(serial, baseDate)
			RESULT = COPY(G_TIME_YY4, 3, 2)
		CASE format = "e"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 2018
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1988
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1925
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1911
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(serial, "yyyy") - 1867
			SELEND
		CASE format = "ee"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 2018, "00")
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1988, "00")
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1925, "00")
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1911, "00")
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0
					RESULT = text(text(serial, "yyyy") - 1867, "00")
			SELEND
		CASE format = "g"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "R"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "H"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "S"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "T"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "M"
			SELEND
		CASE format = "gg"
			RESULT = COPY(text(serial, "ggg"), 1, 1)
		CASE format = "ggg"
			SELECT TRUE
				CASE dateDiff("d", startDate["令和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "令和"
				CASE dateDiff("d", startDate["平成"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "平成"
				CASE dateDiff("d", startDate["昭和"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "昭和"
				CASE dateDiff("d", startDate["大正"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "大正"
				CASE dateDiff("d", startDate["明治"], text(serial, "yyyy/mm/dd")) >= 0;		RESULT = "明治"
			SELEND
		CASE format = "mmmmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 1)
		CASE format = "mmmm"
			DIM month[] = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
			RESULT = month[text(serial, "m") - 1]
		CASE format = "mmm"
			RESULT = COPY(text(serial, "mmmm"), 1, 3)
		CASE format = "dd"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD2, "00")
		CASE format = "d"
			GETTIME(serial, baseDate)
			RESULT = text(G_TIME_DD, "0")
		CASE reTest(format, "^[ad]{3,4}$")
			Matches = reExecute(format, "([ad]{3,4})")
			GETTIME(serial, baseDate)
			DIM aaa[] = "日", "月", "火", "水", "木", "金", "土"
			DIM aaaa[] = "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"
			DIM ddd[] = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
			DIM dddd[] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday";
			RESULT = EVAL(Matches.Item(0).SubMatches(0) + "[" + getWeekday(G_TIME_WW, 1) + "]")
		CASE reTest(format, "(0+\.?0+)?%")
			Matches = reExecute(format, "(0+\.?0+)?%")
			RESULT = text(serial * 100, Matches.Item(0).SubMatches(0)) + "%"
		CASE reTest(format, "^\[DBNum\d{1,4}\](.*?)$")
			Matches = reExecute(format, "^\[DBNum(\d{1,4})\](.*?)$")
			DIM value = VAL(Matches.Item(0).SubMatches(0))
			DIM sss = text(serial, Matches.Item(0).SubMatches(1))
			Matches = reExecute(sss, "(\D+)?(\d+)(\D+)?")
			DIM res = ""
			FOR m = 0 TO Matches.Count - 1
				serial = Matches.Item(m).SubMatches(1)
				SELECT value
					CASE 1, 2
						DIM n[][9] = "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", + _
										"", "壱", "弐", "参", "四", "伍", "六", "七", "八", "九"
						DIM a[][3] = "", "十", "百", "千", + _
										"", "拾", "百", "阡"
						DIM b[][3] = "", "万", "億", "兆", + _
										"", "萬", "億", "兆"
						DIM r = ""
						DIM j = 0
						type = value - 1
						REPEAT
							DIM str = ""
							DIM n4 = serial MOD 10000
							FOR i = LENGTH(n4) TO 1 STEP -1
								s = COPY(n4, i, 1)
								IFB s = 1 AND a[type][LENGTH(n4)-i] <> "" THEN
									str = IIF(s, a[type][LENGTH(n4)-i], "") + str
								ELSE	
									str = n[type][s] + IIF(s, a[type][LENGTH(n4)-i], "") + str
								ENDIF
							NEXT
							IF str <> "" THEN r = str + b[type][j] + r
							j = j + 1
							serial = INT(serial / 10000)
						UNTIL serial = 0
						res = res + Matches.Item(m).SubMatches(0) + r + Matches.Item(m).SubMatches(2)
					CASE 3
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_FULLWIDTH) + Matches.Item(m).SubMatches(2)
					CASE 4
						res = res + Matches.Item(m).SubMatches(0) + STRCONV(serial, SC_HALFWIDTH) + Matches.Item(m).SubMatches(2)
				SELEND
			NEXT
			RESULT = res
		CASE reTest(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			Matches = reExecute(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
			DIM array = SPLIT(Matches.Item(0).SubMatches(1), "/")
			ampm = array[IIF(serial - INT(serial) >= 0.5, 1, 0)]
			hour12 = TRUE
			res = ""
			WITH Matches.Item(0)
				res = text(serial, .SubMatches(0), hour12) + ampm + text(serial, .SubMatches(2), hour12)
			ENDWITH
			RESULT = res
		CASE reTest(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			Matches = reExecute(format, "([^ymdagehns]{0,})?(([ymdagehns])\3{0,})([^ymdagehns]+)?")
			FOR n = 0 TO Matches.Count - 1
				IF n = 0 THEN res = Matches.Item(n).SubMatches(0)
			NEXT
			FOR n = 0 TO Matches.Count - 1
				WITH Matches.Item(n)
					res = res + text(serial, .SubMatches(1), hour12) + .SubMatches(3)
				ENDWITH
			NEXT
			RESULT = res
		CASE format = "0/0"
			DIM separator = POS(".", serial)
			DIM g = 0
			IFB separator <> 0 THEN
				DIM keta = LENGTH(serial)
				DIM shift = POWER(10, keta - separator)
				IFB shift >= POWER(10, 15) THEN
					DIM position = 0
					FOR i = 0 TO 14
						IFB serial * POWER(10, i) - serial >= 1 THEN
							position = i
							BREAK
						ENDIF
					NEXT
					tmp = serial * POWER(10, position)
					FOR i = 1 TO 15
						r = (tmp * POWER(10, i)) / serial - (tmp / serial)
						a1 = tmp * POWER(10, i) - tmp
						IF a1 = INT(a1) THEN BREAK 
					NEXT
					DIM frac[] = a1, r
					g = GCD(frac)
					RESULT = (a1/g) + "/" + (r/g)
				ELSE
					DIM molecule = serial * shift	// 分子
					DIM denominator = shift		// 分母
					DIM nums[] = molecule, denominator
					g = GCD(nums)
					molecule = molecule / g
					denominator = denominator / g
					RESULT = molecule + "/" + denominator
				ENDIF
			ELSE
				RESULT = serial + "/1"
			ENDIF
		CASE reTest(format, "(0+)\.?(0+)?") AND UBound(SPLIT(format, ".")) <= 1 
			Matches = reExecute(format, "(0+)\.?(0+)?")
			len1 = LENGTH(Matches.Item(0).SubMatches(0))
			len2 = LENGTH(Matches.Item(0).SubMatches(1))
			DIM arr[] = LENGTH(INT(serial)), len1
			IFB POS(".", format) THEN
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX) + len2 + 1, len2), " ", "0")
			ELSE
				RESULT = REPLACE(FORMAT(serial, CALCARRAY(arr, CALC_MAX)), " ", "0")
			ENDIF
	SELEND
FEND

//////////////////////////////////////////////////
// 【引数】
//   str : 時刻文字列。hh:nn:ss AM/PM、hh:nn AM/PM、hh AM/PM、hh:nn:ss、hh:nn、hh時nn分ss秒、hh時nn分のいずれかの形式を指定。 
// 【戻り値】
//   シリアル値 (例)0…00:00:00、0.5…12:00:00、0.999988425925926…23:59:59 
//////////////////////////////////////////////////
FUNCTION timeValue(str)
	DIM serial = 0
	DIM Matches
	DIM pattern = "(\d+)"
	DIM hh = "(0?[0-9]|1[0-2])"
	DIM ampm = "([AP]M|[ap]m)"
	SELECT TRUE
		CASE reTest(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(3)) + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + ":" + pattern + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = timeValue(.SubMatches(0) + " " + .SubMatches(2)) + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + hh + " " + ampm + "\b")
			Matches = reExecute(str, "\b" + hh + " " + ampm + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0) MOD 12) + IIF(reTest(.SubMatches(1), "AM|am"), 0, 12)
				serial = serial / 24
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + ":" + pattern + "\b")
			Matches = reExecute(str, "\b" + pattern + ":" + pattern + "\b")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分" + pattern + "秒")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440 + VAL(.SubMatches(2)) / 86400
			ENDWITH
		CASE reTest(str, "\b" + pattern + "時" + pattern + "分")
			Matches = reExecute(str, "\b" + pattern + "時" + pattern + "分")
			WITH Matches.Item(0)
				serial = VAL(.SubMatches(0)) / 24 + VAL(.SubMatches(1)) / 1440
			ENDWITH
		DEFAULT
			serial = ERR_VALUE
	SELEND
	RESULT = serial - INT(serial)
FEND

//////////////////////////////////////////////////
// 【引数】
//   
// 【戻り値】
//   今日の日付 
//////////////////////////////////////////////////
FUNCTION today()
	GETTIME()
	RESULT = G_TIME_YY4 + "/" + G_TIME_MM2 + "/" + G_TIME_DD2
FEND

//////////////////////////////////////////////////
// 【引数】
//   arrayname : 上限値を求める配列の名前 
//   dimension : 返す次元を示す整数 
// 【戻り値】
//   配列の上限値 
//////////////////////////////////////////////////
FUNCTION UBound(arrayname[], dimension = 1)
	RESULT = EVAL("RESIZE(arrayname" + strRepeat("[0]", dimension - 1) + ")")
FEND

//////////////////////////////////////////////////
// 【引数】
//   uwscTime : UWSC時間 
// 【戻り値】
//   シリアル値 
//////////////////////////////////////////////////
FUNCTION uwscToSerial(uwscTime)
	uwscDate = uwscTime / 86400
	RESULT = 36526 + uwscDate
FEND
使用関数

参考文献

  1. Excel は 2 桁の年番号で動作します - Microsoft 365 Apps | Microsoft Learn

関連記事

now関数 (自作関数)
現在の日時のシリアル値を返します。
dateSerial関数 (自作関数)
引数に指定した年、月、日の日付が表すシリアル値を返します。
getSerialTime関数 (自作関数)
UWSC時間からシリアル値を取得します。
uwscToSerial関数 (自作関数)
UWSC時間シリアル値に変換します。
serialToUwsc関数 (自作関数)
シリアル値UWSC時間に変換します。
serialToUNIX関数 (自作関数)
シリアル値UNIX時間に変換します。
UNIXToSerial関数 (自作関数)
UNIX時間 をシリアル値に変換します。
Hour関数 (自作関数)
シリアル値で与えられた時刻から時間を求めます。戻り値は023の範囲の整数となります。
Second関数 (自作関数)
シリアル値からを求めます。
timeValue関数 (自作関数)
指定した時間のシリアル値を求める。