3

I am trying to do some date format in my application.

Suppose I am getting date stamp like "1573457092953.63" And I am trying to do like below format.

"11/11/2019, 1:17:00"

So, I have used date.toLocaleString()

It is working fine in iOS, But, Getting issue in Android.

In iOS getting like 11/11/2019, 1:17:00 PM
Android getting like Mon Nov 11 1:17:00

How to fix this, Even I did not get anything from other forums.

Any suggestions?

2 Answers2

3

Unfortunately, toLocaleString() doesn't work on Android. Please, check the following link.

The workaround is to create a custom mapping and to use it:

const dateTimeMapping = {
        en: {
            onlyTime: 'h:mm A',
            shortDate: 'ddd, D MMMM',
            longDate: 'D MMMM YYYY h:mm A',
        },
        de: {
            onlyTime: 'H:mm',
            shortDate: 'ddd, D. MMMM',
            longDate: 'LLL',
        },
        it: {
            onlyTime: 'H:mm',
            shortDate: 'ddd D MMMM',
            longDate: 'D MMMM YYYY HH:MM',
        },
 }

Pass the locale property and the type of the format:

const result = dateTimeMapping[locale][type];

About the number formatting, you can read more here.

Hristo Eftimov
  • 13,845
  • 13
  • 50
  • 77
2

I found finally some library called

moment

"moment": "^2.24.0" // in package.json file

import Moment from 'moment';

Moment(date).format('DD/MM/YYYY, hh:mm:ss')
Output is 11/11/2019, 1:17:00

Hope this will help someone in future.

It is working fine for both iOS and Andorid domains.