diff --git a/assets/src/utils/functions.js b/assets/src/utils/functions.js --- a/assets/src/utils/functions.js +++ b/assets/src/utils/functions.js @@ -8,6 +8,7 @@ // utility functions import Cookies from 'js-cookie'; +import formatRelative from 'date-fns/formatRelative'; export function handleFetchError(response) { if (!response.ok) { @@ -146,8 +147,5 @@ export function getHumanReadableDate(data) { // Display iso format date string into a human readable date - // This is expected to be used by date field in datatable listing views - // Example: 3/24/2022, 10:31:08 AM - const date = new Date(data); - return date.toLocaleString(); + return formatRelative(new Date(data), new Date(), {addSuffix: true}); } diff --git a/cypress/integration/origin-save.spec.js b/cypress/integration/origin-save.spec.js --- a/cypress/integration/origin-save.spec.js +++ b/cypress/integration/origin-save.spec.js @@ -4,6 +4,7 @@ * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information */ +import formatRelative from 'date-fns/formatRelative'; let url; let origin; @@ -281,7 +282,7 @@ let i = 0; for (const row of rows) { const cells = row.cells; - const requestDateStr = new Date(this.originSaveJSON.data[i].save_request_date).toLocaleString(); + const requestDateStr = formatRelative(new Date(this.originSaveJSON.data[i].save_request_date), new Date(), {addSuffix: true}); const saveStatus = this.originSaveJSON.data[i].save_task_status; assert.equal($(cells[0]).text(), requestDateStr); assert.equal($(cells[1]).text(), this.originSaveJSON.data[i].visit_type); diff --git a/package.json b/package.json --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "d3": "^7.3.0", "datatables.net": "^1.11.5", "datatables.net-bs4": "^1.11.5", + "date-fns": "^2.28.0", "dompurify": "^2.3.6", "email-validator": "^2.0.4", "hex-rgb": "^5.0.0", diff --git a/yarn.lock b/yarn.lock --- a/yarn.lock +++ b/yarn.lock @@ -4125,6 +4125,11 @@ dependencies: jquery ">=1.7" +date-fns@^2.28.0: + version "2.28.0" + resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.28.0.tgz#9570d656f5fc13143e50c975a3b6bbeb46cd08b2" + integrity sha512-8d35hViGYx/QH0icHYCeLmsLmMUheMmTyV9Fcm6gvNwdw31yXXH+O85sOBJ+OLnLQMKZowvpKb6FgMIQjcpvQw== + dateformat@^4.5.1: version "4.6.3" resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-4.6.3.tgz#556fa6497e5217fedb78821424f8a1c22fa3f4b5"