我试图在日期/时间之前显示问候语,具体取决于当天的时间。
我有这个消息。
我还希望将日期和时间显示为“日期为TIME”。
每当我尝试使用时间日期代码更改某些内容时,我就无法再将其显示出来。
任何建议都会有所帮助。
function MakeArray(n) {
this.length = n;
}
monthNames = new MakeArray(13);
monthNames[1] = "January";
monthNames[2] = "February";
monthNames[3] = "March";
monthNames[4] = "April";
monthNames[5] = "May";
monthNames[6] = "June";
monthNames[7] = "July";
monthNames[8] = "August";
monthNames[9] = "September";
monthNames[10] = "October";
monthNames[11] = "November";
monthNames[12] = "December";
dayNames = new MakeArray(8);
dayNames[1] = "Sunday";
dayNames[2] = "Monday";
dayNames[3] = "Tuesday";
dayNames[4] = "Wednesday";
dayNames[5] = "Thursday";
dayNames[6] = "Friday";
dayNames[7] = "Saturday";
function dayPart(oneDate) {
var theHour = oneDate.getHours();
if (theHour < 12) {
return "Good morning";
}
if (theHour < 18) {
return "Good afternoon";
}
return "Good evening";
}
function customDateString(oneDate) {
var theDay = dayNames[oneDate.getDay() + 1],
theMonth = monthNames[oneDate.getMonth() + 1],
theYear = oneDate.getYear();
theYear += (theYear < 100) ? 1900 : 0;
return theDay + ", " + theMonth + " " + oneDate.getDate() + ", " + theYear;
}
var today = new Date();
alert(dayPart(today) + "." + customDateString(today));
上
答案 0 :(得分:0)
尝试此功能:
function customDateString(oneDate) {
var theDay = dayNames[oneDate.getDay() + 1]
var theMonth = monthNames[oneDate.getMonth() + 1]
var theYear = oneDate.getFullYear()
theYear += (theYear < 100) ? 1900 : 0
return 'It is ' + new Date().timeNow() + ' on ' + theMonth + " " + oneDate.getDate() + ", " + theYear + '. ';
}
使用这个原型:
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
getFullYear
会返回一个四位数的年份,而不仅仅是getYear
,例如,14
会返回2014
。