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

Sukemaru (トーク | 投稿記録)
Sukemaru (トーク | 投稿記録)
→sukemaru 版: "UTC+9000"となっていたのを"UTC+0900"に修正。DateTime_JP()を追加
71行目: 71行目:


= sukemaru 版 =
= sukemaru 版 =
現在日時を日本時間 "YYYY/MM/DD hh:mm:ss" の文字列で返します。
現在日時を日本時間 '''"YYYY/MM/DD hh:mm:ss"''' の文字列で返します。


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


<source lang="javascript">
<source lang="javascript">
/**
/**
  * 関数 DateTime(bar,utc)
  * 関数 DateTime(bar,utc)
  * 現在日時を日本時間  "YYYY-MM-DD hh:mm:ss UTC+9000"  の文字列で返す
  * 現在日時を日本時間  "YYYY-MM-DD hh:mm:ss UTC+0900"  の文字列で返す
  * 引数 bar は年月日の区切り文字 ( デフォルト: "/" )
  * 引数 bar は年月日の区切り文字 ( デフォルト: "/" )
  * 引数 utc は文字列  "UTC+9000"  の表示の設定用 ( true / false )
  * 引数 utc は文字列  "UTC+0900"  の表示の設定用 ( true / false )
  * 引数を省略した場合は  "YYYY/MM/DD hh:mm:ss"  の文字列で返す
  * 引数を省略した場合は  "YYYY/MM/DD hh:mm:ss"  の文字列で返す
  */
  */
112行目: 112行目:
   Status = "現在の時刻 " + $now;
   Status = "現在の時刻 " + $now;


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


122行目: 122行目:




* マクロに組み込みやすく、引数なしのシンプルなバージョンも。
* マクロに組み込みやすいように、'''引数なし'''のシンプルなバージョンも。
<source lang="javascript">
<source lang="javascript">
/*  
/**
  * 関数 DateTime()
  * 関数 DateTime()
  * 現在日時を日本時間  "YYYY-MM-DD hh:mm:ss UTC+9000"  で返す(年月日の区切り: "-" )
  * 現在日時を日本時間  "YYYY-MM-DD hh:mm:ss UTC+0900"  で返す(年月日の区切り: "-" )
  * 末尾の"UTC+9000"  が不要なら、toTimeString → toLocaleTimeString
  * 末尾の"UTC+0900"  が不要なら、toTimeString → toLocaleTimeString
  */
  */
function DateTime(){
function DateTime(){
136行目: 136行目:


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




* さらにログ用っぽく、ミリ秒つきバージョンも。
* さらにログ用っぽく、'''ミリ秒つき'''バージョンも。
: マクロ内の各処理の時刻を出力する場合、変数 d = new Date() は関数のなかに置いてください。
: マクロ内の各処理の時刻を出力する場合、変数 d = new Date() は関数スコープのなかに置いてください。
<source lang="javascript">
<source lang="javascript">
/*  
/**
  * 関数 DateTime_ms()
  * 関数 DateTime_ms()
  * 現在日時(ミリ秒つき)を  "YYYY-MM-DD hh:mm:ss.sss UTC+9000"  で返す(年月日の区切り: "-" )
  * 現在日時(ミリ秒つき)を  "YYYY-MM-DD hh:mm:ss.sss UTC+0900"  で返す(年月日の区切り: "-" )
  */
  */
function DateTime_ms(){
function DateTime_ms(){
   var d = newDate();
   var d = newDate();
   var pad = function(str){return str.replace(/[0-9]+/g,function($0){return $0.length<2 ? "0"+$0 : $0;});}
   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";
   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+9000 で出力する
// アウトプットバーに YYYY-MM-DD hh:mm:ss.sss UTC+0900 で出力する
OutputBar.Writeln( DateTime_ms() );
OutputBar.Writeln( DateTime_ms() );
</source>
* '''"YYYY年M月D日 hh:mm:ss"''' バージョンも。
<source lang="javascript">
/**
* 関数 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() );
</source>
</source>
スポンサーリンク