英文:
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 }) => {
const formattedDate = new Intl.DateTimeFormat(undefined).format(date);
return <Text>{formattedDate}</Text>;
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论