xyzcode
xyzcode

Reputation: 219

React/Momentjs date formatting with line break

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

Upvotes: 7

Views: 6686

Answers (2)

Chris
Chris

Reputation: 2806

If 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.

Upvotes: 8

Tholle
Tholle

Reputation: 112787

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.com/[email protected]/moment.js"></script>

Upvotes: 1

Related Questions