使用date-fns根据设备的日期格式进行日期格式化。

huangapple go评论59阅读模式
英文:

Format date with date-fns depending on the devices date format

问题

可以根据用户设备的日期格式使用date-fns来格式化日期吗?例如,我的提供的代码在任何设备上都会显示04/29/2023,但是对于某些用户,他们可能希望根据设备上设置的日期格式显示,例如29/04/2023。如何根据用户设备的日期格式显示日期格式?

  <Text>
        {format(date, "P")}
  </Text>
英文:

Is it possible to format the date from date-fns based on the users devices date format?
for example my provided code always will show 04/29/2023 on any device, but for some users they would prefer format to show that is set in their device, for example 29/04/2023. How to show the date format depending on the users devices date formats?

  <Text>
        {format(date, "P")}
  </Text>

答案1

得分: 1

你可以使用JavaScript中的Intl对象来根据用户设备的区域设置格式化日期。以下是如何在React Native中使用它的示例:

const App = ({ date }) => {
  const formattedDate = new Intl.DateTimeFormat(undefined).format(date);

  return <Text>{formattedDate}</Text>;
};
英文:

You can use the Intl object from JavaScript to format dates according to the user's device locale. Here's an example of how you can use it in React Native:

const App = ({ date }) =&gt; {
  const formattedDate =  new Intl.DateTimeFormat(undefined).format(date);

  return &lt;Text&gt;{formattedDate}&lt;/Text&gt;;
};

huangapple
  • 本文由 发表于 2023年2月8日 16:35:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定