isBetween使用moment-timezone

时间:2018-03-28 15:44:45

标签: javascript momentjs

我正在尝试使用时刻 - 时区来基于一小时进行一些计算。

var time = moment.tz() // get the now
var start = moment.tz('16:00:00', 'HH:mm:ss', '');
var end = moment.tz('16:10:00', 'HH:mm:ss', '');

if(time.isBetween(start, end)) {
  console.log('yep')
}

但输出始终为undefined

我已经尝试过无数种方法让它发挥作用,但无济于事。

moment.tz(moment.now(), 'Europe/Lisbon').isBetween('16:00:00','17:30:00') // to get in ms

moment.tz('Europe/Lisbon').isBetween('16:00:00','17:30:00')

moment.tz(moment.tz('Europe/Lisbon').format('HH:mm:ss'), 'Europe/Lisbon').isBetween('16:00:00','17:30:00')

我可能做错了什么? 我正在使用时区,因为我需要许多不同地方的实际小时。

谢谢你们

4 个答案:

答案 0 :(得分:2)

正如moment.tz文档所述:

  

moment.tz构造函数采用与构造函数相同的所有参数,但将最后一个参数用作time zone identifier

因此,您必须传递已识别的有效时区(例如'Europe/Lisbon'),而不是''

isBetween中的

'16:00:00''17:30:00'代表本地 '16:00:00'今天,Default部分状态。请改用moment.tz(String, String, String);来解析使用区域。

这里有一个片段,显示当前时间是否在区域中的给定范围之间时如何获得

function update() {
  var tmz = $("#tmz").val();
  var fmt = 'HH:mm:ss';
  
  var time = moment.tz(tmz);
  var start = moment.tz('16:00:00', fmt, tmz);
  var end = moment.tz('17:30:00', fmt, tmz);
  
  $(".time").text(time.format(fmt));
  $("#zone").text(tmz);
  if(time.isBetween(start, end)) {
    console.log('yep');
    $("#bool").text("IS");
  } else {
    $("#bool").text("IS NOT");
  }
}

$("#tmz").on("change", update);
update();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data-2012-2022.min.js"></script>

<select id="tmz">
  <option value="America/Los_Angeles">America/Los_Angeles</option>
  <option value="America/New_York">America/New_York</option>
  <option value="Europe/London">Europe/London</option>
  <option value="Europe/Rome">Europe/Rome</option>
  <option value="Asia/Tehran">Asia/Tehran</option>
  <option value="Asia/Kolkata">Asia/Kolkata</option>
  <option value="Australia/Sydney">Australia/Sydney</option>
</select>

<br/>
Now is <span class="time"></span> in <span id="zone"></span><br/>
<span class="time"></span> <span id="bool"></span> between 16:00:00 and 17:30:00

答案 1 :(得分:0)

在@VincenzoC的帮助下,我能够弄明白,以便将来参考或者试图找到最简单的解决方案的人是我用来测试它的代码。再次感谢@VincenzoC。

<强>码

var tmz = 'Europe/Lisbon';
var fmt = 'HH:mm:ss';
var time = moment.tz(tmz);
var start = moment.tz('12:00:00', fmt, tmz);
var end = moment.tz('15:00:00', fmt, tmz);

if(time.isBetween(start, end)) {
    console.log('Yes it is')
}

答案 2 :(得分:0)

这就是我在项目中解决此问题的方式。它也可以验证输入的日期。

代码:

const isDateInRange = ({
  format = 'HH:mm:ss',
  timeZone = 'Europe/Lisbon',
  start,
  end
} = {}) => {
  const dateStart = moment.tz(start, format, timeZone);
  const dateEnd = moment.tz(end, format, timeZone);

  if (!dateStart.isValid() || !dateEnd.isValid()) {
    return false;
  }

  return moment().isBetween(dateStart, dateEnd);
};

if (isDateInRange({ start: '10:00:00', end: '12:00:00' })) {
  console.log('Yep');
} else {
  console.log('Nope');
}
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.min.js"></script>

答案 3 :(得分:0)

Typescript函数涵盖了不同时区的前一天或后一天的边缘情况:

Plugin 'HerringtonDarkholme/yats.vim'
Plugin 'leafgarland/typescript-vim'

测试通过:

import moment from 'moment-timezone';

interface CurrentTimeIsBetweenTzTimeProps {
  currentTime?: any;
  tmz?: string;
  startTime?: string;
  endTime?: string;
  fmt?: string;
}
export const currentTimeIsBetweenTzTime = ({
  currentTime = new Date(),
  tmz = 'America/New_York',
  startTime = '09:00:00',
  endTime = '18:00:00',
  fmt = 'HH:mm:ss',
}: CurrentTimeIsBetweenTzTimeProps = {}) => {
  const time = moment.tz(currentTime, tmz);
  const start = moment.tz(startTime, fmt, tmz);
  const end = moment.tz(endTime, fmt, tmz);
  return time.isBetween(start, end)
    || time.isBetween(start.subtract(1, 'day'), end.subtract(1, 'day'))
    || time.isBetween(start.add(2, 'day'), end.add(2, 'day'));
}

或使用describe('currentTimeIsBetweenTzTime', () => { // Date Fri Oct 23 2020 15:06:40 GMT-0400 (Eastern Daylight Time) const timestamp = new Date(1603480000000) it('it is between start and end', () => { expect(currentTimeIsBetweenTzTime({ currentTime: timestamp, tmz: 'America/New_York', startTime: '15:00:00', endTime: '15:30:00', })).toBe(true); expect(currentTimeIsBetweenTzTime({ currentTime: timestamp, tmz: 'America/Chicago', startTime: '14:00:00', endTime: '14:30:00', })).toBe(true); expect(currentTimeIsBetweenTzTime({ currentTime: timestamp, tmz: 'Australia/Sydney', startTime: '06:00:00', endTime: '06:30:00', })).toBe(true); }); it('it is not between start and end', () => { expect(currentTimeIsBetweenTzTime({ currentTime: timestamp, tmz: 'America/Chicago', startTime: '15:00:00', endTime: '15:30:00', })).toBe(false); expect(currentTimeIsBetweenTzTime({ currentTime: timestamp, tmz: 'America/New_York', startTime: '14:00:00', endTime: '14:30:00', })).toBe(false); expect(currentTimeIsBetweenTzTime({ currentTime: timestamp, tmz: 'Australia/Sydney', startTime: '06:15:00', endTime: '06:30:00', })).toBe(false); }); });

dayjs