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

提供: MeryWiki
ナビゲーションに移動 検索に移動
ページの作成:「メモ帳のF5キーの再現です。いまの時刻をカーソル位置に挿入します。 <source lang="javascript"> // 日付と時刻を挿入.js d = new Date ...」
 
Masme (トーク | 投稿記録)
「masme版」追加。目次を表示。
1行目: 1行目:
<div>__TOC__</div>
= n 版 =
メモ帳のF5キーの再現です。いまの時刻をカーソル位置に挿入します。
メモ帳のF5キーの再現です。いまの時刻をカーソル位置に挿入します。


8行目: 11行目:


function p(x) { return x < 10 ? '0' + x : x }
function p(x) { return x < 10 ? '0' + x : x }
</source>
= masme 版 (2014/02) =
設定された書式(※下表参考)に従い、現在日時を挿入します。
{| class="wikitable"
! 書式設定の例 !! 出力結果の例
|-
| h:mm YYYY/MM/DD || 4:05 2001/02/03
|-
| YYYY-MM-DD hh:mm:ss || 2001-02-03 04:05:06
|-
| YY年M月D日(ddd) h時m分s秒 || 01年2月3日(土) 4時5分6秒
|-
| d, MMM D, YYYY || Sat, Feb 3, 2001
|}
* YY, YYYY, M, MM, MMM, D, DD, d, ddd, h, hh, m, mm, s, ss の文字列は、日時に変換して出力されます。<br>それ以外の文字列はそのまま出力されます。
<source lang="javascript">
//■日付と時刻
// 2014/02/12
//■書式設定 ●初期値="h:mm YYYY/MM/DD"
var format = "h:mm YYYY/MM/DD";
//■月の定義
var MMM = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
//■曜日の定義
var ddd = ["日","月","火","水","木","金","土"];
var d = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var date = new Date();
Number.prototype.padding = function(){return 10>this? "0"+this: this};
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);
</source>
</source>

2014年2月12日 (水) 22:55時点における版

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 版 (2014/02)

設定された書式(※下表参考)に従い、現在日時を挿入します。

書式設定の例 出力結果の例
h:mm YYYY/MM/DD 4:05 2001/02/03
YYYY-MM-DD hh:mm:ss 2001-02-03 04:05:06
YY年M月D日(ddd) h時m分s秒 01年2月3日(土) 4時5分6秒
d, MMM D, YYYY Sat, Feb 3, 2001
  • YY, YYYY, M, MM, MMM, D, DD, d, ddd, h, hh, m, mm, s, ss の文字列は、日時に変換して出力されます。
    それ以外の文字列はそのまま出力されます。
//■日付と時刻
// 2014/02/12

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

//■月の定義
var MMM = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
//■曜日の定義
var ddd = ["日","月","火","水","木","金","土"];
var d = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];

var date = new Date();
Number.prototype.padding = function(){return 10>this? "0"+this: this};
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);
スポンサーリンク