APIで旧暦取得
ここに。。。
ここでは、なんちゃってprototype.jsを利用してObject.extend(クラスの継承)を利用します。
<script type="text/javascript">
var dateSample = new Date(); // 拡張されたDateオブジェクトを生成する
function test(num) {
switch (num) {
case 1: alert(dateSample.getWeekJP()); break; // 曜日を日本語で取得
case 2: alert(dateSample.YMDtmsJP()); break; // 年月日時刻を日本語で取得
case 3: alert(dateSample.YMDJP()); break; // 年月日を日本語で取得
case 6: alert(dateSample.getEraYJP()); break; // 年月日を日本語/和暦で取得
case 4: alert(dateSample.tmsJP()); break; // 時刻を日本語で取得
case 5: alert(dateSample.tmsAmPmJP()); break; // 時刻を12時間計の日本語で取得
default: alert("引数を確認してください");
}
}
</script>
以下に、Dateオブジェクト拡張のソースコードを記述します。
<script type="text/javascript" src="/AJAX/prototype.js" charset="utf-8"></script>
<script type="text/javascript">
var gWeeksEN = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var gWeeksJP = new Array("日", "月", "火", "水", "木", "金", "土");
Object.extend(Date.prototype, {
Version: '1.0.0',
Description: 'Dateオブジェクトの拡張',
// 曜日(日本語)を取得
getWeekJP: function () {
return gWeeksJP[this.getDay()];
},
// 年月日時刻(日本語)を返却
YMDtmsJP: function () {
var YMD = this.getYMD ();
var TMS = this.getTMS ();
return YMD[0]+"年"+YMD[1]+"月"+YMD[2]+"日("+this.getWeekJP()+") "+TMS[0]+"時"+TMS[1]+"分"+TMS[2]+"秒";
},
// 年月日(日本語)を返却
YMDJP: function () {
var YMD = this.getYMD ();
return YMD[0]+"年"+YMD[1]+"月"+YMD[2]+"日("+this.getWeekJP()+") ";
},
// 時刻(日本語)を返却
tmsJP: function () {
var TMS = this.getTMS ();
return TMS[0]+"時"+TMS[1]+"分"+TMS[2]+"秒";
},
// 時刻12(日本語)を返却
tmsAmPmJP: function () {
var TMS12 = this.getTMS12JP ();
return TMS12[3]+" "+TMS12[0]+"時"+TMS12[1]+"分"+TMS12[2]+"秒";
},
// 年月日を配列で取得
getYMD: function () {
return [this.getFullYear(), this.get0Month(), this.get0Date()];
},
// 時刻を配列で取得
getTMS: function () {
return [this.get0Hours(), this.get0Minutes(), this.get0Seconds()];
},
// 時刻を12時間変換した配列で取得
getTMS12JP: function () {
var t = (this.getHours() > 12) ? this.getHours()-12 : this.getHours();
t = (t >= 10)? t:"0"+t;
return [t, this.get0Minutes(), this.get0Seconds(), (this.getHours() > 12) ? "午後":"午前"];
},
// 2桁(0パディング)月の値を取得
get0Month: function () {
return (this.getMonth()+1 >= 10) ? this.getMonth()+1 : "0" + (this.getMonth()+1) ;
},
// 2桁(0パディング)日の値を取得
get0Date: function () {
return (this.getDate() >=10) ? this.getDate() : "0" + this.getDate() ;
},
// 2桁(0パディング)時の値を取得
get0Hours: function () {
return (this.getHours() >= 10) ? this.getHours() : "0" + this.getHours();
},
// 2桁(0パディング)分の値を取得
get0Minutes: function () {
return (this.getMinutes() >= 10) ? this.getMinutes() : "0" + this.getMinutes();
},
// 2桁(0パディング)秒の値を取得
get0Seconds: function () {
return (this.getSeconds() >= 10) ? this.getSeconds() : "0" + this.getSeconds();
}
});
</script>
ここに。。。