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

Create new presenter to format a date #403

Merged
merged 4 commits into from
Sep 4, 2023
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
15 changes: 15 additions & 0 deletions app/presenters/base.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ function formatLongDate (date) {
return date.toLocaleDateString('en-GB', { year: 'numeric', month: 'long', day: 'numeric' })
}

/**
* Formats a date into a human readable day, month, year and time string, for example, '12 September 2021 at 21:43:44'
*
* @param {Date} date The date to be formatted
*
* @returns {string} The date formatted as a 'DD MMMM YYYY at HH:MM:SS' string
*/
function formatLongDateTime (date) {
return date.toLocaleDateString(
'en-GB',
{ year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' }
)
}

/**
* Formats a number which represents a value in pounds as a money string, for example, 1149 as '1149.00'
*
Expand Down Expand Up @@ -114,6 +128,7 @@ module.exports = {
formatAbstractionPeriod,
formatChargingModuleDate,
formatLongDate,
formatLongDateTime,
formatNumberAsMoney,
leftPadZeroes
}
8 changes: 8 additions & 0 deletions test/presenters/base.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ describe('Base presenter', () => {
})
})

describe('#formatLongDateTime()', () => {
it('correctly formats the given date, for example, 12 September 2021 at 14:41:10', async () => {
const result = BasePresenter.formatLongDateTime(new Date('2021-09-12T14:41:10.511Z'))

expect(result).to.equal('12 September 2021 at 14:41:10')
})
})

describe('#formatNumberAsMoney()', () => {
const valueInPence = 1149.5

Expand Down