gpt4 book ai didi

javascript - 如何在日期/时间范围内获取自定义消息?

转载 作者:行者123 更新时间:2023-11-28 08:31:44 25 4
gpt4 key购买 nike

我尝试在日期/时间之前显示问候语,具体取决于当天的时间。

  1. 早上好
  2. 下午好
  3. 晚上好

我有这条消息。

我还希望将日期和时间显示为“It is TIME on DATE”。

每当我尝试使用时间日期代码更改某些内容时,我都无法再让它显示出来。

任何建议都会有帮助。

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));

关于jsFiddle

最佳答案

尝试将此作为您的功能:

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 + '. ';

}

有了这个原型(prototype):

    Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}

Demo

getFullYear 返回四位数年份,而不仅仅是 getYear,例如,会返回 年份中的 14 2014 年

关于javascript - 如何在日期/时间范围内获取自定义消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21770235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com