Skip to content

Commit

Permalink
add parseLocalDate
Browse files Browse the repository at this point in the history
  • Loading branch information
kangzj committed Feb 27, 2025
1 parent 7c1b26a commit 3218112
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion client/my-sites/stats/stats-chart-tabs/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import StatsEmptyState from '../stats-empty-state';
import StatsModulePlaceholder from '../stats-module/placeholder';
import StatTabs from '../stats-tabs';
import { parseLocalDate } from '../utils';
import ChartHeader from './chart-header';
import { buildChartData, getQueryDate } from './utility';

Expand All @@ -43,7 +44,7 @@ const transformChartDataToLineFormat = ( chartData ) => {
options: {},
data: chartData
.map( ( record ) => {
const date = new Date( record.data.period );
const date = parseLocalDate( record.data.period );
const value = record.data.views;
if ( isNaN( date.getTime() ) || typeof value !== 'number' ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useSubscribersQuery from 'calypso/my-sites/stats/hooks/use-subscribers-qu
import { useSelector } from 'calypso/state';
import StatsModulePlaceholder from '../stats-module/placeholder';
import StatsPeriodHeader from '../stats-period-header';
import { parseLocalDate } from '../utils';
import { hideFractionNumber } from './chart-utils';
import SubscribersNavigationArrows from './subscribers-navigation-arrows';
import type uPlot from 'uplot';
Expand Down Expand Up @@ -78,7 +79,7 @@ const transformLineChartData = (
const subscribersData: ChartDataPoint[] = [];
const paidSubscribersData: ChartDataPoint[] = [];
data?.map( ( point ) => {
const dateObj = new Date( point.period );
const dateObj = parseLocalDate( point.period );
if ( isNaN( dateObj.getTime() ) ) {
return null;
}
Expand Down
11 changes: 11 additions & 0 deletions client/my-sites/stats/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,14 @@ export const appendQueryStringForRedirection = ( pathname, query = {} ) => {

return `${ pathname }${ queryString ? '?' : '' }${ queryString }`;
};

/**
* Parse a date string into a Date object
* @param {string} dateString
* @returns
*/
export const parseLocalDate = ( dateString ) => {
const [ year, month, day ] = dateString.substring( 0, 10 ).split( '-' ).map( Number );
// Note: month is 0-indexed in JavaScript Date
return new Date( year, month - 1, day );
};

0 comments on commit 3218112

Please sign in to comment.