Home > Enterprise >  React Native: get current date and time
React Native: get current date and time

Time:03-30

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?

CodePudding user response:

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

CodePudding user response:

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.

  • Related