JavaScript在今天的日期后缀

时间:2019-07-03 22:14:09

标签: javascript

我一直在尝试使此代码能够同时具有日期和时间,并在日期上输出后缀,例如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";
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

只需使用moment.js

console.log(moment('2019-07-04').format('MMMM do'));
console.log(moment('2019-07-03').format('MMMM do'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>

答案 1 :(得分:1)

您的代码中有一个小错误

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