Convert a string type date to the MongoDB ISODate format

0

I need to store the dates contained in a JSON object in a format that assures me that it will be valid in any scenario that is used and the chosen format is ISODate.

Initially I tried to create an object Date() and function .toISOString() for my object, but the results are not correct:

  • If I have a date as 02/01/2017 08:09:45 AM that corresponds to (January 2, 2017), the following happens:

    • If I use new Date(tramite.fecha_tramite) I get Wed Feb 01 2017 08:09:45 GMT-0600 (CST)
    • If I use new Date(tramite.fecha_tramite).toISOString() the result is 2017-02-01T14:09:45.000Z
  • If I have a date like 23/06/2017 02:58:36 PM (June 6, 2017) then I get an error:

    Invalid Date
    /Volumes/datos/Proyectos/convertir-tramites/index.js:40
        console.log('${new Date(tramite.fecha_afecta_ln).toISOString()}')
                                                         ^
    
    RangeError: Invalid time value
    

What I need to do is convert 02/01/2017 08:09:45 AM to 2017-01-02T14:09:45.000Z .

This is a typical JSON object with which I test:

{
    "FOLIO": "1738163100007",
    "estatus": "ENTREGADA",
    "causa_rechazo": "",
    "mov_solicitado": "CAMBIO DE DOMICILIO",
    "mov_definitivo": "CAMBIO DE DOMICILIO",
    "fecha_tramite": "02/01/2017 08:32:47 AM",
    "fecha_rec_cecyrd": "02/01/2017 08:42:40 AM",
    "fecha_reg_cecyrd": "02/01/2017 10:09:19 AM",
    "fecha_cancelado_mac": "",
    "fecha_rechazado": "",
    "fecha_cancelado_mov_post": "",
    "fecha_alta_pe": "02/01/2017 10:09:43 AM",
    "fecha_actual_pe": "02/01/2017 10:09:41 AM",
    "fecha_reinc_pe": "",
    "fecha_existoso": "02/01/2017 10:09:43 AM",
    "fecha_lote_prod": "02/01/2017 05:01:56 PM",
    "fecha_listo_reimp": "",
    "fecha_cpv_creada": "02/01/2017 05:01:42 PM",
    "fecha_registrada_mac": "09/01/2017 09:35:05 AM",
    "fecha_disponible": "09/01/2017 09:49:48 AM",
    "fecha_entregada": "23/01/2017 10:59:51 AM",
    "fecha_afecta_ln": "23/01/2017 02:58:36 PM"
}

And with these are my experiments:

console.log('La fecha pelona: ${tramite.fecha_tramite}')
console.log('Usando solo Date(): ${new Date(tramite.fecha_tramite)}')
console.log('Usando Date().toISOString(): ${new Date(tramite.fecha_tramite).toISOString()}')

In Python I can use the strftime function, but using JavaScript and node, how could I convert a date string type to an object with the required format?

    
asked by toledano 27.04.2017 в 04:17
source

1 answer

2

As Gustavo says, the best solution is MomentJS. While it is ALWAYS better to choose a solution vanilla JS , in the case of dates (especially for dates in non-US format) this is the best option. In Node and using node-moment (Node wrapper for moment ()):

const moment = require('node-moment');

//"fecha_alta_pe": "02javascript:void(0)/01/2017 10:09:43 AM"
console.log('Fecha ISO : ' + moment('02/01/2017 10:09:43 AM').toISOString());
//output -> 2017-02-01T10:09:43.000Z

Of course you must first install Moment via npm install node-moment --save-dev if you want to use just moment () (in a web page) you can download MomentJS from its official site.

    
answered by 27.04.2017 / 06:20
source