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

Fix date parsing when used in a title #16

Merged
merged 2 commits into from
Feb 12, 2025
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
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