「日付と時刻を挿入」の版間の差分
ナビゲーションに移動
検索に移動
細 (更新履歴の書式を変更) |
編集の要約なし |
||
68行目: | 68行目: | ||
); | ); | ||
Document.Write(result); | Document.Write(result); | ||
</source> | </source> |
2021年2月5日 (金) 22:54時点における版
n 版
メモ帳のF5キーの再現です。いまの時刻をカーソル位置に挿入します。
// 日付と時刻を挿入.js
d = new Date
document.selection.Text = d.getHours() + ':' + p(d.getMinutes()) + ' ' + [p(d.getYear()), p(d.getMonth() + 1), p(d.getDate())].join('/')
function p(x) { return x < 10 ? '0' + x : x }
masme 版
書式設定(※下表参考)を元に現在日時を出力・挿入します。
書式設定の例 | 出力結果の例 |
---|---|
h:mm YYYY/MM/DD |
4:05 2001/02/03
|
YYYY-MM-DD hh:mm:ss |
2001-02-03 04:05:06
|
ddd, MMM D, YYYY |
Sat, Feb 3, 2001
|
YY年M月D日(d) h時m分s秒 |
01年2月3日(土) 4時5分6秒
|
hhmm 'hhmm' h'hm'm '' |
0405 hhmm 4hm5 '
|
更新履歴
- 2016/02/28
- 書式 d と ddd の内容を交換し、d を 日~土 出力、ddd を Sun~Sat 出力にした。
- MMM が Jan~Dec 出力なので字数を揃えた。
- アポストロフィー
'
によるエスケープを導入。
- 2014/02/12
- 初版公開。
ソースコード
//■日付と時刻
// 2014/02/12-2016/02/28
//■書式設定 ●初期値="h:mm YYYY/MM/DD"
var format = "h:mm YYYY/MM/DD";
var symbol = (function(){
var t = new Date(), pad = function(n){return n<10? "0"+n : n};
return {
"YYYY": (t.getFullYear()),
"YY": pad(t.getFullYear()%100),
"MMM": ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][t.getMonth()],
"MM": pad(t.getMonth()+1),
"M": (t.getMonth()+1),
"DD": pad(t.getDate()),
"D": (t.getDate()),
"ddd": ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][t.getDay()],
"d": ["日","月","火","水","木","金","土"][t.getDay()],
"hh": pad(t.getHours()),
"h": (t.getHours()),
"mm": pad(t.getMinutes()),
"m": (t.getMinutes()),
"ss": pad(t.getSeconds()),
"s": (t.getSeconds())
};
})();
var result = format.replace(/(')(.*?)\1|([YMDdhms])\3*/g,
function($0,esc,ltr){return ltr || esc || symbol[$0] || $0;}
);
Document.Write(result);
スポンサーリンク