Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend generateDateTimePlusHours to accept optional initialDate #61

Merged
merged 3 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ date to add days/hours/minutes/seconds to that date:
const { dateTime } = require('js-automation-tools');

const pastDateTimePlusDay = dateTime.generateDateTimePlusDays(1, '2024-03-14T00:14:25'); // '2024-03-15T00:14:25'
const pastDateTimePlusHour = dateTime.generateDateTimePlusHours(1, '2024-03-14T00:14:25'); // '2024-03-14T01:14:25'
```
It will also write generated date and time to a global environment variable
`process.env.DATETIME` and `process.env.DATETIME_PLUS_DAYS`,
Expand Down
11 changes: 10 additions & 1 deletion tests/test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ console.log(
process.env.DATETIME_PLUS_HOURS
);

const pastDateTimePlusHour = dateTime.generateDateTimePlusHours(1, '2024-03-14T00:14:25');

console.log(
'pastDateTimePlusHour in process.env.DATETIME_PLUS_HOURS: ' +
`${pastDateTimePlusHour === process.env.DATETIME_PLUS_HOURS} ` +
process.env.DATETIME_PLUS_HOURS
);

const currentDateTimePlusMinute = dateTime.generateDateTimePlusMinutes(1);

console.log(
Expand Down Expand Up @@ -92,7 +100,8 @@ if (
}

if (
Date.parse(currentDateTime) > Date.parse(pastDateTimePlusDay)
(Date.parse(currentDateTime) > Date.parse(pastDateTimePlusDay)) &&
(Date.parse(pastDateTimePlusDay) > Date.parse(pastDateTimePlusHour))
) {
console.log('pastDateTimePlus Day/Hour/Minute/Second looks ok!');
} else {
Expand Down
13 changes: 9 additions & 4 deletions utils/date-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ let dateTime = {

return process.env.DATETIME_PLUS_DAYS;
},
/* eslint-disable max-len */
/**
* Generates current date and time plus number of hours (for example: '2024-03-14T01:14:25').
* @param {Number} hours number of hours that will be added to current date and time.
* Generates current (if no initial date is provided) date and time plus number of hours (for example: '2024-03-14T01:14:25').
* @param {Number} hours number of hours that will be added to current (if no initial date is provided) date and time.
* @param {String=} initialDate string with initial date and time that you want to add a number of hours to (for example: '2024-03-14T00:14:25').
* @returns {String} string with date and time plus number of hours (for example: '2024-03-14T01:14:25').
*/
generateDateTimePlusHours: function (hours) {
generateDateTimePlusHours: function (hours, initialDate) {
/* eslint-enable max-len */
const date = initialDate ? initialDate : Date.now();

process.env.DATETIME_PLUS_HOURS = new Date(
new Date(Date.now() + hours *
new Date(new Date(date).getTime() + hours *
(
_minutesInHour *
_secondsInMinute *
Expand Down
Loading