Skip to content

Latest commit

 

History

History
306 lines (225 loc) · 6.86 KB

File metadata and controls

306 lines (225 loc) · 6.86 KB

default

Usage

npx moment-dayjs-codemod default path/of/files/ or/some**/*glob.js

# or

yarn global add moment-dayjs-codemod
moment-dayjs-codemod default path/of/files/ or/some**/*glob.js

Local Usage

node ./bin/cli.js default path/of/files/ or/some**/*glob.js

Input / Output


basic

Input (basic.input.js):

import moment from 'moment';

const now = moment();
const day = moment('2021-05-28');

const date1 = moment('2019-07-11');
const date2 = moment('2019-07-10');
date1.diff(date2, 'years');
date1.diff(date2, 'days');

moment().seconds();
moment().seconds(30);

moment().hours();
moment().hours(13);

moment().date();
moment().date(6);

moment().day();
moment().day(-14);

moment().add(7, 'days');
moment().subtract(7, 'days');

moment.isDate(new Date());

Output (basic.output.js):

import dayjs from 'dayjs';

const now = dayjs();
const day = dayjs('2021-05-28');

const date1 = dayjs('2019-07-11');
const date2 = dayjs('2019-07-10');
date1.diff(date2, 'year');
date1.diff(date2, 'day');

dayjs().second();
dayjs().set('second', 30);

dayjs().hour();
dayjs().set('hour', 13);

dayjs().date();
dayjs().set('date', 6);

dayjs().day();
dayjs().set('day', -14);

dayjs().add(7, 'day');
dayjs().subtract(7, 'day');

dayjs(new Date()).isValid();

dayofyear

Input (dayofyear.input.js):

import moment from 'moment';
moment().dayOfYear();
moment().dayOfYear(277);

Output (dayofyear.output.js):

import dayjs from 'dayjs';
import dayOfYear from 'dayjs/plugin/dayOfYear';
dayjs.extend(dayOfYear);
dayjs().dayOfYear();
dayjs().dayOfYear(277);

format

Input (format.input.js):

import moment from 'moment';

moment().format('dddd, MMMM Do YYYY, h:mm:ss A');
moment().format('ddd, hA');

Output (format.output.js):

import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
dayjs.extend(customParseFormat);

dayjs().format('dddd, MMMM Do YYYY, h:mm:ss A');
dayjs().format('ddd, hA');

is-between

Input (is-between.input.js):

import moment from 'moment';

moment('2019–10–05').isBetween('2019–10–04', '2019–10–06');

Output (is-between.output.js):

import dayjs from 'dayjs';
import isBetween from 'dayjs/plugin/isBetween';
dayjs.extend(isBetween);

dayjs('2019–10–05').isBetween('2019–10–04', '2019–10–06');

isLeapYear

Input (isLeapYear.input.js):

import moment from 'moment';

moment([2020]).isLeapYear();

Output (isLeapYear.output.js):

import dayjs from 'dayjs';
import isLeapYear from 'dayjs/plugin/isLeapYear';
dayjs.extend(isLeapYear);

dayjs([2020]).isLeapYear();

isoWeeksInYear

Input (isoWeeksInYear.input.js):

import moment from 'moment';
moment().isoWeeksInYear();

Output (isoWeeksInYear.output.js):

import dayjs from 'dayjs';
import isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear';
dayjs.extend(isoWeeksInYear);
dayjs().isoWeeksInYear();

minmax

Input (minmax.input.js):

import moment from 'moment';

const array = [
  new Date(1996, 10, 06),
  new Date(1994, 6, 18),
  new Date(1993, 5, 25),
  new Date(1959, 10, 4),
];

moment.max(array.map((a) => moment(a)));
moment.min(array.map((a) => moment(a)));

Output (minmax.output.js):

import dayjs from 'dayjs';
import minMax from 'dayjs/plugin/minMax';
dayjs.extend(minMax);

const array = [
  new Date(1996, 10, 06),
  new Date(1994, 6, 18),
  new Date(1993, 5, 25),
  new Date(1959, 10, 4),
];

dayjs.max(array.map((a) => dayjs(a)));
dayjs.min(array.map((a) => dayjs(a)));

relativetime

Input (relativetime.input.js):

import moment from 'moment';

const a = moment('2000-01-01');
moment(1568751159000).fromNow();
moment(1568751159000).from(a);
moment(1568751159000).to(a);
moment(1568751159000).toNow();

Output (relativetime.output.js):

import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);

const a = dayjs('2000-01-01');
dayjs(1568751159000).fromNow();
dayjs(1568751159000).from(a);
dayjs(1568751159000).to(a);
dayjs(1568751159000).toNow();

utc-with-imports

Input (utc-with-imports.input.js):

import moment from 'moment';

moment('2019-07-12T15:37:01+02:00').utc().format();

Output (utc-with-imports.output.js):

import dayjs from 'dayjs';
import dayjsPluginUTC from 'dayjs-plugin-utc';
dayjs.extend(dayjsPluginUTC);

dayjs('2019-07-12T15:37:01+02:00').utc().format();

weekOfYear

Input (weekOfYear.input.js):

import moment from 'moment';

moment().week();
moment().week(41);

Output (weekOfYear.output.js):

import dayjs from 'dayjs';
import weekOfYear from 'dayjs/plugin/weekOfYear';
dayjs.extend(weekOfYear);

dayjs().week();
dayjs().week(41);