简体   繁体   English

借助moment.js实现漂亮的打印时间范围

[英]Pretty print time ranges with moment.js

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. 但是,我想对它们进行格式化,以便如果时间在小时上,则排除分钟,并且如果两个时间都在同一时间段内,则从第一时间开始类似地隐藏am / pm指示器。 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? 有没有其他方法可以使用moment或其他插件实现此行为?

Moment.js is a fantastic library, so it is nice to use a library which sits on top of it. Moment.js是一个很棒的库,因此最好使用位于它之上的库。 Twix.js should do the job for your needs. Twix.js应该可以满足您的需求。

http://isaaccambron.com/twix.js/ 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. 看起来可以使用很多修饰符,因此,如果您更关心时间范围而不是日期范围,则可以省略结果字符串的日期部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM