admin管理员组文章数量:1289581
I have a date string that I want to format where the number is under the month:
Jul
6
and I've tried a few different ways to add a new line:
<Moment format="MMM[\n]d">{`${date}`}</Moment>
but the result I'm getting is:
Jul\n6
I have a date string that I want to format where the number is under the month:
Jul
6
and I've tried a few different ways to add a new line:
<Moment format="MMM[\n]d">{`${date}`}</Moment>
but the result I'm getting is:
Jul\n6
Share
Improve this question
edited Jul 6, 2018 at 20:51
Tholle
113k22 gold badges208 silver badges197 bronze badges
asked Jul 6, 2018 at 19:42
xyzcodexyzcode
2191 gold badge7 silver badges16 bronze badges
2
- I assume you're using npm react-moment – Chris Commented Jul 6, 2018 at 21:10
- that's correct. – xyzcode Commented Jul 6, 2018 at 22:05
2 Answers
Reset to default 8If you want to get Tholle's solution working with react-moment, you'll want to do the following:
<Moment style={{whiteSpace: "pre"}} format={"MMM[\n]d"}>{`${date}`}</Moment>
By putting your format within brackets, the [\n]
will have the desired effect.
You can add a line break by styling your element with white-space: pre;
.
document.getElementById('root').innerHTML = moment().format('MMM [\r\n] D')
#root {
white-space: pre;
}
<div id="root"></div>
<script src="https://unpkg./[email protected]/moment.js"></script>
本文标签: javascriptReactMomentjs date formatting with line breakStack Overflow
版权声明:本文标题:javascript - ReactMomentjs date formatting with line break - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741393693a2376248.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论