How to format dates in Ultra Ajax output

By default, dates are output like 12 November 2024. If you wish to change this, you can do so.

Currently, there’s no visual option for this, but you can add config to your wp-config.php to change the date format.

Here is the code which applies the code formatting:

if (!isNaN(date.getTime())) { // Check if the Date object is valid
	// Define the custom format or use a default
	var format = typeof SSS_DATE_FORMAT !== 'undefined' ? SSS_DATE_FORMAT : 'd F Y';

	// Function to format the date
	function formatDate(date, format) {
		const map = {
			F: date.toLocaleString('default', { month: 'long' }),  // Full month name
			M: date.toLocaleString('default', { month: 'short' }), // Short month name
			m: date.toLocaleString('default', { month: 'numeric' }), // month number
			d: date.getDate(),                                      // Day of the month
			j: date.getDate(),                                      // Day of the month, no leading zeros
			Y: date.getFullYear(),                                  // Full year
			y: date.getFullYear().toString().slice(-2),             // Last two digits of year
		};

		// Replace format tokens with actual date parts
		return format.replace(/F|M|m|d|j|Y|y/g, match => map[match]);
	}

	var formattedDate = formatDate(date, format);
	result_date = `<div class="sss_date">${formattedDate}</div>`;
}

So – if you want to change the date format to November 12, 2024 you would add the following definition to your wp-config.php:

define('SSS_DATE_FORMAT', 'F d, Y');

The following single-character items are available in the date format:

  • F – Full month name
  • M – Short month name
  • m – month number
  • d – day of the month including leading zero
  • j – day of the month with no leading zeros
  • Y – 4 digit year
  • y – 2 digit year

Be the first to comment and we'll reply right away.

Leave a reply

Super Speedy Plugins
Logo