I have this code but it displays first 3 letters of month in english. How can I change it to spanish?
if (secondsPast > 86400) {
var date = new Date(timestamp);
var currentDate = new Date(now);
var day = date.getDate();
var month = date.toDateString().match(/ [a-zA-Z]*/)[0].replace(' ', '');
var year = date.getFullYear() == currentDate.getFullYear() ? '' : ', ' + date.getFullYear();
return month + ' ' + day + ' ' + year;
}
You can use toLocaleDateString()
.
The toLocaleDateString() method returns a string with a language sensitive representation of the date portion of this date.
Please read more about it at moz wiki
Example:
var event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(event.toLocaleDateString('es-ES', options));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With