Skip to content

Instantly share code, notes, and snippets.

@zahnrodolfo
Last active January 24, 2019 16:22
Show Gist options
  • Save zahnrodolfo/73a2890510c18b568984d9a5be0bce76 to your computer and use it in GitHub Desktop.
Save zahnrodolfo/73a2890510c18b568984d9a5be0bce76 to your computer and use it in GitHub Desktop.
In here you have methods to add minutes, hours, days, and years to a date and the unit tests are in the spec file. No need to use external libraries. To use these service just add it to a module then call it.
import { TestBed } from '@angular/core/testing';
import { configModule } from '../../../../test/test.helper.spec';
import { DateHelperService } from './date-helper.service';
describe('Helper Service', () => {
let service: DateHelperService;
const date = new Date(Date.UTC(2019, 0, 9, 12, 25, 2));
configModule(TestBed, {
providers: [ DateHelperService ]
});
beforeAll(() => {
service = TestBed.get(DateHelperService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should return current date in milliseconds', () => {
// GIVEN
const currentDateInMilliseconds: number = Date.now();
const currentDateInMillisecondsFromService: number = service.getCurrentDateInMilliseconds();
// THEN
expect(currentDateInMillisecondsFromService).toBe(currentDateInMilliseconds);
});
it('should convert a date to its value in milliseconds', () => {
// GIVEN
const dateInMilliseconds = Date.UTC(2019, 0, 9, 12, 25, 2);
const dateInMillisecondsFromService: number = service.getDateInMilliseconds(date);
// THEN
expect(dateInMillisecondsFromService).toBe(dateInMilliseconds);
});
it('should add one minute to the a date', () => {
// GIVEN
const datePlusOneMinute = new Date(Date.UTC(2019, 0, 9, 12, 26, 2));
const datePlusOneMinuteFromService: Date = service.addMinutesToCurrentDate(date, 1);
// THEN
expect(datePlusOneMinuteFromService.getTime()).toBe(datePlusOneMinute.getTime());
});
it('should add one hour to the a date', () => {
// GIVEN
const datePlusOneHour = new Date(Date.UTC(2019, 0, 9, 13, 25, 2));
const datePlusOneHourFromService: Date = service.addHoursToCurrentDate(date, 1);
// THEN
expect(datePlusOneHourFromService.getTime()).toBe(datePlusOneHour.getTime());
});
it('should add one day to the a date', () => {
// GIVEN
const datePlusOneDay = new Date(Date.UTC(2019, 0, 10, 12, 25, 2));
const datePlusOneDayFromService: Date = service.addDaysToCurrentDate(date, 1);
// THEN
expect(datePlusOneDayFromService.getTime()).toBe(datePlusOneDay.getTime());
});
it('should add one year to the a date', () => {
// GIVEN
const datePlusOneYear = new Date(Date.UTC(2020, 0, 9, 12, 25, 2));
const datePlusOneYearFromService: Date = service.addYearsToCurrentDate(date, 1);
// THEN
expect(datePlusOneYearFromService.getTime()).toBe(datePlusOneYear.getTime());
});
});
import { Injectable } from '@angular/core';
@Injectable()
export class DateHelperService {
oneMinuteInMilliseconds = 60000;
oneHourInMinutes = 60;
oneDayInHours = 24;
getCurrentDateInMilliseconds() {
return Date.now();
}
getDateInMilliseconds(date) {
return Date.parse(date);
}
addMinutesToCurrentDate(currentDate: Date, extraMinutes: number) {
const currentDateInMilliseconds = this.getDateInMilliseconds(currentDate);
const extraMinutesInMilliseconds = extraMinutes
* this.oneMinuteInMilliseconds;
const newDate = new Date(currentDateInMilliseconds + extraMinutesInMilliseconds);
return newDate;
}
addHoursToCurrentDate(currentDate: Date, extraHours: number) {
const currentDateInMilliseconds = this.getDateInMilliseconds(currentDate);
const extraHoursInMilliseconds = extraHours
* this.oneHourInMinutes
* this.oneMinuteInMilliseconds;
const newDate = new Date(currentDateInMilliseconds + extraHoursInMilliseconds);
return newDate;
}
addDaysToCurrentDate(currentDate: Date, extraDays: number) {
const currentDateInMilliseconds = this.getDateInMilliseconds(currentDate);
const extraDaysInMilliseconds = extraDays
* this.oneDayInHours
* this.oneHourInMinutes
* this.oneMinuteInMilliseconds;
const newDate = new Date(currentDateInMilliseconds + extraDaysInMilliseconds);
return newDate;
}
addYearsToCurrentDate(currentDate: Date, extraYears: number) {
const currentDateYear = currentDate.getFullYear();
const currentDateWithExtraYears = currentDate.setFullYear(currentDateYear + extraYears);
const newDate = new Date(currentDateWithExtraYears);
return newDate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment