[英]How to Get Formatted Date String In React Native
In many apps we can see the date formatting like this.在许多应用程序中,我们可以看到这样的日期格式。
If it's today, then print hh:mm,
If it's yesterday, then print yesterday,
If it's in this week, then print day of week, (ex, Wednesday)
If it's in this year, then print MM/dd
else print YY/MM/dd
Something like this.像这样的东西。 How can I implement in React Native or javascript?如何在 React Native 或 javascript 中实现? Is there library for this?有图书馆吗?
Use library momentjs .使用库momentjs 。 There is function isSame
which compare given date with year,month or day.有函数isSame
将给定的日期与年、月或日进行比较。 Like喜欢
moment().isSame('2010-01-01', 'year'); // true
moment().isSame('2011-01-01', 'month'); // false, different year
moment().isSame('2010-02-01', 'day'); //
moment().isSame('2010-02-01', 'week'); //
In your case, 1st on在你的情况下,第一
const dateString = moment().isSame('2018-01-01', 'day') ? moment('2018-01-01 5:55').format('hh:mm') : else check for other condition
Better go from year to month to week and week to day.最好从一年到一个月到一周,一周到一天。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.