DEV Community

Cover image for Using a DateHelper to avoid date issues
Tyler Steck
Tyler Steck

Posted on

Using a DateHelper to avoid date issues

Node.js DateHelper abstracts the date properties into a simple add or subtract days. The method also exposes a function to return the formatted date according to YYYY-MM-DD

class DateHelper {
  constructor(dateString?: string = null) {
    this.date = new Date(dateString);
  }

  public addDays(days): void {
    this.date.setDate(this.date.getDate() + days);
    return this;
  }

  removeDays(days): void {
    this.date.setDate(this.date.getDate() - days);
    return this;
  }

  getFormattedDate(): string {
    const year = this.date.getFullYear();
    const month = this.date.getMonth() + 1;
    const day = this.date.getDate();
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
  }
}
Enter fullscreen mode Exit fullscreen mode

To use this helper, simply create a new instance of the DateHelper class with a string date as the parameter:

const dateHelper = new DateHelper('2023-02-27');
Enter fullscreen mode Exit fullscreen mode

You can then add or remove days by calling the addDays or removeDays methods, respectively:

dateHelper.addDays(3); // add 3 days to the date
dateHelper.removeDays(2); // remove 2 days from the date
Enter fullscreen mode Exit fullscreen mode

Finally, you can get the formatted date string by calling the getFormattedDate method:

const formattedDate = dateHelper.getFormattedDate(); // returns "2023-03-02"
Enter fullscreen mode Exit fullscreen mode

Note that the getFormattedDate method returns the date in the format "YYYY-MM-DD". If you need a different format, you can modify the method accordingly.

Here's an example of how you could write Jest tests for the DateHelper class:

describe('DateHelper', () => {
  test('should add days to date', () => {
    const dateHelper = new DateHelper('2023-02-27');
    dateHelper.addDays(3);
    expect(dateHelper.getFormattedDate()).toBe('2023-03-02');
  });

  test('should remove days from date', () => {
    const dateHelper = new DateHelper('2023-02-27');
    dateHelper.removeDays(2);
    expect(dateHelper.getFormattedDate()).toBe('2023-02-25');
  });

  test('should format date correctly', () => {
    const dateHelper = new DateHelper('2023-02-27');
    expect(dateHelper.getFormattedDate()).toBe('2023-02-27');
  });
});
Enter fullscreen mode Exit fullscreen mode

In this test suite, we have three tests:

  1. The first test checks that the addDays method correctly adds days to the date and returns the expected formatted date.
  2. The second test checks that the removeDays method correctly removes days from the date and returns the expected formatted date.
  3. The third test checks that the getFormattedDate method returns the expected formatted date.
 PASS  src/date-helper.test.js
  DateHelper
    ✓ should add days to date (3 ms)
    ✓ should remove days from date
    ✓ should format date correctly (1 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Enter fullscreen mode Exit fullscreen mode

Top comments (0)