最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - toLocaleString() is not working in Android for React Native - Stack Overflow

matteradmin8PV0评论

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?

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?

Share Improve this question edited Nov 12, 2019 at 8:23 Anilkumar iOS Developer asked Nov 11, 2019 at 7:59 Anilkumar iOS DeveloperAnilkumar iOS Developer 3,76510 gold badges61 silver badges120 bronze badges 1
  • 1 Try to look at momentJS – DevAS Commented Nov 11, 2019 at 11:44
Add a ment  | 

2 Answers 2

Reset to default 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.

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.

Post a comment

comment list (0)

  1. No comments so far