Skip to content

Commit

Permalink
Fix helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Jan 21, 2025
1 parent 2a64aee commit b2cdede
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions resources/Js/Handlebars/dateToLocalFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,41 @@
*/
module.exports = function(input, locale, options) {

// Set result
// Initialize the result with the input value
let result = input;

// Check input
if(typeof input === "string" && input && typeof locale === "string" && locale){

// convert to date
const date = new Date(input);

// Invalid date fallback
if(!isNaN(date)){

// Set options
const options = { weekday: 'long', day: 'numeric', month: 'long' };
// Check if input and locale are valid strings
if (typeof input === "string" && input && typeof locale === "string" && locale) {

// Set result
result = date.toLocaleDateString(locale.replace("_", "-"), options);

}

}
// Convert input to a Date object
const timestamp = new Date(input);

// Define weekday and month names for supported locales
const locales = {
en_US: {
weekdays: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
},
fr_FR: {
weekdays: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
months: ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"]
}
// Add more locales as needed
};

// Fallback to 'en_US' if the locale is not supported
const localeData = locales[locale] || locales["en_US"];

// Get the day of the week, day of the month, and month name
const weekday = localeData.weekdays[timestamp.getDay()];
const day = timestamp.getDate(); // Day of the month without leading zeros
const month = localeData.months[timestamp.getMonth()]; // Months are zero-based

// Combine and format the result
result = `${weekday} ${day} ${month}`;
}

// Return the result
return result;

};

0 comments on commit b2cdede

Please sign in to comment.