问题描述
我正在尝试将时间与momentJS进行比较.这是我的脚本
I'm trying to compare a time with momentJS. Here is My script
$( 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
,所以有什么办法可以解决它?
With my script above i always go to the else
, so is there any way to fix it ?
这是我的小提琴 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()
.例如:
To use ==
or <
or >
you need to convert the moment to a number. To do that use .valueOf()
. For example:
today.valueOf() < moment('03:00', 'hh:mm').valueOf()
但是,moment.js还提供了更具可读性的方法:
However, moment.js also provides methods that are more readable:
today.isBefore(moment('03:00', 'hh:mm'))
您可以使用.isBefore()
,.isAfter()
,.isSame()
,.isSameOrBefore()
等.请阅读文档以获取更多信息: http://momentjs.com/docs/#/query/
You can use .isBefore()
, .isAfter()
, .isSame()
, .isSameOrBefore()
etc. Read the documentation for more info: http://momentjs.com/docs/#/query/
这篇关于与MomentJS比较时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!