最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to format ISO date in React-Native - Stack Overflow

programmeradmin5浏览0评论

I am getting date data in this format from my API:

"2018-12-26T05:00:29"

however I need to display this in the application front end in a different format like this:

"Monday, Nov 26 at 10:00 am"

How can I achieve this in react-native?

I am getting date data in this format from my API:

"2018-12-26T05:00:29"

however I need to display this in the application front end in a different format like this:

"Monday, Nov 26 at 10:00 am"

How can I achieve this in react-native?

Share Improve this question edited Dec 27, 2018 at 2:41 Dacre Denny 30.4k5 gold badges51 silver badges66 bronze badges asked Dec 26, 2018 at 22:10 YenYen 211 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Consider using a third-party library like momentjs for advanced date parsing and formatting. Using moment, you can format the date string as required via the following pattern:

// dddd for full week day, MMM for abreviated month, DD for date, etc
moment(inputDate).format("dddd, MMM DD at HH:mm a")

The momentjs library works well with react-native and can be easily installed by:

npm install moment --save

and imported into your project:

import moment from 'moment';

Here's a snippet demonstrating the pattern shown above:

var inputDate = "2018-12-26T05:00:29";

var outputDate = moment(inputDate).format("dddd, MMM DD at HH:mm a");

console.log(outputDate);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.23.0/moment.min.js"></script>

While Moment.js can resolve this issue quickly, it's however a pretty big library to use for a single use case. I remend using date-fns instead. It's lite.

I e out of this thanks to https://github./date-fns/date-fns/issues/376#issuement-353871093.

Despite the solution, I did some perfection on Date Field Symbol. You may need to use dd instead of DD / yyyy instead of YYYY for formatting days of the month / for formatting years.

Read the doc here: https://github./date-fns/date-fns/blob/master/docs/unicodeTokens.md

发布评论

评论列表(0)

  1. 暂无评论