I'm trying to pare 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");
}
}
});
and here is my form
<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>
from console.log(today.format("hh:mm"))
i get this resullt 01:44
.
With my script above i always go to the else
, so is there any way to fix it ?
Here is my fiddle /
My Upated Question
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
I'm trying to pare 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");
}
}
});
and here is my form
<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>
from console.log(today.format("hh:mm"))
i get this resullt 01:44
.
With my script above i always go to the else
, so is there any way to fix it ?
Here is my fiddle https://jsfiddle/s9wfh9ye/33/
My Upated Question
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
Share
Improve this question
edited Aug 26, 2016 at 3:24
YVS1102
asked Aug 26, 2016 at 2:51
YVS1102YVS1102
2,7487 gold badges36 silver badges65 bronze badges
3
- 1 Possible duplicate of Moment js date time parison – Alexander O'Mara Commented Aug 26, 2016 at 2:57
-
moment('00:00', 'hh:mm')
returns a moment object where astoday.format("hh:mm")
returns a string so you should not be paring those 2 – Arun P Johny Commented Aug 26, 2016 at 3:00 -
2
@ArunPJohny: Don't pare strings for time. Compare integers instead. Use
.valueOf()
to convert moments to unix timestamp – slebetman Commented Aug 26, 2016 at 3:02
1 Answer
Reset to default 12To 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()
However, moment.js also provides methods that are more readable:
today.isBefore(moment('03:00', 'hh:mm'))
You can use .isBefore()
, .isAfter()
, .isSame()
, .isSameOrBefore()
etc. Read the documentation for more info: http://momentjs./docs/#/query/