1

I am using React Native to develop a mobile application.

I am getting from the backend a timestamp that I need to convert to a specific date format for Germany Munich timezone and I need to take care of the added hours in winter and summer.

Here is how my code is working right now

unixTime(unixtime) {
    var u = new Date(unixtime * 1000);
    return ('0' + u.getUTCDate()).slice(-2) +
        '.' + ('0' + (u.getUTCMonth() + 1)).slice(-2) +
        '.' + u.getUTCFullYear() +
        ' ' + ('0' + u.getUTCHours()).slice(-2) +
        ':' + ('0' + u.getUTCMinutes()).slice(-2) +
        ':' + ('0' + u.getUTCSeconds()).slice(-2)

},

How can I achieve that?

Update: Here is what I've found, can someone validate if it is correct?

u = u.toLocaleString("en-US", {timeZone: "Europe/Berlin"})
Amine
  • 2,241
  • 2
  • 19
  • 41
  • You can use moment.js for converting in localtime zone, please read this doc https://momentjs.com/docs/#/i18n/changing-locale/ – Lalit Singh Tanwar Oct 22 '20 at 14:23
  • Thank you, but the thing is that I don't want to use a whole library for just a simple conversion. Actually I found something and I don't know if it is valid, I'ill update my question – Amine Oct 22 '20 at 14:28

4 Answers4

1

How about using "toLocaleString()" methods?

let localizedString u.toLocaleString('de-DE', {timeZone: 'CET'});
//and then manipulate 'localizedString'
//
hoge
  • 82
  • 5
  • Thank you, please take a look at the update in my question, and tell me if this valid – Amine Oct 22 '20 at 14:34
  • It returns "11/21/2020, 11:08:08 PM" if u is initialized such as `new Date(2020,10,22,07,08,09)`. Is it what you would like to know? – hoge Oct 22 '20 at 14:42
  • For me it returns: 10/22/2020 6:24:14 , this sounds right, but I would like to have `06` in place of `6`, that that be achieved? – Amine Oct 22 '20 at 14:48
  • 1
    Do you want zero-padded strings? If so, I'm not certain, but standard library couldn't work for you.Maybe you can handle this problem if you code like this. http://www.enjoyxstudy.com/javascript/dateformat/dateformat.js – hoge Oct 22 '20 at 15:07
  • I ended up using `moment-timezone` – Amine Oct 22 '20 at 15:14
  • 1
    OK. Enjoy coding! – hoge Oct 22 '20 at 15:23
1

you should considering using a lib like moment https://momentjs.com/. code is easier to write, read and debug.

moment.locale('de');
const u = moment(unixtime).format('Y-m-d HH:mm:ss');

Wonkledge
  • 1,732
  • 7
  • 16
0

You can use Intl for this

npm install intl
new Intl.DateTimeFormat('en-GB', {
    timeZone: 'CEST'
  }).format(new Date(timestamp));
Haseeb A
  • 5,356
  • 1
  • 30
  • 34
  • 1
    Thank you, sorry I didn't mention it in the question, but I don't want to use a package for this – Amine Oct 22 '20 at 14:34
0

I ended up using moment-timezone which gave me the most accurate result

const u = moment.unix(unixtime).tz("Europe/Berlin").format('DD-MM-Y HH:mm:ss');
Amine
  • 2,241
  • 2
  • 19
  • 41
  • Please be aware of [Moment's project status](https://momentjs.com/docs/#/-project-status/). You may wish to choose a different library. – Matt Johnson-Pint Oct 22 '20 at 16:02