javascript の Date に Delphi の FormatDateTime みたいなのを追加
javascript の Date に Delphi の FormatDateTime みたいなのを追加してみた
var aaa = new Date(); aaa.format("yyyy/mm/dd(aaa) hh:nn:ss"); aaa.format("yyyy/mm/dd(aaa) 午前/後 h:nn:ss");日本の元号系(gggとか)、エスケープ(ダブル、シングルコーテーションで括る)は、実装されてませんので、文章に入れ込むときは細切れに使ってください 非標準で「前/後」というのを追加してます
Date.prototype.format = function(s) { var jweek = new Array("日", "月", "火", "水", "木", "金", "土"); var eweek = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); var emonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); var ampm = s.match(/am\/pm/ig); if( ampm != null ){ s = s.replace(/am\/pm/ig, "%ESC_WAMPM_ESC%"); } var ap = s.match(/a\/m/ig); if( ap != null ){ s = s.replace(/a\/p/ig, "%ESC_SAMPM_ESC%"); } s = s.replace(/前\/後/ig, "%ESC_JAMPM_ESC%"); s = s.replace(/dddd/g, "%ESC_WWEEK_ESC%"); s = s.replace(/ddd/g, "%ESC_SWEEK_ESC%"); s = s.replace(/mmmm/g, "%ESC_WMONTH_ESC%"); s = s.replace(/mmm/g, "%ESC_SMONTH_ESC%"); s = s.replace(/yyyy/g, String(this.getFullYear())); s = s.replace(/yy/g, String(this.getFullYear()).slice(-2)); s = s.replace(/mm/g, ('0' + (this.getMonth() + 1)).slice(-2)); s = s.replace(/dd/g, ('0' + this.getDate()).slice(-2)); s = s.replace(/m/g, String(this.getMonth() + 1)); s = s.replace(/d/g, this.getDate()); if( (this.getHours() > 11) && ((s.indexOf("%ESC_WAMPM_ESC%") >= 0) || (s.indexOf("%ESC_SAMPM_ESC%") >= 0) || (s.indexOf("%ESC_JAMPM_ESC%") >= 0)) ){ s = s.replace(/hh/g, ('0' + (this.getHours() - 12)).slice(-2)); s = s.replace(/h/g, String(this.getHours() -12)); }else{ s = s.replace(/hh/g, ('0' + this.getHours()).slice(-2)); s = s.replace(/h/g, this.getHours()); } s = s.replace(/nn/g, ('0' + this.getMinutes()).slice(-2)); s = s.replace(/n/g, this.getMinutes()); s = s.replace(/ss/g, ('0' + this.getSeconds()).slice(-2)); s = s.replace(/s/g, this.getSeconds()); s = s.replace(/zzz/g, ('00' + this.getMilliseconds()).slice(-3)); s = s.replace(/Z/g, this.getMilliseconds()); s = s.replace(/aaa/g, jweek[this.getDay()]); s = s.replace(/\%ESC_WWEEK_ESC\%/g, eweek[this.getDay()]); s = s.replace(/\%ESC_SWEEK_ESC\%/g, eweek[this.getDay()].substr(0, 3)); s = s.replace(/\%ESC_WMONTH_ESC\%/g, emonth[this.getMonth()]); s = s.replace(/\%ESC_SMONTH_ESC\%/g, emonth[this.getMonth()].substr(0, 3)); if( ampm != null){ for(var i = 0; i < ampm.length; i++){ s = s.replace(/\%ESC_WAMPM_ESC\%/, ampm[i].split("/")[Math.floor(this.getHours() / 12)]); } } if( ap != null){ for(var i = 0; i < ap.length; i++){ s = s.replace(/\%ESC_SAMPM_ESC\%/, ap[i].split("/")[Math.floor(this.getHours() / 12)]); } } if( (this.getHours() > 11) ){ s = s.replace(/\%ESC_JAMPM_ESC\%/g, '後'); }else{ s = s.replace(/\%ESC_JAMPM_ESC\%/g, '前'); } return s; }
コメント