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

提供: MeryWiki
ナビゲーションに移動 検索に移動
Masme (トーク | 投稿記録)
細 「変換して出力され」→「変換して出力し」
Masme (トーク | 投稿記録)
Masme版マクロ更新
1行目: 1行目:
<div>__TOC__</div>
= n 版 =
= n 版 =
メモ帳のF5キーの再現です。いまの時刻をカーソル位置に挿入します。
メモ帳のF5キーの再現です。いまの時刻をカーソル位置に挿入します。
13行目: 11行目:
</source>
</source>


= masme 版 (2014/02) =
= masme 版 =
設定された書式(※下表参考)に従い、現在日時を挿入します。
書式設定(※下表参考)を元に現在日時を出力・挿入します。
{| class="wikitable"
{|class="wikitable"
! 書式設定の例 !! 出力結果の例
! 書式設定の例 !! 出力結果の例
|-
|-
| h:mm YYYY/MM/DD || 4:05 2001/02/03
|<code>h:mm YYYY/MM/DD</code> ||<code>4:05 2001/02/03</code>
|-
|-
| YYYY-MM-DD hh:mm:ss || 2001-02-03 04:05:06
|<code>YYYY-MM-DD hh:mm:ss</code> ||<code>2001-02-03 04:05:06</code>
|-
|-
| YY年M月D日(ddd) h時m分s秒 || 01年2月3日(土) 4時5分6秒
|<code>ddd, MMM D, YYYY</code> ||<code>Sat, Feb 3, 2001</code>
|-
|-
| d, MMM D, YYYY || Sat, Feb 3, 2001
|<code>YY年M月D日(d) h時m分s秒</code> ||<code>01年2月3日(土) 4時5分6秒</code>
|-
|<code>hhmm 'hhmm' h'hm'm &#x27;&#x27;</code> ||<code>0405 hhmm 4hm5 '</code>
|}
|}
* YY, YYYY, M, MM, MMM, D, DD, d, ddd, h, hh, m, mm, s, ss の文字列は、日時に変換して出力します。<br>それ以外の文字列はそのまま出力されます。


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


//■書式設定 ●初期値="h:mm YYYY/MM/DD"
//■書式設定 ●初期値="h:mm YYYY/MM/DD"
var format = "h:mm YYYY/MM/DD";
var format = "h:mm YYYY/MM/DD";


//■月の定義
var symbol = (function(){
var MMM = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
  var t = new Date(), pad = function(n){return n<10? "0"+n : n};
//■曜日の定義
  return {
var ddd = ["日","月","火","水","木","金","土"];
  "YYYY":  (t.getFullYear()),
var d = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
  "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 date = new Date();
var result = format.replace(/(')(.*?)\1|([YMDdhms])\3*/g,
Number.prototype.padding = function(){return 10>this? "0"+this: this};
   function($0,esc,ltr){return ltr || esc || symbol[$0] || $0;}
var result = format.replace(/([YMDdhms])\1*/g,function(match){
);
   switch(match){
  case "YYYY": return date.getFullYear();
  case "YY":  return(date.getFullYear()%100).padding();
  case "MMM":  return MMM[date.getMonth()];
  case "MM":  return(date.getMonth()+1).padding();
  case "M":    return date.getMonth()+1;
  case "DD":  return date.getDate().padding();
  case "D":    return date.getDate();
  case "ddd":  return ddd[date.getDay()];
  case "d":    return d[date.getDay()];
  case "hh":  return date.getHours().padding();
  case "h":    return date.getHours();
  case "mm":  return date.getMinutes().padding();
  case "m":    return date.getMinutes();
  case "ss":  return date.getSeconds().padding();
  case "s":    return date.getSeconds();
  default:    return match;
  }
});
Document.Write(result);
Document.Write(result);
</source>
</source>

2016年2月28日 (日) 11:50時点における版

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);
スポンサーリンク