-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathdateTime.tsx
87 lines (82 loc) · 2.29 KB
/
dateTime.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import moment from 'moment-timezone';
import ConfigStore from 'sentry/stores/configStore';
import {getFormat} from 'sentry/utils/dates';
export interface DateTimeProps extends React.HTMLAttributes<HTMLTimeElement> {
/**
* Input date.
*/
date: moment.MomentInput;
/**
* If true, will only return the date part, e.g. "Jan 1".
*/
dateOnly?: boolean;
/**
* When set, will force the date time display to be in the specified timezone
*/
forcedTimezone?: string;
/**
* Formatting string. If specified, this formatting string will override all
* other formatting props (dateOnly, timeOnly, year).
*/
format?: string;
/**
* Whether to show the seconds. Is false by default.
*/
seconds?: boolean;
/**
* If true, will only return the time part, e.g. "2:50 PM"
*/
timeOnly?: boolean;
/**
* Whether to show the time zone. If not specified, the returned date string
* will not contain the time zone _unless_ the time is UTC, in which case
* the user would want to know that it's UTC and not their own time zone.
*/
timeZone?: boolean;
/**
* Whether the date input is UTC time or not.
*/
utc?: boolean;
/**
* Whether to show the year. If not specified, the returned date string will
* not contain the year _if_ the date is not in the current calendar year.
* For example: "Feb 1" (2022), "Jan 1" (2022), "Dec 31, 2021".
*/
year?: boolean;
}
export function DateTime({
format,
date,
utc,
dateOnly,
timeOnly,
year,
timeZone,
seconds = false,
forcedTimezone,
...props
}: DateTimeProps) {
const user = ConfigStore.get('user');
const options = user?.options;
const formatString =
format ??
getFormat({
dateOnly,
timeOnly,
// If the year prop is defined, then use it. Otherwise only show the year if `date`
// is in the current year.
year: year ?? moment().year() !== moment(date).year(),
// If timeZone is defined, use it. Otherwise only show the time zone if we're using
// UTC time.
timeZone: timeZone ?? utc,
seconds,
...options,
});
return (
<time {...props}>
{utc
? moment.utc(date as moment.MomentInput).format(formatString)
: moment.tz(date, forcedTimezone ?? options?.timezone ?? '').format(formatString)}
</time>
);
}