Sponsored link


JavaScriptの日付の取得
ここではJavaScriptでの日付の取得の関数を紹介します。
getDateLabel()と呼び出すだけで
2005/10/22(水) 10:20:32のような文字列を返してくれます。
Webアプリケーションで日付の値が自動入力では困るような場合に、使えるでしょう。
もちろん、余裕があれば、selectタグを使った方がいいです。
半角数字を入力してくださいと注意書きをしても
全角の日本語を入力する人がいるからです。
かといってselectタグを使うと作業が増えるため、このようなJavaScritpでの日付入力は有効です。
関数
■関数
現在の日付の文字列を取得します。
new Date()で日付Objectを作成したそこから、各データーを取り出し、桁あわせするだけです。
桁合わせしないと
2005/10/22(水) 10:20:32 2005/1/3(土) 3:2:5
とかなり美しくありません。
■to2String
単純に、桁あわせをします。
もっと美しいやり方があると思いますがわかりません。
<script language="JavaScript"> <!-- function getDateLabel(){ jDayString = new Array("日","月","火","水","木","金","土"); currentDateObject = new Date(); currentYear = currentDateObject.getYear() currentFullYear = currentDateObject.getYear()+1900 ; currentMonth = to2String(currentDateObject.getMonth() + 1); currentDate = to2String(currentDateObject.getDate()); currentDay = jDayString[currentDateObject.getDay()]; currentHour = to2String(currentDateObject.getHours()); currentMinute = to2String(currentDateObject.getMinutes()); currentSecond = to2String(currentDateObject.getSeconds()); dateString=currentFullYear+"/"+currentMonth+"/"+currentDate+"("+currentDay+")"+" "+currentHour+":"+currentMinute+":"+currentSecond; return dateString; } function to2String(value){ label=""+value; if(label.length<2){ label="0"+label; } return label; } // --> </script>

このworkは、クリエイティブ・コモンズ・ライセンスの下でライセンスされています。

