日付と時刻を挿入

提供: MeryWiki
2018年12月8日 (土) 19:33時点におけるSukemaru (トーク | 投稿記録)による版 (sukemaru 版を追加)
ナビゲーションに移動 検索に移動

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);

sukemaru 版

ログへの書き出し用のフォーマットとして現在日時を日本時間 "YYYY-MM-DD hh:mm:ss UTC+9000" で返します。

/* 関数 DateTime(separator,utc) */
// 現在日時を日本時間 "YYYY-MM-DD hh:mm:ss UTC+9000" で返す
// 引数 separator は年月日の区切り文字、utc は "UTC+9000" の有無の設定用
// 引数を省略した場合は "YYYY/MM/DD hh:mm:ss" で返す

function DateTime(separator,utc){
  var d = new Date();
  var b = separator ? separator : "/";
  var u = utc ? " "+d.toString().replace(/^.+(UTC\+\d{4}).+$/,"$1") : "";
  var pad = function(str){return str.replace(/\b[0-9]\b/g,function($0){return "0"+$0;});};
  return pad(d.getFullYear()+b+(d.getMonth()+1)+b+d.getDate()+" "
    +d.toLocaleTimeString("ja-JP")+u);
}


// ▼ 出力例 ▼
var $now;

// エディタ上に YYYY/MM/DD hh:mm:ss で記述する
$now = DateTime();
document.Write( $now + "\n" );
document.selection.Text = $now;

// ステータスバーに YYYY/MM/DD hh:mm:ss で表示する
$now = DateTime( 0 );
Status = "現在の時刻 " + $now;

// アウトプットバーに YYYY-MM-DD hh:mm:ss UTC+9000 で出力する
OutputBar.Writeln( DateTime( "-", true ) );

// ダイアログに YYYY-MM-DD hh:mm:ss で表示する
$now = DateTime( "-" );
Alert( "日本時間の現在時刻:\n" + $now );


  • マクロに組み込みやすく、引数なしのシンプルなバージョンも。
/* 関数 DateTime() */
// 現在日時を日本時間 "YYYY-MM-DD hh:mm:ss UTC+9000" で返す
// "UTC+9000" が不要なら、toTimeString → toLocaleTimeString

function DateTime(){
  var d = new Date();
  var pad = function(str){return str.replace(/\b[0-9]\b/g,function($0){return "0"+$0;});};
  return pad(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.toTimeString());
}

// アウトプットバーに YYYY-MM-DD hh:mm:ss UTC+9000 で出力する
OutputBar.Writeln( DateTime() );
スポンサーリンク