Change the format of the date I receive from YYYY-MM-DD to (February 14, 2018)

1

I have a question.

You can change the date I receive from the BD with my javascript and show it as: "February 14, 2018", since I currently receive "2018-02-14"

this is my JS

$.ajax({
    url: 'datos.php',
    type: 'post',
    data: {fechas : id},
    dataType: 'json',
    success:function(response) {
        $("#Fecha_ini").html(response.fecha_ini);
        $("#Fecha_fin").html(response.fecha_fin);
    }
});
    
asked by Juan Carlos 15.02.2018 в 00:26
source

2 answers

3

This could be a solution

// uso el idioma en español
moment.locale('es');
//
var dateTime = moment( '2018-02-14');

var full = dateTime.format('MMMM D, YYYY');

console.log(full);
<!-- importo la libreria moments -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>
<!-- importo todos los idiomas -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment-with-locales.min.js"></script>
    
answered by 15.02.2018 в 00:49
0

You can do this, it's pretty rudimentary but it works for what you need

var fecha_ini = '2018-02-14';
var fechaPartida = fecha_ini.split('-');
var nombreMeses = ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
var nombreMes = nombreMeses[parseInt(fechaPartida[1])-1];
var fechaTratada = nombreMes +' '+ fechaPartida[2] + ' del ' + fechaPartida[0];

I hope it serves you.

    
answered by 15.02.2018 в 00:55