Commit 302d92ea authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Create function to format dates in a standard sortable format

parent 2b92198d
...@@ -2,6 +2,7 @@ import { ...@@ -2,6 +2,7 @@ import {
clearFormatters, clearFormatters,
decayingInterval, decayingInterval,
formatDate, formatDate,
formatDateTime,
formatRelativeDate, formatRelativeDate,
nextFuzzyUpdate, nextFuzzyUpdate,
} from '../time'; } from '../time';
...@@ -255,4 +256,16 @@ describe('sidebar/util/time', () => { ...@@ -255,4 +256,16 @@ describe('sidebar/util/time', () => {
); );
}); });
}); });
describe('formatDateTime', () => {
[
new Date(Date.UTC(2023, 11, 20, 3, 5, 38)),
new Date('2020-05-04T23:02:01+05:00'),
].forEach(date => {
it('returns right format for provided date', () => {
const expectedDateRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/;
assert.match(formatDateTime(date), expectedDateRegex);
});
});
});
}); });
...@@ -262,3 +262,16 @@ export function formatDate(date: Date, Intl?: IntlType): string { ...@@ -262,3 +262,16 @@ export function formatDate(date: Date, Intl?: IntlType): string {
Intl, Intl,
); );
} }
/**
* Formats a date as `YYYY-MM-DD hh:mm`, using 24h and system timezone.
*/
export function formatDateTime(date: Date): string {
const year = date.getFullYear();
const month = `${date.getMonth() + 1}`.padStart(2, '0');
const day = `${date.getDate()}`.padStart(2, '0');
const hours = `${date.getHours()}`.padStart(2, '0');
const minutes = `${date.getMinutes()}`.padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}`;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment