简体   繁体   English

天:小时:分钟:秒的 Moment.js 格式

[英]Moment.js format for Days:Hours:Minutes:Seconds

I've been tasked with making an input where the user can input days and hh:mm:ss in one field as dd:hh:mm:ss, but going through the Moment.js docs such a format doesn't appear to exist.我的任务是进行输入,用户可以在一个字段中输入天数和 hh:mm:ss 作为 dd:hh:mm:ss,但是通过 Moment.js 文档似乎不存在这种格式. Is there a way for me to make my own workaround?有没有办法让我自己解决?

Update:更新:

I found a way to format the string exactly how I want using duration: moment.duration( days + '.' + hours + ':' + minutes + ':' + sec).format('dd HH:mm:ss'));我找到了一种使用持续时间精确格式化字符串的方法: moment.duration( days + '.' + hours + ':' + minutes + ':' + sec).format('dd HH:mm:ss')); (it's ugly but I can fix this part later, what matters is it works). (这很丑,但我可以稍后修复这部分,重要的是它有效)。

I created the element to be displayed in html using:我使用以下方法创建了要在 html 中显示的元素:

const input = document.createElement('input'); input.setAttribute('data-inputmask', '"alias": "datetime", "placeholder": "_", "inputFormat": "dd HH:MM:ss"'); input.value = value;

This produces an input value that looks like this: 01 00:00:00 , however when I try to erase the date field ( 01 ), it cycles back to the last day of the previous month.这会产生一个如下所示的输入值: 01 00:00:00 ,但是当我尝试擦除日期字段( 01 )时,它会循环回到上个月的最后一天。

Example例子

I need the result to be __ 00:00:00 ( _ is my placeholder for empty values in the field) or I need to remove the ability to erase that field when the date is at 01我需要结果为__ 00:00:00_是字段中空值的占位符),或者我需要删除日期为01时擦除该字段的功能

Update 2:更新 2:

It seems like I can achieve the desired result by using input.setAttribute('data-inputmask', '"alias": "datetime", "placeholder": "_", "inputFormat": "yy HH:MM:ss"');看来我可以通过使用input.setAttribute('data-inputmask', '"alias": "datetime", "placeholder": "_", "inputFormat": "yy HH:MM:ss"'); , but is there a more elegant way of setting the inputFormat attribute? ,但是有没有更优雅的方法来设置 inputFormat 属性? I don't want to use yy to set my day value.我不想使用yy来设置我的日值。

You can manipulate the tokens that moment.js provides to display time and date as you desire like for your case:您可以操纵 moment.js 提供的令牌以根据您的情况显示时间和日期:

moment().format("dddd, h:mm:ss");

I have linked the displaying portion of the docs you can find more ways there as well.我已经链接了文档的显示部分,您也可以在那里找到更多方法。 More Info here更多信息在这里

You'll need to handle the days 'manual';您需要处理“手动”的日子;

After the input was altered;输入改变后;

  1. Remove the days删除天数
  2. Create momentjs object with the desired time使用所需时间创建 momentjs object
  3. Add the number of days to the input : .add(n, 'days')将天数添加到输入中.add(n, 'days')

 // 12 days from now @ 13:30:00 console.log(format('12:13:30:00')); // 120 days from now @ 12:11:11 console.log(format('120:12:11:11')); // -1 days (yesterday) @ 10:00:00 console.log(format('-1:10:00:00')); function format(input) { // Get number of days let days = input.substr(0, input.indexOf(':')); // Create momentjs let mom = new moment(input.replace(days, ''), 'HH:mm:SS') // Add days mom.add(days, 'days'); // Return human readable return mom.format('lll'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Provide your custom format as the second parameter ( format ) to create an momentjs object based on your user format.提供您的自定义格式作为第二个参数 ( format ) 以根据您的用户格式创建一个momentjs object。 momentjs String + Format ; momentjs 字符串 + 格式

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

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