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.
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.
Using [], for example:
moment().format('[de] YYYY'); // de 2018
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>
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>
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()}')