简体   繁体   中英

JavaScript adding suffix to today's date

I have been trying to get this code to work to have both the date and time and also output the suffix on the date, like July '3rd'. I have been trying to get the main date function try to call the suffix function, but I keep getting an error of undefined on output. Apologies I am sure it's something simple I missed, I am still learning all this.

The script:

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";
}

Any help would be greatly appreciated.

Just use 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> 

There's a little error in your code

var addSuffix = getTodayDate.dateSuffix;

Here you're trying to call the function/property . dateSuffix on the object getTodayDate .

getTodayDate is actually a variable holding the day as an integer. dateSuffix on the other hand is a function that expects a parameter in parantheses.

So simply change it to this:

var addSuffix = dateSuffix(getTodayDate);

Also, the following function already returns the day as a number

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

So you don't need to return it from the dateSuffix function.

Change

return i + "th";

to

return  "th";

Here's a working example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM