diff --git a/src/models/activeModel.ts b/src/models/activeModel.ts index 21e92ca..316a5aa 100644 --- a/src/models/activeModel.ts +++ b/src/models/activeModel.ts @@ -169,7 +169,7 @@ abstract class ActiveModel extends Model implements ActiveModelInterface { activeModelAttributes.forEach(activeModelAttribute => (this.data[activeModelAttribute] as ActiveModel)._save(req, dataInterface)) - if ('updatedAt' in this.modelSchema) this.data['updatedAt'] = utils.getCurrentDate() + if ('updatedAt' in this.modelSchema) this.data['updatedAt'] = utils.dateHelpers.getCurrentDateTimeString() dataInterface(req, this.tableName, this.data.id, this.attributes()) } diff --git a/src/utils/dateHelpers.ts b/src/utils/dateHelpers.ts new file mode 100644 index 0000000..8f63d25 --- /dev/null +++ b/src/utils/dateHelpers.ts @@ -0,0 +1,29 @@ +const getCurrentDateTime = (): Date => { + const currentDate = new Date() + const offset = currentDate.getTimezoneOffset() + return new Date(currentDate.getTime() - (offset*60*1000)) +} + +const getCurrentDateTimeString = (): string => { + return getCurrentDateTime().toISOString() +} + +const getCurrentDate = (): Date => { + const currentDate = getCurrentDateTime() + currentDate.setHours(0,0,0,0) + + return currentDate +} + +const getCurrentDateString = (): string => { + return getCurrentDate().toISOString() +} + +const dateHelpers = { + getCurrentDateTime: getCurrentDateTime, + getCurrentDateTimeString: getCurrentDateTimeString, + getCurrentDate: getCurrentDate, + getCurrentDateString: getCurrentDateString +} + +export default dateHelpers \ No newline at end of file diff --git a/src/utils/getCurrentDate.ts b/src/utils/getCurrentDate.ts deleted file mode 100644 index 71b4b0e..0000000 --- a/src/utils/getCurrentDate.ts +++ /dev/null @@ -1,8 +0,0 @@ -const getCurrentDate = (): string => { - let currentDate = new Date() - const offset = currentDate.getTimezoneOffset() - currentDate = new Date(currentDate.getTime() - (offset*60*1000)) - return currentDate.toISOString() -} - -export default getCurrentDate \ No newline at end of file diff --git a/src/utils/utils.ts b/src/utils/utils.ts index d18248c..37568ce 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,7 +1,7 @@ import addLeadingZeros from './addLeadingZeros' import cast from './cast' +import dateHelpers from './dateHelpers' import formatDate from './formatDate' -import getCurrentDate from './getCurrentDate' import numberToCurrency from './numberToCurrency' import pluralise from './pluralise' @@ -9,7 +9,7 @@ export default { addLeadingZeros: addLeadingZeros, cast: cast, formatDate: formatDate, - getCurrentDate: getCurrentDate, + dateHelpers: dateHelpers, pluralise: pluralise, numberToCurrency: numberToCurrency } \ No newline at end of file diff --git a/src/validation/validators/inputValidators/dateValidator.ts b/src/validation/validators/inputValidators/dateValidator.ts index 3a88df4..830e354 100644 --- a/src/validation/validators/inputValidators/dateValidator.ts +++ b/src/validation/validators/inputValidators/dateValidator.ts @@ -35,7 +35,7 @@ class DateValidator extends InputValidator { break case 'tommorow': onOrAfterDate = this.todayAtMidnight() - onOrAfterDate = new Date(onOrAfterDate.setDate(onOrAfterDate.getDate() + 1)) + onOrAfterDate.setDate(onOrAfterDate.getDate() + 1) break default: onOrAfterDate = new Date(this.options.onOrAfterDate) @@ -57,7 +57,7 @@ class DateValidator extends InputValidator { break case 'yesterday': onOrBeforeDate = this.todayAtMidnight() - onOrBeforeDate = new Date(onOrBeforeDate.setDate(onOrBeforeDate.getDate() - 1)) + onOrBeforeDate.setDate(onOrBeforeDate.getDate() - 1) break default: onOrBeforeDate = new Date(this.options.onOrBeforeDate)