question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Help migrating from momentjs to dayjs (using startOf/endOf/add/substract)

See original GitHub issue

Hello, I’m unable to migrate my momentjs code to dayjs.

The job of my code is to have a timeframe depending of a cohort (for a line chart).

import moment from 'moment';
import dayjs from 'dayjs';

/* MOMENTJS CODE */

export const getFromDate = (cohort, subAmount = 0) => {
  switch (cohort) {
    case 'hours': return moment().startOf('hour').subtract(subAmount, 'hours');
    case 'days': return moment().startOf('day').subtract(subAmount, 'days');
    case 'weeks': return moment().startOf('isoWeek').subtract(subAmount, 'weeks');
    case 'months': return moment().startOf('month').subtract(subAmount, 'months');
    case 'years': return moment().startOf('year').subtract(subAmount, 'years');
    default: return null;
  }
};

export const getToDate = (cohort, addAmount = 0) => {
  switch (cohort) {
    case 'hours': return moment().endOf('hour').add(addAmount, 'hours');
    case 'days': return moment().endOf('day').add(addAmount, 'days');
    case 'weeks': return moment().endOf('isoWeek').add(addAmount, 'weeks');
    case 'months': return moment().endOf('month').add(addAmount, 'months');
    case 'years': return moment().endOf('year').add(addAmount, 'years');
    default: return null;
  }
};

/* DAYJS CODE */

export const getFromDate = (cohort, subAmount = 0) => {
  switch (cohort) {
    case 'hours': return dayjs().startOf('hour').subtract(subAmount, 'hour');
    case 'days': return dayjs().startOf('date').subtract(subAmount, 'date');
    case 'weeks': return dayjs().startOf('day').subtract(subAmount, 'day');
    case 'months': return dayjs().startOf('month').subtract(subAmount, 'month');
    case 'years': return dayjs().startOf('year').subtract(subAmount, 'year');
    default: return null;
  }
};

export const getToDate = (cohort, addAmount = 0) => {
  switch (cohort) {
    case 'hours': return dayjs().endOf('hour').add(addAmount, 'hour');
    case 'days': return dayjs().endOf('date').add(addAmount, 'date');
    case 'weeks': return dayjs().endOf('day').add(addAmount, 'day');
    case 'months': return dayjs().endOf('month').add(addAmount, 'month');
    case 'years': return dayjs().endOf('year').add(addAmount, 'year');
    default: return null;
  }
};

/* REST OF THE CODE */

export const getCohort = (timeAlias) => {
  switch (timeAlias) {
    case '24h': return 'hours';
    case '1s': return 'days'; // === 1 week
    case '1m': return 'weeks'; // === 1 month
    case '3m': return 'weeks';
    case '1a': return 'months'; // === 1 year
    default: return 'days';
  }
};

export const getTimeframe = (timeAlias) => {
  const cohort = getCohort(timeAlias);
  let subAmount;
  switch (timeAlias) {
    case '24h': subAmount = 23; break; // cohort = hours
    case '1s': subAmount = 7; break; // cohort = days
    case '1m': subAmount = 4; break; // cohort = weeks
    case '3m': subAmount = 4 * 3; break; // cohort = weeks
    case '1a': subAmount = 12; break; // cohort = months
    default: subAmount = 7;
  }
  return {
    fromDate: getFromDate(cohort, subAmount),
    toDate: getToDate(cohort),
  };
};

Here the results:

"1s" using Moment : fromDate=2020-02-19 ; toDate=2020-02-26
"1s" using Dayjs : fromDate=2020-02-25 ; toDate=2020-02-26

"1m" using Moment : fromDate=2020-02-03 ; toDate=2020-03-01
"1m" using Dayjs : fromDate=2020-02-22 ; toDate=2020-02-26

I’m unable to achieve the same result. Look on CodeSandbox for a better comprehension.

Do you know why I’m unable to get the same result as using moment ? I can alter the code but I need to get fromDate and toDate for 24 hours, 1 week, 1 month, 3 months and 1 year.

Thanks 😉

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
iamkuncommented, Mar 8, 2020
1reaction
iamkuncommented, Feb 27, 2020

PS: It could be a great idea to add week and isoWeek to the code like moment have. It’s quite useful 😉

we will

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating from MomentJS to DayJS - Medium
Our current codebase at WikiEduDashboard uses the MomentJS but I have been working to migrate it to the modern star — DayJS. DayJS...
Read more >
Optimize your Front End Applications by migrating from ...
Optimize your Front End Applications by migrating from Moment to Dayjs. Moment is great library for handling Date object but heavy as well....
Read more >
Why we switched from Moment.js to Day.js?
The migration process. Make sure that the service is 100% covered with unit tests; Check if all Moment.js API usages are available in...
Read more >
Changing from moment to Day.js — how, why and fixing vue ...
moment.js is an awesome date/time library that I have used in essentially every project I've worked on for the last decade.
Read more >
Why we switched from Moment.js to Day.js - ottonova.tech
The migration process. Make sure that the service is 100% covered with unit tests; Check if all Moment.js API usages are available in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found