Skip to content

Commit

Permalink
Merge pull request #26 from tim-s-ccs/make-some-date-updates
Browse files Browse the repository at this point in the history
Make some small updates to add some date helpers
  • Loading branch information
tim-s-ccs authored Feb 10, 2022
2 parents d687c76 + 7c0c484 commit 3fbc33d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/models/activeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
29 changes: 29 additions & 0 deletions src/utils/dateHelpers.ts
Original file line number Diff line number Diff line change
@@ -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
8 changes: 0 additions & 8 deletions src/utils/getCurrentDate.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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'

export default {
addLeadingZeros: addLeadingZeros,
cast: cast,
formatDate: formatDate,
getCurrentDate: getCurrentDate,
dateHelpers: dateHelpers,
pluralise: pluralise,
numberToCurrency: numberToCurrency
}
4 changes: 2 additions & 2 deletions src/validation/validators/inputValidators/dateValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 3fbc33d

Please sign in to comment.