Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display month in Spanish

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;

}
like image 216
Beto Cárdenas Avatar asked Mar 14 '18 05:03

Beto Cárdenas


1 Answers

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));
like image 89
AurA Avatar answered Sep 16 '22 22:09

AurA