Moment: Line Breaks in Formatting Don't Work

Created on 16 Apr 2018  ·  3Comments  ·  Source: moment/moment

If I say moment(myDatetime).format('something\nsomething else'), the line break is not interpreted as a line break. Doesn't matter if I surround \n with square brackets, add a space to either side of the square brackets, if I say
instead of \n, etc.

Most helpful comment

This sounds like an HTML/CSS issue, and nothing to do with moment.

moment is honouring the newline in formatting string when wrapped in []:

var formatted = moment().format('DD[\n]MM[\n]YYYY');
formatted.match(/\n/g).length; // 2 new-lines in output
formatted.split('\n'); // breaks into 3 lines / 3 array items
formatted.charCodeAt(2); // 10 - yep, it's a newline

All 3 comments

Where are you using the output? If it's in HTML, have you set the CSS white-space property to ensure the line-break is honoured?

I was doing that. I ended up doing this (TypeScript):

{moment(lastSaved).format('M/D/YY')} <br /> {moment(lastSaved).format('h:mm:ss a')}

This sounds like an HTML/CSS issue, and nothing to do with moment.

moment is honouring the newline in formatting string when wrapped in []:

var formatted = moment().format('DD[\n]MM[\n]YYYY');
formatted.match(/\n/g).length; // 2 new-lines in output
formatted.split('\n'); // breaks into 3 lines / 3 array items
formatted.charCodeAt(2); // 10 - yep, it's a newline
Was this page helpful?
0 / 5 - 0 ratings