フォームに作成日を表示する時に、「今日」「昨日」と表示し、それ以外は「月/日」を表示するJavaScriptを紹介します。
計算結果フィールドの値に以下を記述します
var today = new Date(); //今日 var created = @Created(); //作成日
//今日を設定 todayYear = today.getFullYear().toFixed(0); todayMonth = today.getMonth()+1; todayMonth = todayMonth.toFixed(0); todayDay = today.getDate().toFixed(0);
//昨日を設定 var yesterday = new Date(todayYear,todayMonth,todayDay-1); yesterdayYear = yesterday.getFullYear().toFixed(0); yesterdayMonth = yesterday.getMonth(); yesterdayMonth = yesterdayMonth.toFixed(0); yesterdayDay = yesterday.getDate().toFixed(0);
//作成日を設定 createdYear = created.getFullYear().toFixed(0); createdMonth = created.getMonth()+1; createdMonth = createdMonth.toFixed(); createdDay = created.getDate().toFixed(0);
if ( todayYear == createdYear && todayMonth == createdMonth && todayDay == createdDay ){ “今日”; } else if (yesterdayYear == createdYear && yesterdayMonth == createdMonth && yesterdayDay == createdDay ){ “昨日”; } else { //それ以外は月/日を表示する createdMonth + “/” + createdDay; } |