Javascript dayjs我只想做一个数字

Javascript dayjs我只想做一个数字,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,Unix被传递到参数d。 我想用很多方法只得到数字,但我得到了一个错误 错误消息 TypeError:无法读取未定义的属性“匹配” const date = (d) => { const dayjs = require('dayjs'); const relativeTime = require('dayjs/plugin/relativeTime'); dayjs.extend(relativeTime); const now = Date.now()

Unix被传递到参数d。
我想用很多方法只得到数字,但我得到了一个错误

错误消息
TypeError:无法读取未定义的属性“匹配”

  const date = (d) => {
    const dayjs = require('dayjs');
    const relativeTime = require('dayjs/plugin/relativeTime');
    dayjs.extend(relativeTime);
    const now = Date.now();
    const day = dayjs(now - d).fromNow();

    console.log(day.result.match(/[0-9]*/g));
  };

无法读取未定义的属性“match”,因为结果未定义。看看day.js,它看起来像dayjs()。fromNow()直接在
day
const中返回字符串输出。要小心,因为它返回的数字不仅仅是以天为单位的,根据总天数,细分可以是小时/月/年,当您向下滚动时,您可以看到范围表

  const date = (d) => {
    const dayjs = require('dayjs');
    const relativeTime = require('dayjs/plugin/relativeTime');
    dayjs.extend(relativeTime);
    const now = Date.now();
    const day = dayjs(now - d).fromNow(true); //pass in true to remove the suffix 'ago'

    console.log(day.split(' ')[0]);
  };

我们所需要做的不是正则表达式匹配,而是将字符串按空格分割,并获取第一个元素,即2天前的数字
->
2

您无法读取未定义的属性“match”,因为结果未定义。看看day.js,它看起来像dayjs()。fromNow()直接在
day
const中返回字符串输出。要小心,因为它返回的数字不仅仅是以天为单位的,根据总天数,细分可以是小时/月/年,当您向下滚动时,您可以看到范围表

  const date = (d) => {
    const dayjs = require('dayjs');
    const relativeTime = require('dayjs/plugin/relativeTime');
    dayjs.extend(relativeTime);
    const now = Date.now();
    const day = dayjs(now - d).fromNow(true); //pass in true to remove the suffix 'ago'

    console.log(day.split(' ')[0]);
  };
我们所需要做的不是正则表达式匹配,而是将字符串按空格分割,并获取第一个元素,即2天前的数字
->
2