0

Tengo el grid de paramquery que solo ordena la fecha tomando los primeros dígitos dd; ya intenté con dataType:"date" y format :"dd/mm/yyyy", pero sigue sin ordenarlas.

Esta fecha siempre la mantiene abajo (13/02/2018 02:55:00 PM) y 24/08/2017 09:40:00 AM la mantiene arriba.

1

1 respuesta 1

1
let fechas = ['13/02/2018 02:55:00 PM', '24/08/2017 09:40:00 AM',  '01/04/2011 10:55:00 AM'];

Array.prototype._w3skulls = function(){
    let res = new Array();
    for (let i = 0; i < this.length; i++) {
        let f = this[i][0].split('/');
        let aux = f[2];

        f[2] = f[0];
        f[0] = aux; 

        this[i][0] = f[0].concat('-' + f[1] + '-' + f[2]);

        let h  = this[i][1].split(':');

        if(this[i][2] == 'PM'){
            h[0] = parseInt(h[0]) + 12;
        }

        this[i][1] = h[0] + ':' + h[1] + ':' + h[2];
        res[i] = this[i][0] + ' '+ this[i][1];
    }
    return res;
};

function sortDates(fechas){
    let f_aux = fechas.map(function(elem, index, array) {
        return elem.split(' ');
    });

    let res = f_aux._w3skulls();

    let w3 = new Array();
    res.forEach(function(item, index){
        w3[index] = new Date(item);
    });

    w3.sort();
    for(let i = 0; i < w3.length; i++){
        let dia = '';  let mes = ''; let hora =''; let min = ''; let seg ='';
        let am = 'am';
        if(w3[i].getDate() < 10){
            dia = '0' + w3[i].getDate();
        }else{
            dia = w3[i].getDate();
        }

        if((w3[i].getMonth() + 1) < 10){
            mes = '0' + (w3[i].getMonth() + 1);
        }else{
            mes = (w3[i].getMonth() + 1);
        }

        if((w3[i].getHours() - 12) > 10){
            hora = '0' + w3[i].getHours();
            am = 'pm';
        }else{
            hora = w3[i].getHours();
        }

        if(w3[i].getMinutes() < 10){
            min = '0' + w3[i].getMinutes();
        }else{
            min =w3[i].getMinutes();
        }

        if(w3[i].getSeconds() < 10){
            seg = '0' + w3[i].getSeconds();
        }else{
            seg = w3[i].getSeconds();
        }

        w3[i] = dia + '/' + mes + '/' + w3[i].getFullYear() + ' ' + hora + ':' + min + ':' + seg + ' ' + am.toUpperCase();
    }
    return w3;
}

console.log(sortDates(fechas));
1
  • El script formatea los datos del array a un formato que pueda ser convertible en una fecha, tiene en cuenta los AM y PM. (_w3skulls). En un segundo paso, se ordena el array de fechas con sort(), para devolver más tarde el mismo array de origen pero ordenado por fecha ascendente. el 16 feb. 2018 a las 2:14

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.