http://clonedoppelganger.net/javascript/DateFormatter.html 前は適当にブログに書いただけだったけれど、phpspot開発日誌で紹介されたこともあって結構アクセスがあるので、ちゃんと説明ページを一枚作ってみた。 どうぞご利用下さい。
http://clonedoppelganger.net/javascript/DateFormatter.html 前は適当にブログに書いただけだったけれど、phpspot開発日誌で紹介されたこともあって結構アクセスがあるので、ちゃんと説明ページを一枚作ってみた。 どうぞご利用下さい。
PHPのdate関数のフォーマットパターンと互換性のあるJavaScriptの日付フォーマットライブラリです。 ただし、全てのパターンが実装されているわけではありませんので、注意してください。PHPにない拡張機能として、日本語の曜日と旧暦の月名を表示することができます。 DateFormatter.jsをロードします。 <script type="text/javascript" src="./DateFormatter.js"></script> DateFormatterオブジェクトのformatメソッドでフォーマットを行います。例えば、現在の年月日時間を表示するときは以下のようにします。 var now = new Date(); alert(DateFormatter.format(now, "Y/m/d H:i:s")); 表示結果は「2007/01/15 16:30:12」とい
PHP互換のJavaScript日付フォーマット関数をもっとPHPっぽくするとこんな感じか。 function date(pattern, time) { var d = new Date(); if (time != null) { d.setTime(time); } return DateFormatter.format(d, pattern); } alert( date("Y/m/d H:i:s") ); mktimeに当たるものも必要だから中途半端だけど。ついでにDateに追加するバージョンはこんな感じ。 Date.prototype.format = function(pattern) { return DateFormatter.format(this, pattern); } var now = new Date(); alert( now.format("Y/m/d H
cloned.log - PHP互換のJavaScript日付フォーマット関数をモジュール化した PHPで使えるdate関数のJavaScript版。 次のようにphpのdate関数っぽく簡単に使えるようです。 var now = new Date(); DateFormatter.format(now, "Y/m/d(J) H:i:s") 通常、JavaScriptで日付を取る場合、JavaScriptでの日付取得 で紹介しているような方法を取る必要がありますが、PHPに慣れている人にとってはこちらの方が便利かもしれませんね。 関連エントリ PHPの関数をJavaScriptで使うことができる「p2js」
[追記3] モジュール化したので、こちらがおすすめです。 [追記2] id:spiritlooseさんの指摘を勘違いしてしまった私だけど、こいつはこのままにしておくことにする。一応、出来るだけfunctionを配列にしたり、クロージャを減らしてswitch文の中にロジックを書いたりして工夫はしてみたものの、ベンチとると現状の方が速いので(勿論、各メソッドをprototype化した方が高速だとは思うけれど)。 下のコードは一度に1000回以上ループするような局面ではあまり向いていないので、必要に応じて改変するのが良いでしょう。 [追記] id:spiritlooseさんの指摘がもっともだと思ったので、Dateに引っ付けたバージョンに変更した。うん。こっちの方が無駄がない感じ。今回の使用例はこんな感じになる。 var now = new Date(); alert( Date.format(n
This shop will be powered by Are you the store owner? Log in here
配列操作の比較表: Ruby, Python, JavaScript, Perl, C++ プログラムを書いていると、他のプログラミング言語の記憶とごっちゃになって、「配列の後ろに要素を追加するのは push だっけ、 append だっけ」などと混乱することがあります。特に Ruby, Python, JavaScript はコードの書き方が似ているので、この問題が起きがちです。 そこで、備忘録として、 Ruby, Python, JavaScript, Perl, C++ の配列操作の比較表を作りました。一番慣れている Ruby を基準にしています。間違いなどがあったらご指摘いただけると助かります。他の言語のもあるといいなあ。 Ruby (Array) Python (list) JavaScript (Array) Perl (@) C++ (std::vector)
J4P5: Javascript For PHP 5 J4P5 is a JavaScript interpreter written in PHP 5, that allows to run untrusted scripts in a sandbox on your server. It aims to implement most of Ecma-262 3d edition. PHP5上でかかれたJavascriptインタプリタ。 JavascriptのソースをPHP5で動かすことが出来るようです。 次のようなソースで、JavascriptのソースがPHP5上であまりにも簡単に動いてしまうからかなり驚き。 <?php /* * P4J5のサンプル */ // モジュールの読み込み include "js.php"; // Javascriptのコード記述し$codeに代入 $cod
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く