MCPcopy Create free account
hub / github.com/apitable/apitable / dateTimeFormat

Function dateTimeFormat

packages/core/src/model/field/date_time_base_field.ts:151–193  ·  view source on GitHub ↗
(
  timestamp: any,
  props?: IOptionalDateTimeFieldProperty,
  userTimeZone?: string,
)

Source from the content-addressed store, hash-verified

149};
150
151export const dateTimeFormat = (
152 timestamp: any,
153 props?: IOptionalDateTimeFieldProperty,
154 userTimeZone?: string,
155) => {
156 if (!timestamp) {
157 return null;
158 }
159
160 props = props || DEFAULT_DATETIME_PROPS;
161 const dateFormat = DateFormat[props.dateFormat || DEFAULT_DATETIME_PROPS.dateFormat];
162 const timeFormat = TimeFormat[props.timeFormat || DEFAULT_DATETIME_PROPS.timeFormat];
163 let format = dateFormat + (props.includeTime ? ' ' + timeFormat : '');
164
165 if (props.includeTime && timeFormat === 'hh:mm') {
166 const locale = getLanguage();
167 const formatLocale = locale.toLowerCase().split('_').join('-');
168 dayjs.locale(formatLocale);
169 format += ' a';
170 }
171 let timeZone = props.timeZone || userTimeZone;
172 if (isServer()) {
173 timeZone = timeZone || DEFAULT_TIME_ZONE;
174 }
175 try {
176 if (props.includeTimeZone) {
177 timeZone = timeZone || DEFAULT_DATETIME_PROPS.timeZone;
178 const abbr = getTimeZoneAbbrByUtc(timeZone)!;
179 return dfzFormatWithValid(utcToZonedTime(Number(timestamp), timeZone), covertDayjsFormat2DateFnsFormat(format), timeZone ) + ` (${abbr})`;
180 }
181 if (!props.includeTimeZone && timeZone) {
182 // dayjs(Number(timestamp)).tz(timeZone).format(format);
183 // Frequent invocations of the "tz" function here will result in severe page lag.
184 return dfzFormatWithValid(utcToZonedTime(Number(timestamp), timeZone), covertDayjsFormat2DateFnsFormat(format), timeZone );
185 }
186 } catch (e) {
187 if (e instanceof RangeError) {
188 return 'Invalid Date';
189 }
190 throw e;
191 }
192 return dfzFormatWithValid(timestamp, covertDayjsFormat2DateFnsFormat(format));
193};
194
195const withTimeZone = (timestamp: number | undefined | string, timeZone?: string, locale?: string) => {
196 if (timeZone) {

Callers 7

cellValueToStringMethod · 0.90
cellValueToStringMethod · 0.90
_compareFunction · 0.85
eqFunction · 0.85
cellValueToStringFunction · 0.85

Calls 7

getLanguageFunction · 0.90
isServerFunction · 0.90
getTimeZoneAbbrByUtcFunction · 0.90
dfzFormatWithValidFunction · 0.85
joinMethod · 0.65
localeMethod · 0.45

Tested by

no test coverage detected