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

improve formatDate function with keepLocalTime argument #4578

Merged
merged 2 commits into from
Jul 24, 2024
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
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 @@ -489,15 +489,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 @@ -3050,8 +3050,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 @@ -3323,6 +3323,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
Loading