「日付と時刻を挿入」の版間の差分

提供:MeryWiki
ナビゲーションに移動 検索に移動
(→‎sukemaru 版: "UTC+9000"となっていたのを"UTC+0900"に修正。DateTime_JP()を追加)
(更新履歴の書式を変更)
28行目: 28行目:


== 更新履歴 ==
== 更新履歴 ==
* 2016/02/28
; 2016/02/28
** 書式 d と ddd の内容を交換し、d を 日~土 出力、ddd を Sun~Sat 出力にした。
* 書式 d と ddd の内容を交換し、d を 日~土 出力、ddd を Sun~Sat 出力にした。
*** MMM が Jan~Dec 出力なので字数を揃えた。
** MMM が Jan~Dec 出力なので字数を揃えた。
** アポストロフィー <code>'</code> によるエスケープを導入。
* アポストロフィー <code>'</code> によるエスケープを導入。
* 2014/02/12
; 2014/02/12
** 初版公開。
* 初版公開。


== ソースコード ==
== ソースコード ==

2019年12月21日 (土) 22:05時点における版

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" の文字列で返します。

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

/**
 * 関数 DateTime(bar,utc)
 * 現在日時を日本時間  "YYYY-MM-DD hh:mm:ss UTC+0900"  の文字列で返す
 * 引数 bar は年月日の区切り文字 ( デフォルト: "/" )
 * 引数 utc は文字列  "UTC+0900"  の表示の設定用 ( true / false )
 * 引数を省略した場合は  "YYYY/MM/DD hh:mm:ss"  の文字列で返す
 */
function DateTime(bar,utc) {
  var d = new Date();
  var yymmdd = bar ? d.getFullYear()+bar+(d.getMonth()+1)+bar+d.getDate()
                   : d.getFullYear()+"/"+(d.getMonth()+1)+"/"+d.getDate();
  var hhmmss = utc ? d.toTimeString()
                   : d.toLocaleTimeString();
  var pad = function(str) {
    return str.replace(/[0-9]+/g,function($0) {
      return $0.length<2 ? "0"+$0 : $0;
    });
  }
  return pad(yymmdd+" "+hhmmss);
}


// ▼ 使用例 ▼
// ※変数に代入でも、文字列値としてそのまま使ってもよい
{
  var $now;

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

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

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

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


  • マクロに組み込みやすいように、引数なしのシンプルなバージョンも。
/**
 * 関数 DateTime()
 * 現在日時を日本時間  "YYYY-MM-DD hh:mm:ss UTC+0900"  で返す(年月日の区切り: "-" )
 * 末尾の"UTC+0900"  が不要なら、toTimeString → toLocaleTimeString
 */
function DateTime(){
  var d = new Date();
  var pad = function(str){return str.replace(/[0-9]+/g,function($0){return $0.length<2 ? "0"+$0 : $0;});}
  return pad(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.toTimeString());
}

// ▼ 使用例 ▼
// アウトプットバーに YYYY-MM-DD hh:mm:ss UTC+0900 で出力する
OutputBar.Writeln( DateTime() );


  • さらにログ用っぽく、ミリ秒つきバージョンも。
マクロ内の各処理の時刻を出力する場合、変数 d = new Date() は関数スコープのなかに置いてください。
/**
 * 関数 DateTime_ms()
 * 現在日時(ミリ秒つき)を  "YYYY-MM-DD hh:mm:ss.sss UTC+0900"  で返す(年月日の区切り: "-" )
 */
function DateTime_ms(){
  var d = newDate();
  var pad = function(str){return str.replace(/[0-9]+/g,function($0){return $0.length<2 ? "0"+$0 : $0;});}
  return d.getFullYear()+"-"+pad((d.getMonth()+1)+"-"+d.getDate()+" "+d.toLocaleTimeString())+"."+(d.getMilliseconds()+"000").substr(0,3)+" UTC+0900";
}

// ▼ 使用例 ▼
// アウトプットバーに YYYY-MM-DD hh:mm:ss.sss UTC+0900 で出力する
OutputBar.Writeln( DateTime_ms() );


  • "YYYY年M月D日 hh:mm:ss" バージョンも。
/**
 * 関数 DateTime_JP()
 * 現在日時を日本時間  "YYYY年M月D日 hh:mm:ss"  で返す
 */
function DateTime_JP(){
  var d = new Date();
  return d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日 "+d.toLocaleTimeString();
}

// ▼ 使用例 ▼
// エディタ上に YYYY年M月D日 hh:mm:ss で記述する
  document.Writeln( DateTime_JP() );
スポンサーリンク