Question

I have two moment objects, one representing a start datetime and the other an end datetime.

At present, I'm displaying the time from these objects like this:

12:00pm - 12:30pm

I want to, however, format these such that if a time falls on the hour, the minutes are excluded and similarly hide the am/pm indicator from the first time, if both times are in the same period. For example:

12 - 1pm
10.30am - to 2.30pm

I have come across moment-range, but this doesn't look like it provides any nice formatting functions. Is there another way I can achieve this behaviour using moment or another plugin?

Était-ce utile?

La solution

Moment.js is a fantastic library, so it is nice to use a library which sits on top of it. Twix.js should do the job for your needs.

http://isaaccambron.com/twix.js/

From the "Smart Formatting" section on that page:

moment("1/25/1982 9:00 AM").twix("1/25/1982 11:00 AM").format();  //=> 'Jan 25, 1982, 9 - 11 AM'
moment("1/25/1982 9:00").twix("1/26/1982 13:00").format(); //=> 'Jan 25, 9 AM - Jan 26, 1 PM, 1982'

It looks like there are plenty of modifiers you can use, so that if you are more concerned with time-ranges rather than date-ranges, you can omit the day portion of the resulting string.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top