@@ -31,7 +31,7 @@ export interface DatePickerCellProps {
31
31
readonly displayDate : string ;
32
32
/* Defines the type of the HTML input element. */
33
33
readonly format : DateKind ;
34
- /* Timezone offset in minutes.
34
+ /* Timezone offset in minutes.
35
35
This can be used to adjust the date by a given timezone offset. */
36
36
readonly timezoneOffset ?: number ;
37
37
/* Minimum value that can be entered by the user.
@@ -40,17 +40,27 @@ export interface DatePickerCellProps {
40
40
/* Maximum value that can be entered by the user.
41
41
This is passed to the max attribute of the HTML input element. */
42
42
readonly max ?: string | Date ;
43
- /* Granularity that the date must adhere.
43
+ /* Granularity that the date must adhere.
44
44
This is passed to the step attribute of the HTML input element. */
45
45
readonly step ?: string ;
46
46
}
47
47
48
48
export type DateKind = "date" | "time" | "datetime-local" ;
49
49
50
- export const formatValueForHTMLInput = ( dateKind : DateKind , date : Date | undefined | null ) : string => {
50
+ export const formatValueForHTMLInput = (
51
+ dateKind : DateKind ,
52
+ date : Date | undefined | null ,
53
+ timezoneOffsetMs ?: number
54
+ ) : string => {
51
55
if ( date === undefined || date === null ) {
52
56
return "" ;
53
57
}
58
+
59
+ if ( timezoneOffsetMs ) {
60
+ // Adjust based on the configured timezone offset:
61
+ date = new Date ( date . getTime ( ) + timezoneOffsetMs ) ;
62
+ }
63
+
54
64
const isoDate = date . toISOString ( ) ;
55
65
switch ( dateKind ) {
56
66
case "date" :
@@ -72,18 +82,18 @@ const Editor: ReturnType<ProvideEditorCallback<DatePickerCell>> = cell => {
72
82
const step =
73
83
cellData . step !== undefined && ! Number . isNaN ( Number ( cellData . step ) ) ? Number ( cellData . step ) : undefined ;
74
84
75
- const minValue = cellData . min instanceof Date ? formatValueForHTMLInput ( format , cellData . min ) : cellData . min ;
85
+ // Convert timezone offset from minutes to milliseconds:
86
+ const timezoneOffsetMs = cellData . timezoneOffset ? cellData . timezoneOffset * 60 * 1000 : 0 ;
76
87
77
- const maxValue = cellData . max instanceof Date ? formatValueForHTMLInput ( format , cellData . max ) : cellData . max ;
88
+ // We need to convert the min and max to iso strings. Thereby, we are also
89
+ // adjusting the values to the given timezone offset.
90
+ const minValue =
91
+ cellData . min instanceof Date ? formatValueForHTMLInput ( format , cellData . min , timezoneOffsetMs ) : cellData . min ;
92
+ const maxValue =
93
+ cellData . max instanceof Date ? formatValueForHTMLInput ( format , cellData . max , timezoneOffsetMs ) : cellData . max ;
94
+
95
+ const value = formatValueForHTMLInput ( format , cellData . date , timezoneOffsetMs ) ;
78
96
79
- let date = cellData . date ;
80
- // Convert timezone offset to milliseconds
81
- const timezoneOffsetMs = cellData . timezoneOffset ? cellData . timezoneOffset * 60 * 1000 : 0 ;
82
- if ( timezoneOffsetMs && date ) {
83
- // Adjust based on the timezone offset
84
- date = new Date ( date . getTime ( ) + timezoneOffsetMs ) ;
85
- }
86
- const value = formatValueForHTMLInput ( format , date ) ;
87
97
if ( cell . value . readonly ) {
88
98
return (
89
99
< TextCellEntry
0 commit comments