【问题标题】:How to use Moment.JS to check whether a timestamp is between 2 other timestamps如何使用 Moment.JS 检查时间戳是否在其他 2 个时间戳之间
【发布时间】:2017-04-11 08:11:37
【问题描述】:

我有以下 JSON 数组(注意这些只是数组的第 5 和第 6 个元素):

[   
{ 
    Day: 'Mon',
    EndTime: '{ts \'1970-01-01 18:00:00\'}',
    StartTime: '{ts \'1970-01-01 16:30:00\'}',
    courseName: 'Computer Science 250: Introduction to Website Design',
    Credits: '4' 
},  
{   
    Day: 'Mon',
    EndTime: '{ts \'1970-01-01 18:30:00\'}',
    StartTime: '{ts \'1970-01-01 17:30:00\'}',
    courseName: 'Math 220: Differential Equations',
    Credits: '3' 
}
]

数组中的数据按“EndTime”的值排序。当我尝试检查对象在 i - 1 (18:00:00) 的结束时间是否在下一个对象的开始时间和结束时间之间(如果在 i 17:30:00 到 18:30: 00) 我应该得到 true,但 isBetween 方法却返回 false。

如何解决这个问题,我知道我犯了一些简单的错误?

这是我的代码:

for(let i = 1; i < monday.length-1; i++) {

const year = '1970-01-01';
const format = 'DD/MM/YYYY hh:mm:ss a';

  var next_endtime = monday[i].EndTime.substr(16, 8);
  var next_starttime = monday[i].StartTime.substr(16, 8);
  var prev_endtime = monday[i-1].EndTime.substr(16, 8);

  var plesson_e = moment(year + 'T' + prev_endtime, format), 
      nlesson_start = moment(year + 'T' + next_starttime, format), 
      nlesson_end = moment(year + 'T' + next_endtime, format);

  var testbool = moment(plesson_e).isBetween(nlesson_start, nlesson_end, 'time');

console.log(testbool);
}

【问题讨论】:

  • 我猜它永远不会运行任何 for 循环...如果数组 "monday" 的长度为 2,那么 1
  • 尝试将格式更改为'YYYY-MM-DD hh:mm:ss a';
  • @GiorgioBozio 对此表示感谢,我会为此设置条件

标签: javascript arrays json momentjs


【解决方案1】:

您传递给 moment 的字符串与您指定的格式不匹配:

const year = '1970-01-01'; // => see the '-', and the year is first
const format = 'DD/MM/YYYY hh:mm:ss a'; // => you put '/' and day first

尝试将格式值更改为:

const format = 'YYYY-MM-DD hh:mm:ss a';

this fiddle

【讨论】:

  • 知道这很简单,非常感谢,现在可以使用了
  • 有时候,两双眼睛胜过一只;)
【解决方案2】:

你传递给moment JS的日期时间或格式有问题,它对我有用:

const monday = [{
    Day: 'Mon',
    EndTime: '{ts \'1970-01-01 18:00:00\'}',
    StartTime: '{ts \'1970-01-01 16:30:00\'}',
    courseName: 'Computer Science 250: Introduction to Website Design',
    Credits: '4'
  },
  {
    Day: 'Mon',
    EndTime: '{ts \'1970-01-01 18:30:00\'}',
    StartTime: '{ts \'1970-01-01 17:30:00\'}',
    courseName: 'Math 220: Differential Equations',
    Credits: '3'
  }
]

const format = 'hh:mm:ss a';

var next_endtime = monday[1].EndTime.substr(16, 8);
var next_starttime = monday[1].StartTime.substr(16, 8);
var prev_endtime = monday[0].EndTime.substr(16, 8);

var plesson_e = moment(prev_endtime, format),
  nlesson_start = moment(next_starttime, format),
  nlesson_end = moment(next_endtime, format);

var testbool = moment(plesson_e).isBetween(nlesson_start, nlesson_end, 'time');

console.log(next_endtime, next_starttime, prev_endtime);
console.log(nlesson_end, nlesson_start, plesson_e);
console.log(testbool);
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"&gt;&lt;/script&gt;

【讨论】:

    猜你喜欢
    • 2021-12-14
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多