3

I'm trying to obtain the current date and time in react native

I have tried like:

const today = new Date()

and it prints me

"2022-03-30T13:13:04.154Z"

but the real mine time is

"2022-03-30T15:13:04.154Z"

with timezone it-IT

I have tried also to use moment but it gives me back the same results "2022-03-30T13:13:04.154Z"

How can I obtain the date and time with timezone?

Jack23
  • 1,368
  • 7
  • 32
  • Does this answer your question? [new Date() returns UTC time instead of local time](https://stackoverflow.com/questions/67780302/new-date-returns-utc-time-instead-of-local-time) – Thanhal P A Mar 30 '22 at 13:43
  • thank you for answer, I have tried this solution but it is the same, back of 2 hours – Jack23 Mar 30 '22 at 13:45

2 Answers2

2

ideally creating the date in the way you have done should work.

as for moment:

import moment from 'moment'

moment(today).format('LLLL');

considering that today is the constant new Date(); LLLL is one format, you can try other formats:

moment().format('LT');   // 7:06 PM
moment().format('LTS');  // 7:06:13 PM
moment().format('L');    // 03/30/2022
moment().format('l');    // 3/30/2022
moment().format('LL');   // March 30, 2022
moment().format('ll');   // Mar 30, 2022
moment().format('LLL');  // March 30, 2022 7:06 PM
moment().format('lll');  // Mar 30, 2022 7:06 PM
moment().format('LLLL'); // Wednesday, March 30, 2022 7:06 PM
moment().format('llll'); // Wed, Mar 30, 2022 7:07 PM

this is from the official moment js website

Eragon_18
  • 720
  • 7
  • 11
  • thank you for your answer, my problem is also with moment, the time is back of 2 hours respect my local time – Jack23 Mar 30 '22 at 13:39
0

Don't forget that the end character 'Z' means universal time and there is 2 hours difference between current local Italian time and 'Z' time.

sroumieux
  • 111
  • 1
  • 3