Skip to content

Commit

Permalink
Merge pull request #16 from PacificCommunity/fixDateParsing
Browse files Browse the repository at this point in the history
Fix date parsing when used in a title
  • Loading branch information
thhomas authored Feb 12, 2025
2 parents 7fd7199 + 136ecd7 commit c3c2e69
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/utils/parseDate.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

/**
* Parse Date to a UTC date
* @param dateStr
* @param dateStr
* @returns {Number}
*/
export const parseDate = (dateStr: string) => {
// test dateStr format
Expand Down
13 changes: 7 additions & 6 deletions lib/utils/parseTextExpr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ export const parseTextExpr = (textExpr: string, dimensions: any[]) : string =>
if (dimension.values.length > 1) {
if (dimension.id === 'TIME_PERIOD') {
// if concept is TIME_PERIOD, we use the first and last value
const date1Str = dimension.values[0][valueAttribute];
const date2Str = dimension.values[dimension.values.length - 1][valueAttribute];
const date1 = new Date(date1Str);
const date2 = new Date(date2Str);
const dateMin = new Date(Math.min(...dimension.values.map((item: any) => new Date(item["start"]))));
const dimValueMin = dimension.values.find((item: any) => new Date(item["start"]).getTime() === dateMin.getTime());
const dateMax = new Date(Math.max(...dimension.values.map((item: any) => new Date(item["end"]))));
const date1 = new Date(dateMin);
const date2 = new Date(dateMax);
let date1Text = `${date1.getFullYear()}`;
let date2Text = `${date2.getFullYear()}`;
if (date1Str.split('-').length >= 2) {
if (dimValueMin[valueAttribute].split('-').length >= 2) {
// if year and month is provided
date1Text = `${date1.toLocaleString('default', { month: 'short' })} ${date1Text}`;
date2Text = `${date2.toLocaleString('default', { month: 'short' })} ${date2Text}`;
}
if (date1Str.split('-').length >= 3) {
if (dimValueMin[valueAttribute].split('-').length >= 3) {
// if year, month and day is provided
date1Text = `${date1.getDay()} ${date1Text}`;
date2Text = `${date2.getDay()} ${date2Text}`;
Expand Down

0 comments on commit c3c2e69

Please sign in to comment.