javascript - If current time is between two times using moment - Stack Overflow

I'm trying to find if the current local time is between two other times using momentjs.I have the

I'm trying to find if the current local time is between two other times using momentjs.

I have the following code:

var currentTime = moment().format('YYYY-MM-DD HH:mm');

var prefix = 'YYYY-MM-DD ';
// the prefix is because moment expects a date prefix when parsing
var start_time = moment(prefix + '16:00').format('HH:mm');
var end_time = moment(prefix + '16:30').format('HH:mm');

if( moment(currentTime).isBetween(start_time,end_time) )
    alert('TRUE');
else
    alert('FALSE');

And now let's assume the time is currently 16:10, it should be alerting TRUE but it alerts FALSE.

Any ideas why this isn't working as intended. Is the formatting wrong?

I'm trying to find if the current local time is between two other times using momentjs.

I have the following code:

var currentTime = moment().format('YYYY-MM-DD HH:mm');

var prefix = 'YYYY-MM-DD ';
// the prefix is because moment expects a date prefix when parsing
var start_time = moment(prefix + '16:00').format('HH:mm');
var end_time = moment(prefix + '16:30').format('HH:mm');

if( moment(currentTime).isBetween(start_time,end_time) )
    alert('TRUE');
else
    alert('FALSE');

And now let's assume the time is currently 16:10, it should be alerting TRUE but it alerts FALSE.

Any ideas why this isn't working as intended. Is the formatting wrong?

Share Improve this question edited Oct 20, 2015 at 15:43 Cameron asked Oct 20, 2015 at 15:38 CameronCameron 28.9k102 gold badges289 silver badges490 bronze badges 3
  • basic debugging: have you looked at what values are in curTime, start_time, and end_time? – Marc B Commented Oct 20, 2015 at 15:43
  • you are paring different formats – webduvet Commented Oct 20, 2015 at 15:45
  • You can look following github./moment/moment/issues/1199 – Yunus Commented Mar 8, 2018 at 6:27
Add a ment  | 

2 Answers 2

Reset to default 4

Don't format the dates you get back from moment if you're planning on paring them.

format() returns a string, not a date/time-parable object.

var currentTime = moment();

var extra = moment().format('YYYY-MM-DD') + ' ';
var start_time = moment(extra + '16:00');
var end_time = moment(extra + '16:30');

if (moment(currentTime).isBetween(start_time, end_time))
  console.log('TRUE');
else
  console.log('FALSE');
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.10.6/moment.min.js"></script>

You don't need to format the date:

var currentTime = moment();

var prefix = moment();
// the prefix is because moment expects a date prefix when parsing
var start_time = prefix.add(16, 'hours');
var end_time = mprefix.add(16, 'hours').add(30, 'minutes');

if( moment(currentTime).isBetween(start_time,end_time) )
    alert('TRUE');
else
    alert('FALSE');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745064512a4609173.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信