gpt4 book ai didi

JavaScript 添加后缀到今天的日期

转载 作者:行者123 更新时间:2023-12-01 00:52:18 26 4
gpt4 key购买 nike

我一直在尝试让这段代码同时具有日期和时间,并输出日期的后缀,例如 7 月“3 日”。我一直在尝试让主日期函数尝试调用后缀函数,但我一直在输出中收到未定义的错误。抱歉,我确信我错过了一些简单的事情,我仍在学习这一切。

脚本:

function todayDateTime() {

var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var todayDate = new Date();
var getCurrentHours = todayDate.getHours();
var getCurrentMinutes = todayDate.getMinutes();
var getCurrentAmPm = getCurrentHours >= 12 ? 'PM' : 'AM';
var getTodayMonth = todayDate.getMonth()+1;
var getTodayDate = todayDate.getDate();

var addSuffix = getTodayDate.dateSuffix;

var getTodayFullYear = todayDate.getFullYear();
getCurrentHours = getCurrentHours % 12;
getCurrentHours = getCurrentHours ? getCurrentHours : 12;
getCurrentMinutes = getCurrentMinutes < 10 ? '0'+getCurrentMinutes :
getCurrentMinutes;

var getCurrentDateTime = getCurrentHours + ':' + getCurrentMinutes + ' ' +
getCurrentAmPm + '<br />' + monthNames[getTodayMonth] + ' ' + getTodayDate +
addSuffix + ' ' + getTodayFullYear;


return(getCurrentDateTime);
}



function dateSuffix(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}

任何帮助将不胜感激。

最佳答案

您的代码中有一个小错误

var addSuffix = getTodayDate.dateSuffix;

这里您尝试调用对象 getTodayDate 上的函数/属性 .dateSuffix

getTodayDate 实际上是一个以整数形式保存日期的变量。另一方面,dateSuffix 是一个需要括号中参数的函数。

所以只需将其更改为:

var addSuffix = dateSuffix(getTodayDate);

此外,以下函数已经以数字形式返回日期

var getCurrentDateTime = getCurrentHours + ':' + getCurrentMinutes + ' ' + 
getCurrentAmPm + '<br />' + monthNames[getTodayMonth] + ' ' + getTodayDate +
addSuffix + ' ' + getTodayFullYear;

因此您不需要从 dateSuffix 函数返回它。

改变

return i + "th";

return  "th";

这是一个工作示例:

function todayDateTime() {

var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var todayDate = new Date();
var getCurrentHours = todayDate.getHours();
var getCurrentMinutes = todayDate.getMinutes();
var getCurrentAmPm = getCurrentHours >= 12 ? 'PM' : 'AM';
var getTodayMonth = todayDate.getMonth() + 1;
var getTodayDate = todayDate.getDate();
var addSuffix = dateSuffix(getTodayDate);

var getTodayFullYear = todayDate.getFullYear();
getCurrentHours = getCurrentHours % 12;
getCurrentHours = getCurrentHours ? getCurrentHours : 12;
getCurrentMinutes = getCurrentMinutes < 10 ? '0' + getCurrentMinutes :
getCurrentMinutes;

var getCurrentDateTime = getCurrentHours + ':' + getCurrentMinutes + ' ' +
getCurrentAmPm + '<br />' + monthNames[getTodayMonth] + ' ' + getTodayDate +
addSuffix + ' ' + getTodayFullYear;


return (getCurrentDateTime);
}



function dateSuffix(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return "st";
}
if (j == 2 && k != 12) {
return "nd";
}
if (j == 3 && k != 13) {
return "rd";
}
return "th";
}

console.log(todayDateTime());

关于JavaScript 添加后缀到今天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56878540/

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