Skip to content

Commit

Permalink
CMS-559: Add time zone to emergency-alert time fields (#1550)
Browse files Browse the repository at this point in the history
* CMS-559: Add time zone to emergency-alert time fields

* CMS-559: Format date with date-fns

* CMS-559: Add small fix

* CMS-559: Add timezone
  • Loading branch information
ayumi-oxd authored Dec 12, 2024
1 parent 78ea0d1 commit fe14fcc
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
"use strict";

const format = require('date-fns/format')
const utcToZonedTime = require('date-fns-tz/utcToZonedTime')

const formatDateToPacificTime = (dateString) => {
// Strapi returns the date in ISO format e.g. 2025-01-01T00:00:00.000Z
// Convert dateString to Pacific Time
const pacificTime = utcToZonedTime(dateString, 'America/Los_Angeles')
// Format the date in YYYY-MM-DD e.g. 2025-01-01
return format(pacificTime, 'yyyy-MM-dd')
}

module.exports = {
beforeCreate(event) {
const { data } = event.params
const date = new Date(data.createdAt)
const createdDate = date.toISOString().split('T')[0]
const createdDate = formatDateToPacificTime(data.createdAt)
if (data.isActive === true) {
data.activeDate = createdDate
}
},
beforeUpdate(event) {
const { data } = event.params
const date = new Date(data.updatedAt)
const updatedDate = date.toISOString().split('T')[0]
const updatedDate = formatDateToPacificTime(data.updatedAt)
if (data.isActive === true) {
data.activeDate = updatedDate
}
if (data.isActive === false && data.activeDate) {
data.inactiveDate = updatedDate
}
}
}
}

0 comments on commit fe14fcc

Please sign in to comment.