2022-04-12 01:43:49 +02:00
|
|
|
<template>
|
2022-04-15 23:10:47 +02:00
|
|
|
<div>
|
|
|
|
<n-card>
|
|
|
|
<n-space justify="center">
|
2022-04-22 23:31:40 +02:00
|
|
|
<n-form-item label="Use current date-time ?" label-placement="left" :show-feedback="false">
|
2022-04-15 23:10:47 +02:00
|
|
|
<n-switch v-model:value="useCurrentDate" />
|
|
|
|
</n-form-item>
|
|
|
|
</n-space>
|
|
|
|
<n-form-item
|
|
|
|
:feedback="inputInvalid ? 'Invalid date for the current format' : ''"
|
|
|
|
:validation-status="inputInvalid ? 'error' : undefined"
|
|
|
|
>
|
2022-04-22 23:31:40 +02:00
|
|
|
<n-input-group style="flex-grow: 1">
|
2022-04-15 23:10:47 +02:00
|
|
|
<n-select
|
|
|
|
v-model:value="inputFormat"
|
2022-04-22 23:31:40 +02:00
|
|
|
style="width: 200px"
|
2022-04-15 23:10:47 +02:00
|
|
|
:options="formats.map(({ name }, i) => ({ label: name, value: i }))"
|
|
|
|
:disabled="useCurrentDate"
|
|
|
|
/>
|
2022-04-12 01:43:49 +02:00
|
|
|
|
2022-04-15 23:10:47 +02:00
|
|
|
<n-input
|
|
|
|
v-model:value="inputDate"
|
|
|
|
:on-input="onDateInputChanged"
|
|
|
|
:disabled="useCurrentDate"
|
|
|
|
placeholder="Your date string..."
|
|
|
|
/>
|
|
|
|
</n-input-group>
|
|
|
|
</n-form-item>
|
2022-04-22 23:31:40 +02:00
|
|
|
<n-divider style="margin-top: 0" />
|
|
|
|
<div v-for="{ name, fromDate } in formats" :key="name" style="margin: 5px 0">
|
2022-04-15 23:10:47 +02:00
|
|
|
<n-input-group>
|
2022-04-22 23:31:40 +02:00
|
|
|
<n-input-group-label style="flex: 0 0 170px"> {{ name }}: </n-input-group-label>
|
2022-04-16 21:00:14 +02:00
|
|
|
<input-copyable :value="fromDate(baseDate)" />
|
2022-04-15 23:10:47 +02:00
|
|
|
</n-input-group>
|
|
|
|
</div>
|
|
|
|
</n-card>
|
|
|
|
</div>
|
2022-04-12 01:43:49 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { useRafFn } from '@vueuse/core';
|
2022-04-22 23:31:40 +02:00
|
|
|
import {
|
|
|
|
formatISO,
|
|
|
|
formatISO9075,
|
|
|
|
formatRFC3339,
|
|
|
|
formatRFC7231,
|
|
|
|
fromUnixTime,
|
|
|
|
getTime,
|
|
|
|
getUnixTime,
|
|
|
|
isDate,
|
|
|
|
parseISO,
|
|
|
|
parseJSON,
|
|
|
|
} from 'date-fns';
|
|
|
|
import { ref } from 'vue';
|
|
|
|
import InputCopyable from '../../components/InputCopyable.vue';
|
2022-04-12 01:43:49 +02:00
|
|
|
|
2022-04-22 23:31:40 +02:00
|
|
|
const useCurrentDate = ref(true);
|
|
|
|
const inputDate = ref('');
|
|
|
|
const inputFormat = ref(6);
|
|
|
|
const inputInvalid = ref(false);
|
|
|
|
const baseDate = ref(new Date());
|
2022-04-12 01:43:49 +02:00
|
|
|
|
|
|
|
useRafFn(() => {
|
2022-04-22 23:31:40 +02:00
|
|
|
if (useCurrentDate.value) {
|
|
|
|
baseDate.value = new Date();
|
|
|
|
}
|
|
|
|
});
|
2022-04-12 01:43:49 +02:00
|
|
|
|
|
|
|
function onDateInputChanged(value: string) {
|
2022-04-22 23:31:40 +02:00
|
|
|
const { toDate } = formats[inputFormat.value];
|
|
|
|
inputInvalid.value = false;
|
2022-04-12 01:43:49 +02:00
|
|
|
|
2022-04-22 23:31:40 +02:00
|
|
|
try {
|
|
|
|
const formatted: Date | string = toDate(value);
|
2022-04-12 01:43:49 +02:00
|
|
|
|
2022-04-22 23:31:40 +02:00
|
|
|
if (!isDate(formatted) || isNaN(formatted.getTime())) {
|
|
|
|
throw new Error('Invalid date');
|
2022-04-12 01:43:49 +02:00
|
|
|
}
|
2022-04-22 23:31:40 +02:00
|
|
|
|
|
|
|
baseDate.value = formatted;
|
|
|
|
} catch (_) {
|
|
|
|
inputInvalid.value = true;
|
|
|
|
}
|
2022-04-12 01:43:49 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 22:06:39 +10:00
|
|
|
type Format = {
|
|
|
|
name: string;
|
|
|
|
fromDate: (date: Date) => string;
|
|
|
|
toDate: (value: string) => Date;
|
|
|
|
};
|
|
|
|
|
|
|
|
const toDate: Format['toDate'] = (date) => new Date(date);
|
|
|
|
|
|
|
|
const formats: Format[] = [
|
2022-04-22 23:31:40 +02:00
|
|
|
{
|
|
|
|
name: 'JS locale date string',
|
2022-05-11 22:06:39 +10:00
|
|
|
fromDate: (date) => date.toString(),
|
|
|
|
toDate,
|
2022-04-22 23:31:40 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'ISO 8601',
|
2022-05-11 22:06:39 +10:00
|
|
|
fromDate: formatISO,
|
|
|
|
toDate: parseISO,
|
2022-04-22 23:31:40 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'ISO 9075',
|
2022-05-11 22:06:39 +10:00
|
|
|
fromDate: formatISO9075,
|
|
|
|
toDate: parseISO,
|
2022-04-22 23:31:40 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'RFC 3339',
|
2022-05-11 22:06:39 +10:00
|
|
|
fromDate: formatRFC3339,
|
|
|
|
toDate,
|
2022-04-22 23:31:40 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'RFC 7231',
|
2022-05-11 22:06:39 +10:00
|
|
|
fromDate: formatRFC7231,
|
|
|
|
toDate,
|
2022-04-22 23:31:40 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Timestamp',
|
2022-05-11 22:06:39 +10:00
|
|
|
fromDate: (date) => String(getTime(date)),
|
|
|
|
toDate: (ms) => parseJSON(+ms),
|
2022-04-22 23:31:40 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Unix timestamp',
|
2022-05-11 22:06:39 +10:00
|
|
|
fromDate: (date) => String(getUnixTime(date)),
|
|
|
|
toDate: (sec) => fromUnixTime(+sec),
|
2022-04-22 23:31:40 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'UTC format',
|
2022-05-11 22:06:39 +10:00
|
|
|
fromDate: (date) => date.toUTCString(),
|
|
|
|
toDate,
|
2022-04-22 23:31:40 +02:00
|
|
|
},
|
|
|
|
];
|
2022-04-12 01:43:49 +02:00
|
|
|
</script>
|