moment#unitOfTime TypeScript Examples

The following examples show how to use moment#unitOfTime. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: fieldFormatter.ts    From generator-earth with MIT License 6 votes vote down vote up
/**
 * ant-d date-picker disabledDate
 * https://github.com/ant-design/ant-design/blob/a6d545f9fa970e481e14bd33b31cb640cc086e40/components/date-picker/demo/disabled-date.md
 * https://github.com/ant-design/ant-design/blob/1735d89a66700ebb8c09eb94aa573b393ea14d23/components/date-picker/index.zh-CN.md
 *
 * 该方法将在before和after之外current全部disable (即return true的全部disable)
 */
export function disablePickerDate(type: unitOfTime.StartOf = 'day', beforeMonth = moment(), afterMonth = moment()) {
    return (current) => {

        let before = moment(beforeMonth).startOf(type)
        let after = moment(afterMonth).endOf(type)

        return current < before || current > after
    }
}
Example #2
Source File: stat.ts    From jitsu with MIT License 6 votes vote down vote up
function emptySeries(from: Moment, to: Moment, granularity: Granularity): DatePoint[] {
  let res: DatePoint[] = []
  let end = moment(to)
    .utc()
    .startOf(granularity as unitOfTime.StartOf)
  let start = moment(from)
    .utc()
    .startOf(granularity as unitOfTime.StartOf)
  while (end.isSameOrAfter(start)) {
    res.push({ date: moment(end), events: 0 })
    end = end.subtract(1, granularity as unitOfTime.DurationConstructor)
  }
  return res
}
Example #3
Source File: time.ts    From datart with Apache License 2.0 6 votes vote down vote up
export function getTimeRange(
  amount?: [number, number],
  unit?,
): (unitTime) => [string, string] {
  return unitOfTime => {
    const startTime = moment().add(amount?.[0], unit).startOf(unitOfTime);
    const endTime = moment().add(amount?.[1], unit).endOf(unitOfTime);
    return [startTime.format(TIME_FORMATTER), endTime.format(TIME_FORMATTER)];
  };
}
Example #4
Source File: time.ts    From datart with Apache License 2.0 6 votes vote down vote up
export function getTime(
  amount?: number | string,
  unit?: unitOfTime.DurationConstructor,
): (unitTime, isStart?: boolean) => Moment {
  return (unitOfTime: unitOfTime.StartOf, isStart?: boolean) => {
    if (!!isStart) {
      return moment().add(amount, unit).startOf(unitOfTime);
    }
    return moment().add(amount, unit).add(1, unit).startOf(unitOfTime);
  };
}
Example #5
Source File: ActionReportPage.tsx    From OpenVolunteerPlatform with MIT License 5 votes vote down vote up
DAY_UNIT_OF_TIME: unitOfTime.Base = "day"
Example #6
Source File: RelativeTimeSelector.tsx    From datart with Apache License 2.0 5 votes vote down vote up
RelativeTimeSelector: FC<
  {
    time?: TimeFilterConditionValue;
    onChange: (time) => void;
  } & I18NComponentProps
> = memo(({ time, i18nPrefix, onChange }) => {
  const t = useI18NPrefix(i18nPrefix);
  const [amount, setAmount] = useState(() => (time as any)?.amount || 1);
  const [unit, setUnit] = useState<unitOfTime.DurationConstructor>(
    () => (time as any)?.unit || 'd',
  );
  const [direction, setDirection] = useState(
    () => (time as any)?.direction || '-',
  );

  const handleTimeChange = (
    unit: unitOfTime.DurationConstructor,
    amount: number,
    direction: string,
  ) => {
    onChange?.({
      unit,
      amount,
      direction,
    });
  };

  const handleUnitChange = (newUnit: unitOfTime.DurationConstructor) => {
    setUnit(newUnit);
    handleTimeChange(newUnit, amount, direction);
  };

  const handleAmountChange = newAmount => {
    setAmount(newAmount);
    handleTimeChange(unit, newAmount, direction);
  };

  const handleDirectionChange = newDirection => {
    setDirection(newDirection);
    handleTimeChange(unit, amount, newDirection);
  };

  return (
    <StyledRelativeTimeSelector>
      <Select defaultValue={direction} onChange={handleDirectionChange}>
        {TIME_DIRECTION.map(item => (
          <Select.Option value={item.value}>{t(item.name)}</Select.Option>
        ))}
      </Select>
      {TIME_DIRECTION.filter(d => d.name !== 'current').find(
        d => d.value === direction,
      ) && (
        <InputNumber
          defaultValue={amount}
          step={1}
          min={1}
          onChange={handleAmountChange}
        />
      )}
      <Select defaultValue={unit} onChange={handleUnitChange}>
        {TIME_UNIT_OPTIONS.map(item => (
          <Select.Option value={item.value}>{t(item.name)}</Select.Option>
        ))}
      </Select>
    </StyledRelativeTimeSelector>
  );
})