Add word "from" to date in momentsjs

4

How can I add the word "from" to the date using momentjs?

Example: 4 de Mayo de XXXX

The day, month and year give the same, the important thing is to add the word "of" in the date.

    
asked by Felipe 11.04.2018 в 18:15
source

5 answers

2

Using [], for example:

moment().format('[de] YYYY');     // de 2018
    
answered by 11.04.2018 / 18:21
source
6

Loading the locale for Spanish is that simple:

let now= moment();
console.log(now.format('LL'));
console.log(now.format('LLLL'));
//Personalizado, hay que poner entre corchetes el texto que no sea la fecha en sí misma
console.log(now.format('[Hoy es] dddd, DD [de] MMMM [de] YYYY [y son las] HH:mm'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/locale/es.js"></script>
    
answered by 11.04.2018 в 18:22
5

Here's the way I would do it, just change the language format, as nothing.

document.addEventListener("click", prueba);

function prueba(){

   var anio, mes, dia;
   anio = moment().format("YYYY");
   mes = moment().format("MMMM");
   dia = moment().format("dddd");
   alert(dia + " de " + mes + " de " + anio);
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/moment.min.js"></script>

<button onclick="prueba">Presionar</button>
    
answered by 11.04.2018 в 18:29
4

momentjs supports multiple formats according to the culture you want. link

moment().format('LL');   // 11 de abril de 2018

You only have to download the library with all the cultures and use the code that I give you above.

link

    
answered by 11.04.2018 в 18:22
3

A variation of the response of Pablo Lozano , you can have several languages and you decide which one to visualize, by default moment.js load the English language:

moment.locale('en');
let en= moment();
console.log(en.format('LL'));
console.log(en.format('LLLL'));


moment.locale('es');

let es= moment();
console.info(es.format('LL'));
console.info(es.format('LLLL'));

moment.locale('fr');

let fr= moment();
console.warn(fr.format('LL'));
console.warn(fr.format('LLLL'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/locale/es.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/locale/fr.js"></script>

Now with native Javascript it would be like this:

const dias_semana = ["domingo", "lunes", "martes", "miercoles", "jueves", "sabado"];

const meses = ["enero", "febrero","marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre"];


let mi_fecha = new Date();

console.log('Hoy es ${dias_semana[mi_fecha.getDay()]},${mi_fecha.getDate()} de ${meses[mi_fecha.getMonth()]} del ${mi_fecha.getFullYear()}')
    
answered by 11.04.2018 в 18:59