Skip to content
This repository was archived by the owner on Apr 17, 2022. It is now read-only.

Commit 0a53b1a

Browse files
committed
feat: Add support for UTC zone (resolves #88)
1 parent ef9dce8 commit 0a53b1a

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ interface Vue3DatePicker {
188188
maxRange?: number | string;
189189
fixedStart?: boolean;
190190
fixedEnd?: boolean;
191+
utc?: boolean;
191192
}
192193

193194
interface PublicMethods extends MethodOptions {

src/Vue3DatePicker/Vue3DatePicker.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
maxRange: { type: [Number, String] as PropType<number | string>, default: null },
285285
fixedStart: { type: Boolean as PropType<boolean>, default: false },
286286
fixedEnd: { type: Boolean as PropType<boolean>, default: false },
287+
utc: { type: Boolean as PropType<boolean>, default: false },
287288
});
288289
const slots = useSlots();
289290
const isOpen = ref(false);
@@ -296,6 +297,7 @@
296297
provide('textInput', toRef(props, 'textInput'));
297298
298299
onMounted(() => {
300+
// store.setTimezone(props.timezone);
299301
parseExternalModelValue(props.modelValue);
300302
if (!props.inline) {
301303
window.addEventListener('scroll', onScroll);
@@ -346,6 +348,7 @@
346348
props.enableSeconds,
347349
formatLocaleRef,
348350
props.multiDates,
351+
props.utc,
349352
emit,
350353
);
351354

src/Vue3DatePicker/components/composition/external-internal-mapper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ComputedRef, Ref, ref, watch } from 'vue';
22

33
import {
4+
dateToUtc,
45
formatDate,
56
getDefaultPattern,
67
getTImeForExternal,
@@ -36,6 +37,7 @@ export const useExternalInternalMapper = (
3637
enableSeconds: boolean,
3738
formatLocale: ComputedRef<Locale>,
3839
multiDates: boolean,
40+
utc: boolean,
3941
emit: VueEmit,
4042
): IExternalInternalMapper => {
4143
const inputValue = ref('');
@@ -135,7 +137,12 @@ export const useExternalInternalMapper = (
135137
if (internalModelValue.value && range && partialRange && internalModelValue.value.length === 1) {
136138
internalModelValue.value.push(null);
137139
}
138-
emit('update:modelValue', internalModelValue.value);
140+
const zonedDate = utc
141+
? Array.isArray(internalModelValue.value)
142+
? internalModelValue.value.map((date) => (date ? dateToUtc(date) : date))
143+
: dateToUtc(internalModelValue.value)
144+
: internalModelValue.value;
145+
emit('update:modelValue', zonedDate);
139146
}
140147
formatInputValue();
141148
};

src/Vue3DatePicker/utils/date-utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,16 @@ export const isMonthWithinRange = (
236236

237237
return valid;
238238
};
239+
240+
export const dateToUtc = (date: Date): string => {
241+
const utcDate = Date.UTC(
242+
date.getUTCFullYear(),
243+
date.getUTCMonth(),
244+
date.getUTCDate(),
245+
date.getUTCHours(),
246+
date.getUTCMinutes(),
247+
date.getUTCSeconds(),
248+
);
249+
250+
return new Date(utcDate).toISOString();
251+
};

0 commit comments

Comments
 (0)