数値を表示書式に基づいて変換した文字列を返します。
GETTIMEで取得する時間はシリアル値でないため、uwscToSerialなどでシリアル値に変換します。
- 構文
- text( serial, format )
- 引数
- serial
- シリアル値
- format
- フォーマット
- 戻値
- 数値を表示書式に基づいて変換した文字列
プログラム
//////////////////////////////////////////////////
// 【引数】
// serial : シリアル値
// format : フォーマット
// 【戻値】
// 数値を表示書式に基づいて変換した文字列
//////////////////////////////////////////////////
FUNCTION text(serial, format)
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"
SELECT TRUE
CASE reTest(format, "\[h+\]")
Matches = reExecute(format, "\[(h+)\]")
RESULT = text(Hour(serial), strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
CASE reTest(format, "^h+$")
Matches = reExecute(format, "^(h+)$")
RESULT = text(Hour(serial) 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"
SELECT text(serial, "m")
CASE 1; RESULT = "January"
CASE 2; RESULT = "February"
CASE 3; RESULT = "March"
CASE 4; RESULT = "April"
CASE 5; RESULT = "May"
CASE 6; RESULT = "June"
CASE 7; RESULT = "July"
CASE 8; RESULT = "August"
CASE 9; RESULT = "September"
CASE 10; RESULT = "October"
CASE 11; RESULT = "November"
CASE 12; RESULT = "December"
SELEND
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 format = "aaaa"
GETTIME(serial, baseDate)
RESULT = getWeekdayName(G_TIME_WW, "aaaa")
CASE format = "aaa"
GETTIME(serial, baseDate)
RESULT = getWeekdayName(G_TIME_WW, "aaa")
CASE format = "dddd"
GETTIME(serial, baseDate)
RESULT = getWeekdayName(G_TIME_WW, "dddd")
CASE format = "ddd"
GETTIME(serial, baseDate)
RESULT = getWeekdayName(G_TIME_WW, "ddd")
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})\](.*?)$")
SELECT Matches.Item(0).SubMatches(0)
CASE 1
CASE 2
CASE 3
RESULT = STRCONV(text(serial, Matches.Item(0).SubMatches(1)), SC_FULLWIDTH)
CASE 4
SELEND
CASE reTest(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
Matches = reExecute(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$")
res = ""
WITH Matches.Item(0)
res = text(serial, .SubMatches(0)) + .SubMatches(1) + text(serial, .SubMatches(2))
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)) + .SubMatches(3)
ENDWITH
NEXT
RESULT = res
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
//////////////////////////////////////////////////
// 【引数】
// interval : 時間単位(yyyy︰年、m︰月、d︰日)
// 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 "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
SELEND
RESULT = d
FEND
//////////////////////////////////////////////////
// 【引数】
// 数値 : GETTIMEで取得した曜日の番号(G_TIME_WW=0:日曜….6:土曜)
// フォーマット : 以下のアルファベットは数値で指定した対応する曜日に置き換えられます。(aaa : 日〜土、aaaa : 日曜日〜土曜日、ddd : Sun〜Sat、dddd : Sunday〜Saturday)
// 【戻値】
// 数値をフォーマットした文字列を返します。
//////////////////////////////////////////////////
FUNCTION getWeekdayName(num, format = "aaa")
DIM re = CREATEOLEOBJ("VBScript.RegExp")
DIM aaa[] = "日", "月", "火", "水", "木", "金", "土";
DIM aaaa[] = "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日";
DIM ddd[] = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat";
DIM dddd[] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday";
match = reExecute(format, "(a|d)+")
type = match.Item(0).Value
RESULT = reReplace(format, EVAL(type + "[" + num + "]"), type)
FEND
//////////////////////////////////////////////////
// 【引数】
// シリアル値 : 時間を表すシリアル値を指定
// 【戻値】
// 例)0…00、0.5…12、1.0…24
//////////////////////////////////////////////////
FUNCTION Hour(serial)
RESULT = REPLACE(FORMAT(INT(serial * 24), 2), " ", "0")
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
//////////////////////////////////////////////////
// 【引数】
// str1 : 置換される文字列
// str2 : 置換後の文字列
// Pattern : 置換する文字列のパターン
// IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse
// Global : 文字列全体を検索する場合はTrue、しない場合はFalse
// 【戻値】
// 正規表現置換後の文字列
//////////////////////////////////////////////////
FUNCTION reReplace(str1, str2, Pattern, IgnoreCase = TRUE, Global = TRUE)
DIM re = CREATEOLEOBJ("VBScript.RegExp")
re.Pattern = Pattern
re.IgnoreCase = IgnoreCase
re.Global = Global
RESULT = re.Replace(str1, str2)
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
//////////////////////////////////////////////////
// 【引数】
// 配列 : 上限値を求める配列
// 【戻値】
// 配列の上限値
//////////////////////////////////////////////////
FUNCTION UBound(array[])
RESULT = RESIZE(array)
FEND
解説
- 2-7行目
HASHTBL startDate startDate["明治"] = "1868/01/25" startDate["大正"] = "1912/07/30" startDate["昭和"] = "1926/12/25" startDate["平成"] = "1989/01/08" startDate["令和"] = "2019/05/01"
キーに指定した元号の始期を代入。
- 9行目
DIM baseDate = "1899/12/30"
- baseDate
- シリアル値の基準日を代入
- 11行目
SELECT TRUE
- 12行目>>>
- 15行目>>>
- 18行目>>>
- 21行目>>>
- 24行目>>>
- 27行目>>>
- 30行目>>>
- 33行目>>>
- 36行目>>>
- 39行目>>>
- 42行目>>>
- 45行目>>>
- 58行目>>>
- 71行目>>>
- 79行目>>>
- 81行目>>>
- 89行目>>>
- 91行目>>>
- 106行目>>>
- 108行目>>>
- 111行目>>>
- 114行目>>>
- 117行目>>>
- 120行目>>>
- 123行目>>>
- 126行目>>>
- 129行目>>>
- 141行目>>>
- 148行目>>>
- 159行目>>>
- 12-14行目
CASE reTest(format, "\[h+\]") Matches = reExecute(format, "\[(h+)\]") RESULT = text(Hour(serial), strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
「時間」を経過時間で表示します。
- 15-17行目
CASE reTest(format, "^h+$") Matches = reExecute(format, "^(h+)$") RESULT = text(Hour(serial) MOD 24, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
時刻の「時間」を表示します。
- 18-20行目
CASE reTest(format, "\[m+\]") Matches = reExecute(format, "\[(m+)\]") RESULT = text(serial * 1440, strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
「月」を経過時間で表示します。
- 21-23行目
CASE format = "m" GETTIME(serial, baseDate) RESULT = text(G_TIME_MM, "0")
日付の「月」を表示します。
- 24-26行目
CASE format = "mm" GETTIME(serial, baseDate) RESULT = G_TIME_MM2
日付の「月」を表示します。2桁に満たない場合は0埋めします。
- 27-29行目
CASE format = "n" GETTIME(serial, baseDate) RESULT = G_TIME_NN
- 30-32行目
CASE format = "nn" GETTIME(serial, baseDate) RESULT = G_TIME_NN2
- 33-35行目
CASE format = "s" GETTIME(serial, baseDate) RESULT = text(G_TIME_SS, "0")
- 36-38行目
CASE format = "ss" GETTIME(serial, baseDate) RESULT = G_TIME_SS2
- 39-41行目
CASE format = "yyyy" GETTIME(serial, baseDate) RESULT = G_TIME_YY4
- 42-44行目
CASE format = "yy" GETTIME(serial, baseDate) RESULT = COPY(G_TIME_YY4, 3, 2)
- 45-57行目
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
- 58-70行目
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
- 71-78行目
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
- 79-80行目
CASE format = "gg" RESULT = COPY(text(serial, "ggg"), 1, 1)
- 81-88行目
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
- 89-90行目
CASE format = "mmmmm" RESULT = COPY(text(serial, "mmmm"), 1, 1)
- 91-105行目
CASE format = "mmmm" SELECT text(serial, "m") CASE 1; RESULT = "January" CASE 2; RESULT = "February" CASE 3; RESULT = "March" CASE 4; RESULT = "April" CASE 5; RESULT = "May" CASE 6; RESULT = "June" CASE 7; RESULT = "July" CASE 8; RESULT = "August" CASE 9; RESULT = "September" CASE 10; RESULT = "October" CASE 11; RESULT = "November" CASE 12; RESULT = "December" SELEND
- 106-107行目
CASE format = "mmm" RESULT = COPY(text(serial, "mmmm"), 1, 3)
- 108-110行目
CASE format = "dd" GETTIME(serial, baseDate) RESULT = text(G_TIME_DD2, "00")
- 111-113行目
CASE format = "d" GETTIME(serial, baseDate) RESULT = text(G_TIME_DD, "0")
- 114-116行目
CASE format = "aaaa" GETTIME(serial, baseDate) RESULT = getWeekdayName(G_TIME_WW, "aaaa")
- 117-119行目
CASE format = "aaa" GETTIME(serial, baseDate) RESULT = getWeekdayName(G_TIME_WW, "aaa")
- 120-122行目
CASE format = "dddd" GETTIME(serial, baseDate) RESULT = getWeekdayName(G_TIME_WW, "dddd")
- 123-125行目
CASE format = "ddd" GETTIME(serial, baseDate) RESULT = getWeekdayName(G_TIME_WW, "ddd")
- 126-128行目
CASE reTest(format, "(0+\.?0+)?%") Matches = reExecute(format, "(0+\.?0+)?%") RESULT = text(serial * 100, Matches.Item(0).SubMatches(0)) + "%"
- 129-140行目
CASE reTest(format, "^\[DBNum\d{1,4}\](.*?)$") Matches = reExecute(format, "^\[DBNum(\d{1,4})\](.*?)$") SELECT Matches.Item(0).SubMatches(0) CASE 1 CASE 2 CASE 3 RESULT = STRCONV(text(serial, Matches.Item(0).SubMatches(1)), SC_FULLWIDTH) CASE 4 SELEND
- 141-147行目
CASE reTest(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$") Matches = reExecute(format, "^(.*?)(AM\/PM|am\/pm|A\/P|a\/p)(.*?)$") res = "" WITH Matches.Item(0) res = text(serial, .SubMatches(0)) + .SubMatches(1) + text(serial, .SubMatches(2)) ENDWITH RESULT = res
- 148-158行目
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)) + .SubMatches(3) ENDWITH NEXT RESULT = res
- 159-168行目
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
書式記号
数値の書式記号
書式記号 |
説明 |
0 |
書式設定で指定した0の桁数よりも入力した数値の桁数が少ない場合、0が表示されます。 |
# |
|
/ |
分数を表す。 |
日付の書式記号
年
書式記号 |
説明 |
yy |
西暦の下2桁を表示します。 |
yyyy |
西暦を4桁で表示します。 |
e |
年を年号を元に表示します。 |
ee |
年を年号を元に2桁の数値で表示します。 |
g |
元号をアルファベットの頭文字(M、T、S、H、R)で表示します。 |
gg |
元号を漢字の頭文字(明、大、昭、平、令)で表示します。 |
ggg |
元号を漢字(明治、大正、昭和、平成、令和)で表示します。 |
月
書式記号 |
説明 |
m |
月を表示します。 |
mm |
1桁の月には0をつけて2桁で表示します。 |
mmm |
英語の月の頭文字3文字(Jan~Dec)を表示します。 |
mmmm |
英語の月(January~December)を表示します。 |
mmmmm |
英語の月の頭文字(J~D)で表示します。 |
日
書式記号 |
説明 |
d |
日にちを表示します。 |
dd |
1桁の日にちには0をつけて2桁で表示します。 |
曜日
書式記号 |
説明 |
ddd |
英語の曜日の頭文字から3文字(Sun~Sat)を表示します。 |
dddd |
英語の曜日(Sunday~Saturday)を表示します。 |
aaa |
漢字で曜日の頭文字(日~土)を表示します。 |
aaaa |
漢字で曜日(日曜日~土曜日)を表示します。 |
時間の書式記号
書式記号 |
説明 |
h |
時刻(0~23)を表示します。 |
hh |
1桁の時刻には0を付けて時刻(00~23)を表示します。 |
n |
分(0~59)を表示します。 |
nn |
1桁の分は、0を付けて分(00~59)を表示します。 |
s |
秒(0~59)を表示します。 |
ss |
1桁の秒は0を付けて秒(00~59)を表示します。 |
[ ] |
経過時間を表します。例:[m]:ss |
AM/PM
書式記号 |
説明 |
h AM/PM |
12時間表示で時刻の後にAMまたはPMを表示します。 |
h:nn am/pm |
12時間表示で時刻の後にamまたはpmを表示します。 |
hh:nn:ss A/P |
12時間表示で時刻の後にAまたはPを表示します。 |
hh:nn:ss a/p |
12時間表示で時刻の後にaまたはpを表示します。 |
文字列の書式記号
メモ
//////////////////////////////////////////////////
// 【引数】
// serial : シリアル値
// format : フォーマット
// 【戻値】
// 数値を表示書式に基づいて変換した文字列
//////////////////////////////////////////////////
FUNCTION text(serial, format)
SELECT TRUE
CASE reTest(format, "\[h+\]")
Matches = reExecute(format, "\[(h+)\]")
RESULT = text(Hour(serial), strRepeat("0", LENGTH(Matches.Item(0).SubMatches(0))))
CASE reTest(format, "^h+$")
Matches = reExecute(format, "^(h+)$")
RESULT = text(Hour(serial) 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, "1899/12/30")
RESULT = text(G_TIME_MM, "0")
CASE format = "mm"
GETTIME(serial, "1899/12/30")
RESULT = G_TIME_MM2
CASE format = "s"
GETTIME(serial, "1899/12/30")
RESULT = text(G_TIME_SS, "0")
CASE format = "ss"
GETTIME(serial, "1899/12/30")
RESULT = G_TIME_SS2
CASE format = "yyyy"
GETTIME(serial, "1899/12/30")
RESULT = G_TIME_YY4
CASE format = "yy"
GETTIME(serial, "1899/12/30")
RESULT = COPY(G_TIME_YY4, 3, 2)
CASE format = "e"
SELECT TRUE
// 令和
CASE dateDiff("d", "2019/05/01", text(serial, "yyyy/mm/dd")) >= 0
RESULT = text(serial, "yyyy") – 2018
// 平成
CASE dateDiff("d", "1989/01/08", text(serial, "yyyy/mm/dd")) >= 0
RESULT = text(serial, "yyyy") – 1988
// 昭和
CASE dateDiff("d", "1926/12/25", text(serial, "yyyy/mm/dd")) >= 0
RESULT = text(serial, "yyyy") – 1925
// 大正
CASE dateDiff("d", "1912/07/30", text(serial, "yyyy/mm/dd")) >= 0
RESULT = text(serial, "yyyy") – 1911
// 明治
CASE dateDiff("d", "1868/01/25", text(serial, "yyyy/mm/dd")) >= 0
RESULT = text(serial, "yyyy") – 1867
SELEND
CASE format = "g"
SELECT TRUE
// 令和
CASE dateDiff("d", "2019/05/01", text(serial, "yyyy/mm/dd")) >= 0
RESULT = "R"
// 平成
CASE dateDiff("d", "1989/01/08", text(serial, "yyyy/mm/dd")) >= 0
RESULT = "H"
// 昭和
CASE dateDiff("d", "1926/12/25", text(serial, "yyyy/mm/dd")) >= 0
RESULT = "S"
// 大正
CASE dateDiff("d", "1912/07/30", text(serial, "yyyy/mm/dd")) >= 0
RESULT = "T"
// 明治
CASE dateDiff("d", "1868/01/25", 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", "2019/05/01", text(serial, "yyyy/mm/dd")) >= 0
RESULT = "令和"
// 平成
CASE dateDiff("d", "1989/01/08", text(serial, "yyyy/mm/dd")) >= 0
RESULT = "平成"
// 昭和
CASE dateDiff("d", "1926/12/25", text(serial, "yyyy/mm/dd")) >= 0
RESULT = "昭和"
// 大正
CASE dateDiff("d", "1912/07/30", text(serial, "yyyy/mm/dd")) >= 0
RESULT = "大正"
// 明治
CASE dateDiff("d", "1868/01/25", text(serial, "yyyy/mm/dd")) >= 0
RESULT = "明治"
SELEND
CASE format = "mmmmm"
RESULT = COPY(text(serial, "mmmm"), 1, 1)
CASE format = "mmmm"
SELECT text(serial, "m")
CASE 1; RESULT = "January"
CASE 2; RESULT = "February"
CASE 3; RESULT = "March"
CASE 4; RESULT = "April"
CASE 5; RESULT = "May"
CASE 6; RESULT = "June"
CASE 7; RESULT = "July"
CASE 8; RESULT = "August"
CASE 9; RESULT = "September"
CASE 10; RESULT = "October"
CASE 11; RESULT = "November"
CASE 12; RESULT = "December"
SELEND
CASE format = "mmm"
RESULT = COPY(text(serial, "mmmm"), 1, 3)
CASE format = "dd"
GETTIME(serial, "1899/12/30")
RESULT = text(G_TIME_DD2, "00")
CASE format = "d"
GETTIME(serial, "1899/12/30")
RESULT = text(G_TIME_DD, "0")
CASE format = "aaaa"
GETTIME(serial, "1899/12/30")
RESULT = getWeekdayName(G_TIME_WW, "aaaa")
CASE format = "aaa"
GETTIME(serial, "1899/12/30")
RESULT = getWeekdayName(G_TIME_WW, "aaa")
CASE format = "dddd"
GETTIME(serial, "1899/12/30")
RESULT = getWeekdayName(G_TIME_WW, "dddd")
CASE format = "ddd"
GETTIME(serial, "1899/12/30")
RESULT = getWeekdayName(G_TIME_WW, "ddd")
CASE reTest(format, "(0+\.?0+)?%")
Matches = reExecute(format, "(0+\.?0+)?%")
RESULT = text(serial * 100, Matches.Item(0).SubMatches(0)) + "%"
CASE reTest(format, "([ymd]+)/([ymd]+)/([ymd]+)")
Matches = reExecute(format, "([ymd]+)/([ymd]+)/([ymd]+)")
RESULT = text(serial, Matches.Item(0).SubMatches(0)) + "/" + text(serial, Matches.Item(0).SubMatches(1)) + "/" + text(serial, Matches.Item(0).SubMatches(2))
CASE reTest(format, "^(.*?)\/(.*?)$")
// 分母、分子
DIM demon, numer
// 分数
DIM frac[1]
DIM Pattern = "(\d+\.\d*?(\d+)\2+)"
IFB reTest(serial, Pattern) THEN
Matches = reExecute(serial, Pattern)
// 循環節の桁数
digit = LENGTH(Matches.Item(0).SubMatches(1))
denom = POWER(10, digit) – 1
numer = serial * POWER(10, digit) – serial
// 分子が小数なら整数に通分する
num = POWER(10, LENGTH(numer) – POS(".", numer))
denom = denom * num
numer = numer * num
ELSE
denom = POWER(10, LENGTH(serial) – POS(".", serial))
numer = serial * denom
ENDIF
frac[0] = denom
frac[1] = numer
// 最大公約数を求める
n = GCD(frac)
denom = frac[0]
numer = frac[1]
// 既約分数にする
denom = denom / n
numer = numer / n
RESULT = numer + "/" + denom
CASE reTest(format, "((#+),?)+")
RESULT = reReplace(serial, "$1,", "(\d)(?=(\d{3})+(?!\d))")
CASE reTest(format, "(0+)\.?(0+)?")
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
CASE reTest(format, "(.+)?((?:AM?|am?)\/(?:PM?|pm?))(.+)?")
DIM arr[2]
Matches = reExecute(format, "(.+)?((?:AM?|am?)\/(?:PM?|pm?))(.+)?")
res = ""
FOR n = 0 TO Matches.Item(0).SubMatches.Count – 1
arr[n] = Matches.Item(0).SubMatches(n)
NEXT
DIM res[2]
FOR n = 0 TO 2 STEP 2
Matches = reExecute(arr[n], "([^yegmdahs]+)?(([yegmdahs])\3{0,})([^yegmdahs]+)?")
FOR m = 0 TO Matches.Count – 1
SELECT TRUE
// 「m」を含む
CASE reTest(Matches.Item(m).SubMatches(1), "m+")
// アルファベット部分の前後で「m」が月か分か判断
SELECT TRUE
// 「m」が最初に出現するアルファベットなら
CASE m = 0
// 次に出現するアルファベットが「d」なら「m」は月
IFB reTest(Matches.Item(m+1).SubMatches(1), "d+") THEN
res[n] = res[n] + text(serial, Matches.Item(m).SubMatches(1))
ENDIF
// 次に出現するアルファベットが「s」なら「m」は分
IFB reTest(Matches.Item(m+1).SubMatches(1), "s+") THEN
res[n] = res[n] + text(INT(serial * 1440) MOD 60, strRepeat("0", LENGTH(Matches.Item(m).SubMatches(1))))
ENDIF
// 「m」が2番目(2-1=1)以降に出現するアルファベットなら
CASE m >= 1
// 前に出現したアルファベットが「y」なら「m」は月
IFB reTest(Matches.Item(m-1).SubMatches(1), "y+") THEN
res[n] = res[n] + text(serial, Matches.Item(m).SubMatches(1))
ENDIF
// 前に出現したアルファベットが「h」なら「m」は分
IFB reTest(Matches.Item(m-1).SubMatches(1), "h+") THEN
res[n] = res[n] + text(INT(serial * 1440) MOD 60, strRepeat("0", LENGTH(Matches.Item(m).SubMatches(1))))
ENDIF
SELEND
CASE reTest(Matches.Item(m).SubMatches(1), "^h+$")
IFB text(serial, Matches.Item(m).SubMatches(1)) < 12 THEN
res[1] = COPY(arr[1], 1, POS("/", arr[1]) - 1) // 午前
ELSE
res[1] = COPY(arr[1], POS("/", arr[1]) + 1) // 午後
ENDIF
res[n] = res[n] + text(serial, Matches.Item(m).SubMatches(1))
// それ以外ならば
DEFAULT
res[n] = res[n] + text(serial, Matches.Item(m).SubMatches(1))
SELEND
res[n] = Matches.Item(m).SubMatches(0) + res[n] + Matches.Item(m).SubMatches(3)
NEXT
NEXT
RESULT = JOIN(res, "")
CASE reTest(format, "(([yegmdahs])\2{0,})([^yegmdahs]+)?")
res = ""
Matches = reExecute(format, "(([yegmdahs])\2{0,})([^yegmdahs]+)?")
FOR n = 0 TO Matches.Count - 1
// mの前がyもしくはmの後がdならば→mは月
SELECT TRUE
// 「m」を含む
CASE reTest(Matches.Item(n).SubMatches(0), "m+")
// アルファベット部分の前後で「m」が月か分か判断
SELECT TRUE
// 「m」が最初に出現するアルファベットなら
CASE n = 0
// 次に出現するアルファベットが「d」なら「m」は月
IFB reTest(Matches.Item(n+1).SubMatches(0), "d+") THEN
res = res + text(serial, Matches.Item(n).SubMatches(0))
ENDIF
// 次に出現するアルファベットが「s」なら「m」は分
IFB reTest(Matches.Item(n+1).SubMatches(0), "s+") THEN
res = res + text(INT(serial * 1440) MOD 60, strRepeat("0", LENGTH(Matches.Item(n).SubMatches(0))))
ENDIF
// 「m」が2番目(2-1=1)以降に出現するアルファベットなら
CASE n >= 1
// 前に出現したアルファベットが「y」なら「m」は月
IFB reTest(Matches.Item(n-1).SubMatches(0), "y+") THEN
res = res + text(serial, Matches.Item(n).SubMatches(0))
ENDIF
// 前に出現したアルファベットが「h」なら「m」は分
IFB reTest(Matches.Item(n-1).SubMatches(0), "h+") THEN
res = res + text(INT(serial * 1440) MOD 60, strRepeat("0", LENGTH(Matches.Item(n).SubMatches(0))))
ENDIF
SELEND
// それ以外ならば
DEFAULT
res = res + text(serial, Matches.Item(n).SubMatches(0))
SELEND
res = res + Matches.Item(n).SubMatches(2)
NEXT
RESULT = res
SELEND
FEND
//////////////////////////////////////////////////
// 【引数】
// interval : 時間単位(yyyy︰年、m︰月、d︰日)
// 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 "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
SELEND
RESULT = d
FEND
//////////////////////////////////////////////////
// 【引数】
// arr : 最大公約数を求める数値を格納した配列
// 【戻値】
// 最大公約数
//////////////////////////////////////////////////
FUNCTION GCD(arr[])
DIM c = LENGTH(arr)
DIM rem = arr[c-1] MOD arr[c-2]
IFB rem = 0 THEN
IFB LENGTH(arr) = 2 THEN
RESULT = arr[c-2]
EXIT
ENDIF
RESIZE(arr, c-2)
RESULT = GCD(arr)
EXIT
ENDIF
arr[c-1] = arr[c-2]
arr[c-2] = rem
RESULT = GCD(arr)
FEND
//////////////////////////////////////////////////
// 【引数】
// 数値 : GETTIMEで取得した曜日の番号(G_TIME_WW=0:日曜….6:土曜)
// フォーマット : 以下のアルファベットは数値で指定した対応する曜日に置き換えられます。(aaa : 日〜土、aaaa : 日曜日〜土曜日、ddd : Sun〜Sat、dddd : Sunday〜Saturday)
// 【戻値】
// 数値をフォーマットした文字列を返します。
//////////////////////////////////////////////////
FUNCTION getWeekdayName(num, format = "aaa")
DIM re = CREATEOLEOBJ("VBScript.RegExp")
DIM aaa[] = "日", "月", "火", "水", "木", "金", "土";
DIM aaaa[] = "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日";
DIM ddd[] = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat";
DIM dddd[] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday";
match = reExecute(format, "(a|d)+")
type = match.Item(0).Value
RESULT = reReplace(format, EVAL(type + "[" + num + "]"), type)
FEND
//////////////////////////////////////////////////
// 【引数】
// シリアル値 : 時間を表すシリアル値を指定
// 【戻値】
// 例)0…00、0.5…12、1.0…24
//////////////////////////////////////////////////
FUNCTION Hour(serial)
RESULT = REPLACE(FORMAT(INT(serial * 24), 2), " ", "0")
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
//////////////////////////////////////////////////
// 【引数】
// str1 : 置換される文字列
// str2 : 置換後の文字列
// Pattern : 置換する文字列のパターン
// IgnoreCase : 大文字・小文字を区別しない場合はTrue、区別する場合はFalse
// Global : 文字列全体を検索する場合はTrue、しない場合はFalse
// 【戻値】
// 正規表現置換後の文字列
//////////////////////////////////////////////////
FUNCTION reReplace(str1, str2, Pattern, IgnoreCase = TRUE, Global = TRUE)
DIM re = CREATEOLEOBJ("VBScript.RegExp")
re.Pattern = Pattern
re.IgnoreCase = IgnoreCase
re.Global = Global
RESULT = re.Replace(str1, str2)
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