javascript - 与 MomentJS 比较时间

标签 javascript jquery momentjs

我正在尝试将时间与 momentJS 进行比较。这是我的脚本

$( document ).ready(function() {
var today =moment();
console.log(today.format("hh:mm"));
    if((today.format('D') == (moment().day("Sunday").format('D')) || (today.format('D') == moment().day('Saturday').format('D'))))
        {
            $('.kompensasi').val("100,000");
            $('.chk').hide();
        }
    else{       
        if((today.format("hh:mm") > moment('00:00', 'hh:mm')) && (today.format("hh:mm") < moment('03:00', 'hh:mm')))
                {
                    $('.kompensasi').val("30,000");
                    $('.cekbok').val('');
                }else{
                    $('.cekbok').val('Dapet RO 1');
                    $('.kompensasi').val("0");
                }
        }
});

这是我的表格

<div class="col-sm-7">



            Kompensasi : <input name="dapet" type="text" readonly class="kompensasi" />
           </div>  
    </div>
             <div class="form-group chk">
                      <label class="col-sm-3 control-label">Ro</label>
                      <div class="col-sm-7">
                      <input type="text" class='cekbok' name='rostatus' />
                      </div>
            </div>

console.log(today.format("hh:mm")) 我得到了这个结果 01:44

使用上面的脚本我总是转到else,那么有什么办法可以修复它吗?

这是我的 fiddle https://jsfiddle.net/s9wfh9ye/33/

我更新的问题

    var today =moment();
    var after  = moment(today.format("hh:mm")).isAfter(moment('00:00', "hh:mm"));
    var before = moment(today.format("hh:mm")).isBefore(moment('03:00', "hh:mm"));

        today.format('hh:mm').valueOf() -->02:17

        moment('00:00', 'hh:mm').valueOf() --> 1472058000000

        moment('03:00', 'hh:mm').valueOf() -->1472068800000



   console.log(after); // false

console.log(before); // false

最佳答案

使用==<>您需要将时刻转换为数字。为此,请使用 .valueOf() .例如:

today.valueOf() < moment('03:00', 'hh:mm').valueOf()

但是,moment.js 还提供了更具可读性的方法:

today.isBefore(moment('03:00', 'hh:mm'))

您可以使用 .isBefore() , .isAfter() , .isSame() , .isSameOrBefore()等。阅读文档以获取更多信息:http://momentjs.com/docs/#/query/

关于javascript - 与 MomentJS 比较时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39157618/

相关文章:

javascript - 如何拒绝直接访问服务器中的 xml 文件

javascript - 如何在回调中访问正确的“this”?

javascript - 更改单选按钮上的产品图像

jquery - 鼠标移开时翻转停止

javascript - document.ready 和 append 动态表单项

javascript - 使用 jQuery.ajax 提交文件会出现 TypeError

javascript - 尝试使用 Moment 获取月份和年份范围

javascript - 使用 moment 将日期时间字符串格式转换为本地日期时间浏览器

javascript - 使用 moment.js (时区)时以 ("YYYY-MM-DD HH:mm") 格式返回日期和时间

javascript - 函数参数测试类型