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

提供: MeryWiki
ナビゲーションに移動 検索に移動
Sukemaru (トーク | 投稿記録)
sukemaru 版を追加
Sukemaru (トーク | 投稿記録)
71行目: 71行目:


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


<source lang="javascript">
<source lang="javascript">
/* 関数 DateTime(separator,utc) */
/**
// 現在日時を日本時間 "YYYY-MM-DD hh:mm:ss UTC+9000" で返す
* 関数 DateTime(bar,utc)
// 引数 separator は年月日の区切り文字、utc は "UTC+9000" の有無の設定用
* 現在日時を日本時間 "YYYY-MM-DD hh:mm:ss UTC+9000" の文字列で返す
// 引数を省略した場合は "YYYY/MM/DD hh:mm:ss" で返す
* 引数 bar は年月日の区切り文字 ( デフォルト: "/" )
 
* 引数 utc は文字列  "UTC+9000" の表示の設定用 ( true / false )
function DateTime(separator,utc){
* 引数を省略した場合は "YYYY/MM/DD hh:mm:ss" の文字列で返す
*/
function DateTime(bar,utc) {
   var d = new Date();
   var d = new Date();
   var b = separator ? separator : "/";
   var yymmdd = bar ? d.getFullYear()+bar+(d.getMonth()+1)+bar+d.getDate()
   var u = utc ? " "+d.toString().replace(/^.+(UTC\+\d{4}).+$/,"$1") : "";
                  : d.getFullYear()+"/"+(d.getMonth()+1)+"/"+d.getDate();
   var pad = function(str){return str.replace(/\b[0-9]\b/g,function($0){return "0"+$0;});};
   var hhmmss = utc ? d.toTimeString()
   return pad(d.getFullYear()+b+(d.getMonth()+1)+b+d.getDate()+" "
                  : d.toLocaleTimeString();
    +d.toLocaleTimeString("ja-JP")+u);
   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;
// ※変数に代入でも、文字列値としてそのまま使ってもよい
{
  var $now;


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


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


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


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


112行目: 124行目:
* マクロに組み込みやすく、引数なしのシンプルなバージョンも。
* マクロに組み込みやすく、引数なしのシンプルなバージョンも。
<source lang="javascript">
<source lang="javascript">
/* 関数 DateTime() */
/*
// 現在日時を日本時間 "YYYY-MM-DD hh:mm:ss UTC+9000" で返す
* 関数 DateTime()
// "UTC+9000" が不要なら、toTimeString → toLocaleTimeString
* 現在日時を日本時間 "YYYY-MM-DD hh:mm:ss UTC+9000" で返す(年月日の区切り: "-" )
 
* 末尾の"UTC+9000" が不要なら、toTimeString → toLocaleTimeString
*/
function DateTime(){
function DateTime(){
   var d = new Date();
   var d = new Date();
   var pad = function(str){return str.replace(/\b[0-9]\b/g,function($0){return "0"+$0;});};
   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());
   return pad(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.toTimeString());
}
}


// ▼ 使用例 ▼
// アウトプットバーに YYYY-MM-DD hh:mm:ss UTC+9000 で出力する
// アウトプットバーに YYYY-MM-DD hh:mm:ss UTC+9000 で出力する
OutputBar.Writeln( DateTime() );
OutputBar.Writeln( DateTime() );
</source>
* さらにログ用っぽく、ミリ秒つきバージョンも。
: マクロ内の各処理の時刻を出力する場合、変数 d = new Date() は関数のなかに置いてください。
<source lang="javascript">
/*
* 関数 DateTime_ms()
* 現在日時(ミリ秒つき)を  "YYYY-MM-DD hh:mm:ss.sss UTC+9000"  で返す(年月日の区切り: "-" )
*/
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+9000";
}
// ▼ 使用例 ▼
// アウトプットバーに YYYY-MM-DD hh:mm:ss.sss UTC+9000 で出力する
OutputBar.Writeln( DateTime_ms() );
</source>
</source>

2018年12月9日 (日) 17:27時点における版

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+9000" の文字列で返すこともできます。

/**
 * 関数 DateTime(bar,utc)
 * 現在日時を日本時間  "YYYY-MM-DD hh:mm:ss UTC+9000"  の文字列で返す
 * 引数 bar は年月日の区切り文字 ( デフォルト: "/" )
 * 引数 utc は文字列  "UTC+9000"  の表示の設定用 ( 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+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(/[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+9000 で出力する
OutputBar.Writeln( DateTime() );


  • さらにログ用っぽく、ミリ秒つきバージョンも。
マクロ内の各処理の時刻を出力する場合、変数 d = new Date() は関数のなかに置いてください。
/* 
 * 関数 DateTime_ms()
 * 現在日時(ミリ秒つき)を  "YYYY-MM-DD hh:mm:ss.sss UTC+9000"  で返す(年月日の区切り: "-" )
 */
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+9000";
}

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