React Native 中的日期格式

Eri*_*ric 5 datetime android ios ecmascript-6 react-native

我尝试根据设备设置格式化日期时间值。无论我对模拟器设置或 xCode 方案进行更改,我总是会获得美国格式date.toLocaleDateString()

所以我尝试了不同的库(moment,react-native-localize,...)但相同,始终采用美国格式。

所以我尝试使用以下代码直接设置区域设置:

  const date = new Date(Date.UTC(2019, 11, 26, 14, 5, 0))
  const options = {
     dateStyle: 'medium',
     timeStyle: 'short',
  }
  console.log(date.toLocaleDateString('en-US', options))               // Dec 26, 2019, 3:05 PM
  console.log(date.toLocaleDateString('fr-FR', options))               // Dec 26, 2019, 3:05 PM
  console.log(new Intl.DateTimeFormat('en-US', options).format(date))  // Dec 26, 2019, 3:05 PM
  console.log(new Intl.DateTimeFormat('fr-FR', options).format(date))  // Dec 26, 2019, 3:05 PM
Run Code Online (Sandbox Code Playgroud)

我仍然得到相同的结果!

我该怎么做才能在“en-US”以外的其他语言环境中显示我的日期?

我不想自己硬核格式(“DD/MM/YYY HH:mm”),我想使用我设置的区域设置或更好的设备设置。

谢谢你的建议

Mat*_*ral 1

尝试下面的代码:

import Moment from 'moment';

render(){
    Moment.locale('en');
    var dt = '2016-05-02T00:00:00';
    return(<View> {Moment(dt).format('d MMM')} </View>) //basically you can do all sorts of the formatting and others
}
Run Code Online (Sandbox Code Playgroud)

这里有一个格式化示例:

moment("12-25-1995", "MM-DD-YYYY");
Run Code Online (Sandbox Code Playgroud)

您可以在这里查看 moment.js 官方文档https://momentjs.com/docs/

  • 这正是问题所在,输出应该是“13/12/19”,第一个位置是日期,而不是“12/13/19”。不考虑区域设置,无论您输入什么,都会得到相同的结果! (2认同)