Skip to content

Commit 35c573f

Browse files
committed
Add parseDateAsUTC helper to convert MYSQL to ISO8601
1 parent a11561b commit 35c573f

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

client/dashboard/me/billing-monetize-subscriptions/monetize-item.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { __, sprintf } from '@wordpress/i18n';
99
import { isToday } from 'date-fns';
1010
import SiteIcon from '../../components/site-icon';
1111
import { Text } from '../../components/text';
12-
import { formatDate, getRelativeTimeString } from '../../utils/datetime';
12+
import { formatDate, getRelativeTimeString, parseDateAsUTC } from '../../utils/datetime';
1313

1414
export const MonetizeSubscriptionTerms = ( {
1515
subscription,
@@ -22,8 +22,8 @@ export const MonetizeSubscriptionTerms = ( {
2222
return <>{ __( 'Never expires' ) }</>;
2323
}
2424

25-
// Check if end_date is in the past (convert to ISO 8601 and append Z to parse as UTC)
26-
const endDate = new Date( subscription.end_date.replace( ' ', 'T' ) + 'Z' );
25+
// Check if end_date is in the past
26+
const endDate = parseDateAsUTC( subscription.end_date );
2727
const isExpired = endDate < new Date();
2828

2929
// Show "Expired" for past dates
@@ -44,14 +44,14 @@ export const MonetizeSubscriptionTerms = ( {
4444
{ subscription.renew_interval === null
4545
? // translators: %(date)s is the date the subscription expires. Format is LL (e.g. January 1, 2020).
4646
sprintf( __( 'Expires on %(date)s' ), {
47-
date: formatDate( new Date( subscription.end_date.replace( ' ', 'T' ) + 'Z' ), locale, {
47+
date: formatDate( endDate, locale, {
4848
dateStyle: 'long',
4949
} ),
5050
} )
5151
: // translators: %(amount)s is the renewal price, %(date)s is the date the subscription renews. Format is LL (e.g. January 1, 2020).
5252
sprintf( __( 'Renews at %(amount)s on %(date)s' ), {
5353
amount: formatCurrency( Number( subscription.renewal_price ), subscription.currency ),
54-
date: formatDate( new Date( subscription.end_date.replace( ' ', 'T' ) + 'Z' ), locale, {
54+
date: formatDate( endDate, locale, {
5555
dateStyle: 'long',
5656
} ),
5757
} ) }

client/dashboard/utils/datetime.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,17 @@ export function formatSiteYmd( date: Date ) {
303303
const day = String( date.getDate() ).padStart( 2, '0' );
304304
return `${ year }-${ month }-${ day }`;
305305
}
306+
307+
/**
308+
* Parse a date string as UTC, handling MySQL and ISO8601 formats.
309+
*/
310+
export function parseDateAsUTC( dateString: string ): Date {
311+
// Handle datetime without timezone: "YYYY-MM-DD HH:MM:SS" or "YYYY-MM-DDTHH:MM:SS" (optional microseconds)
312+
const match = /^(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2}:\d{2}(?:\.\d+)?)$/.exec( dateString.trim() );
313+
if ( match ) {
314+
return new Date( `${ match[ 1 ] }T${ match[ 2 ] }Z` );
315+
}
316+
317+
// Everything else - pass through unchanged
318+
return new Date( dateString );
319+
}

0 commit comments

Comments
 (0)