Date Formats
The range works with different date types:
- JS date object - a common way of passing a date is through a Date object:
new Date(1995, 11, 17, 15, 24)(make sure to not simply pass a date string tonew Date()) - ISO date string - standardized way of passing dates:
'2008-09-15T15:53:00'(make sure to pass it as a string) - Moment.js object - a great solution that solves common date management difficulties:
moment([2018, 3, 27, 12, 15])(make sure to have moment.js loaded)
When passing dates to the component - eg. invalids, min/max - you can do it in either format and the picker will automatically know what to do with it. If you want to specify how the picker should return values, you can do it in the returnFormat option.
Demo
JS date object
<script setup>
const date = ref()
function setDate() {
date.value = [new Date(2020, 10, 15), new Date(2020, 10, 21)]
}
</script>
<template>
<MbscDatepicker v-model="date" returnFormat="jsdate" />
</template>
ISO date string
<script setup>
const date = ref()
function setDate() {
date.value = ['2020-05-20', '2020-05-26']
}
</script>
<template>
<MbscDatepicker v-model="date" returnFormat="iso8601" />
</template>
Moment.js object
<script setup>
const date = ref()
function setDate() {
date.value = [moment([2020, 2, 6]), moment([2020, 2, 12])]
}
</script>
<template>
<MbscDatepicker v-model="date" returnFormat="moment" />
</template>
SFC JSSFC TS
<script setup>
import {
MbscButton,
MbscDatepicker,
MbscPage,
setOptions
} from '@mobiscroll/vue'
import moment from 'moment'
import { ref } from 'vue'
setOptions({
locale: ,
theme: ' ',
themeVariant: ' '
})
const date = ref()
const iso = ref()
const momentJs = ref()
function setDate() {
date.value = [new Date(2020, 10, 15), new Date(2020, 10, 21)]
}
function setISO() {
iso.value = ['2020-05-20', '2020-05-26']
}
function setMoment() {
// Make sure that moment js is loaded
momentJs.value = [moment([2020, 2, 6]), moment([2020, 2, 12])]
}
</script>
<template>
<MbscPage>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Controlling the default value</div>
<div class="MbscButton-group-block">
<MbscButton @click="setDate">Set: Sun Nov 15 2020 - Sat Nov 21 2020</MbscButton>
</div>
<MbscDatepicker
:controls="['calendar']"
select="range"
v-model="date"
returnFormat="jsdate"
label="Date object"
/>
</div>
<div class="mbsc-padding">Return value: {{ date }}</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Working with Date strings</div>
<div class="MbscButton-group-block">
<MbscButton @click="setISO()">Set: 2020-05-20 - 2020-05-26</MbscButton>
</div>
<div>
<MbscDatepicker
:controls="['calendar']"
select="range"
v-model="iso"
returnFormat="iso8601"
label="ISO string"
/>
</div>
</div>
<div class="mbsc-padding">Return value: {{ iso }}</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Working with Moment JS Objects</div>
<div class="MbscButton-group-block">
<MbscButton @click="setMoment()">Set: 2020-03-06 - 2020-03-12</MbscButton>
</div>
<div>
<MbscDatepicker
:controls="['calendar']"
select="range"
v-model="momentJs"
returnFormat="moment"
label="Moment JS"
/>
</div>
</div>
<div class="mbsc-padding">
Return value: {{ momentJs ? momentJs[0].format() + ' - ' + momentJs[1].format() : '' }}
</div>
</MbscPage>
</template>
<script setup lang="ts">
import {
MbscButton,
MbscDatepicker,
MbscPage,
setOptions
} from '@mobiscroll/vue'
import moment from 'moment'
import { ref } from 'vue'
setOptions({
locale: ,
theme: ' ',
themeVariant: ' '
})
const date = ref<any>()
const iso = ref<any>()
const momentJs = ref<any>()
function setDate() {
date.value = [new Date(2020, 10, 15), new Date(2020, 10, 21)]
}
function setISO() {
iso.value = ['2020-05-20', '2020-05-26']
}
function setMoment() {
// Make sure that moment js is loaded
momentJs.value = [moment([2020, 2, 6]), moment([2020, 2, 12])]
}
</script>
<template>
<MbscPage>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Controlling the default value</div>
<div class="MbscButton-group-block">
<MbscButton @click="setDate">Set: Sun Nov 15 2020 - Sat Nov 21 2020</MbscButton>
</div>
<MbscDatepicker
:controls="['calendar']"
select="range"
v-model="date"
returnFormat="jsdate"
label="Date object"
/>
</div>
<div class="mbsc-padding">Return value: {{ date }}</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Working with Date strings</div>
<div class="MbscButton-group-block">
<MbscButton @click="setISO()">Set: 2020-05-20 - 2020-05-26</MbscButton>
</div>
<div>
<MbscDatepicker
:controls="['calendar']"
select="range"
v-model="iso"
returnFormat="iso8601"
label="ISO string"
/>
</div>
</div>
<div class="mbsc-padding">Return value: {{ iso }}</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Working with Moment JS Objects</div>
<div class="MbscButton-group-block">
<MbscButton @click="setMoment()">Set: 2020-03-06 - 2020-03-12</MbscButton>
</div>
<div>
<MbscDatepicker
:controls="['calendar']"
select="range"
v-model="momentJs"
returnFormat="moment"
label="Moment JS"
/>
</div>
</div>
<div class="mbsc-padding">
Return value: {{ momentJs ? momentJs[0].format() + ' - ' + momentJs[1].format() : '' }}
</div>
</MbscPage>
</template>