Skip to content

Commit

Permalink
improve formatDate function with keepLocalTime argument (superdesk#4578)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzonidoo committed Jul 24, 2024
1 parent 07043ac commit 34095b9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
34 changes: 26 additions & 8 deletions scripts/core/get-superdesk-api-implementation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,33 @@ export function getSuperdeskApiImplementation(
gettext: (message, params) => gettext(message, params),
gettextPlural: (count, singular, plural, params) => gettextPlural(count, singular, plural, params),
formatDate: formatDate,
formatDateTime: (date: Date) => {
return moment(date)
.tz(appConfig.default_timezone)
.format(appConfig.view.dateformat + ' ' + appConfig.view.timeformat);
formatDateTime: (date: Date, timezoneId?: string) => {
if (timezoneId != null) {
return moment(date)
.tz(timezoneId)
.format(appConfig.view.dateformat + ' ' + appConfig.view.timeformat);
} else {
const timezone: 'browser' | 'server' = appConfig.view.timezone ?? 'browser';
const keepLocalTime = timezone === 'browser';

return moment(date)
.tz(appConfig.default_timezone, keepLocalTime)
.format(appConfig.view.dateformat + ' ' + appConfig.view.timeformat);
}
},
longFormatDateTime: (date: Date | string) => {
return moment(date)
.tz(appConfig.default_timezone)
.format(appConfig.longDateFormat || 'LLL');
longFormatDateTime: (date: Date | string, timezoneId?: string) => {
if (timezoneId != null) {
return moment(date)
.tz(timezoneId)
.format(appConfig.view.dateformat + ' ' + appConfig.view.timeformat);
} else {
const timezone: 'browser' | 'server' = appConfig.view.timezone ?? 'browser';
const keepLocalTime = timezone === 'browser';

return moment(date)
.tz(appConfig.default_timezone, keepLocalTime)
.format(appConfig.longDateFormat || 'LLL');
}
},
getRelativeOrAbsoluteDateTime: getRelativeOrAbsoluteDateTime,
},
Expand Down
7 changes: 5 additions & 2 deletions scripts/core/superdesk-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2971,8 +2971,8 @@ declare module 'superdesk-api' {
gettext(message: string, params?: {[placeholder: string]: string | number | React.ComponentType}): string;
gettextPlural(count: number, singular: string, plural: string, params?: {[placeholder: string]: string | number | React.ComponentType}): string;
formatDate(date: Date | string): string;
formatDateTime(date: Date): string;
longFormatDateTime(date: Date | string): string;
formatDateTime(date: Date, timezoneId?: string): string;
longFormatDateTime(date: Date | string, timezoneId?: string): string;
getRelativeOrAbsoluteDateTime(
datetimeString: string,
format: string,
Expand Down Expand Up @@ -3244,6 +3244,9 @@ declare module 'superdesk-api' {
view: {
dateformat: string; // a combination of YYYY, MM, and DD with a custom separator e.g. 'MM/DD/YYYY'
timeformat: string;

// determines whether browser or server timezone is used for outputting date and time in user interface
timezone?: 'browser' | 'server'; // defaults to browser
};
user: {
sign_off_mapping?: string;
Expand Down

0 comments on commit 34095b9

Please sign in to comment.