Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

314
Vistas
moment js not showing local time it's showing same in the Database

how to display createdAt field date according to the local timezone. i have tried it's only working for localhost when I upload it on the server it's showing createdAt field data UTC format.

here is my code

dateFormatwithSec(date_time) {

    const testDateUtc = moment.utc(date_time);
    const localDate = moment(testDateUtc).local();
    return localDate.format('YYYY-MM-DD HH:mm:ss');
},

why it's not providing the right output on the server.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This isn't an answer, it's really an extended comment. The OP should include an example date_time string, the actual output and the expected output in the OP, not in comments.

Until that is known, no one can understand what "it's not providing the right output on the server" means.

Here is an explanation of what is happening, hopefully it helps.

Moment's utc method causes it to display timestamps as UTC. For parsing, it causes it to use UTC only if there is no offset in the timestamp. So given a timestamp like "2021-12-02 15:15:33" it will return a moment object with a Date that has a time value equivalent to 2021-12-02T15:15:33+00:00.

The local method causes moment to display the date in the local timezone. Note that "local" means according to system settings, not the actual location of the system.

Consider the following, where the Z token has been added to display the offset that moment is using for the objects:

function dateFormatwithSec (date_time) {

  // Set moment to UTC mode and parse the input string as UTC
  let testDateUtc = moment.utc(date_time);
  console.log('testDateUtc: ' + testDateUtc.format('YYYY-MM-DD HH:mm:ss Z'));

  // Set the moment object to local mode and return a formatted date
  let localDate = moment(testDateUtc).local();
  return localDate.format('YYYY-MM-DD HH:mm:ss Z');
}

let ts = '2021-12-02 15:15:33+04:00';
console.log('localDate  : '+ dateFormatwithSec(ts));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

PS. When parsing strings, it's always best to also supply the format, so:

moment.utc(date_time, 'YYYY-MM-DD HH:mm:ss')

is preferred. It also allows maintainers to see the expected format (though it should be in the documentation too). Even very small changes to the format will cause moment to fall back to the implementation's built–in parser, e.g. '2021-12-02 15:15:33+04:00' vs '2021-12-02 15:15:33 +04:00'.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I found a simple solution taking an idea from RobG. it's working for me perfectly. what I did.

 function dateFormatwithSec (date_time) {
      const dateutc = date_time.replace('Z', '');
        //  console.log(dateutc);
        const dt = new Date(`${dateutc}Z`);
        const finaldate = moment(dt).format('L, h:mm ss a');
        //display local time with moment 
         console.log( "local time is - " , finaldate)
                 return finaldate ;
}


let ts = '2021-12-03T18:34:17.000Z';
dateFormatwithSec(ts);

"local time is - ", "12/04/2021, 12:04 17 am"
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda