A Vue.js component implementing the datetime picker control using the Eonasdan's bootstrap datetime picker plugin.
npm install vue-datetime-picker




A Vue.js component implementing the datetime picker control using the Eonasdan's bootstrap datetime picker plugin.
The demo page is HERE.
- Vue.js ^1.0.24
- bootstrap ^3.3.6
- font-awesome ^4.2.0
- moment ^2.9.0
- moment-timezone ^0.4.0
- Eonasdan's bootstrap datetime picker ^4.17.37
- vue-i18n-plugin ^0.2.2 This is optional.
``shell`
$ npm install vue-datetime-picker
`shell`
$ bower install vue-datetime-picker
The HTML snippets are as follows:
`html`
Selected Datetime: {{formatDatetime(result1)}}
type="datetime"
language="en"
datetime-format="LLL">
Selected Datetime: {{formatDatetime(result2)}}
type="date"
language="en-US"
date-format="L">
Selected Date: {{formatDate(result3)}}
type="time"
language="zh-CN"
time-format="LT">
Selected Time: {{formatTime(result4)}}
Demonstration of the range of datetime. Note how the minimum/maximum
selectable datetime of the start/end datetime picker was changed
according to the selection of another picker.
:model.sync="startDatetime"
:on-change="onStartDatetimeChanged">
:model.sync="endDatetime"
:on-change="onEndDatetimeChanged">
The Javascript snippets are as follows:
`javascript
var Vue = require("vue");
var vm = new Vue({
el: "#app",
components: {
"vue-datetime-picker": require("vue-datetime-picker")
},
data: {
result1: null,
result2: null,
result3: null,
startDatetime: moment(),
endDatetime: null
},
methods: {
formatDatetime: function(datetime) {
if (datetime === null) {
return "[null]";
} else {
return datetime.format("YYYY-MM-DD HH:mm:ss");
}
},
formatDate: function(date) {
if (date === null) {
return "[null]";
} else {
return date.format("YYYY-MM-DD");
}
},
formatTime: function(time) {
if (time === null) {
return "[null]";
} else {
return time.format("HH:mm:ss");
}
},
onStartDatetimeChanged: function(newStart) {
var endPicker = this.$.endPicker.control;
endPicker.minDate(newStart);
},
onEndDatetimeChanged: function(newEnd) {
var startPicker = this.$.startPicker.control;
startPicker.maxDate(newEnd);
}
}
});
`
The model bind to the control, which must be a two way binding variable.
Note that the value of the model must be either a null value, or anull
moment object. If the model is set to, the input box of the datetime picker control will set to empty,null
indicating no datetime was selected; also, if the input box of the datetime
picker control is set to empty (that is, the user delete the text in the input
box of the datetime picker control), the value of the model will be set to instead of an empty string; if the user does select a datetime, the
value of the model will be set to the moment
object representing the date, without any timezone information.
The optional type of the datetime picker control. Available values are
- "datetime": Indicating that the control is a datetime picker,"date"
- : Indicating that the control is a date picker (without time picker),"time"
- : Indicating that the control is a time picker (without date picker).
The default value of this property is "datetime".
The optional code of language used by the moment
library.
If it is not set, and the vue-i18n
plugin is used, the component will use the language code $language provided"en-US"
by the vue-i18n plugin; otherwise, the
component will use the default value .
The supported languages are exactly the same as the supported languages of the
moment library. In order to use the
supported language, you must also include the corresponding i18n js file of
the moment library in your HTML file.
A convenient way is to include the moment-with-locales.min.js.
Note that the language code passed to this property could be a locale code
consists of a language code and a country code, e.g., "en-US". The component"zh-CN"
will automatically convert the locale code to the language code supported by
the moment library. Since some languages
have different variants in different country or region, e.g., for the"zh-TW"
simplified Chinese and for the traditional Chinese, it's recommended"[language]-[country]"
to use the locale code in the form of .
The optional format of the datetime this component should display, which
must be a valid datetime format of the moment
library.
This property only works when the type property is set to "datetime". Default"YYYY-MM-DD HH:mm:ss"
value of this property is .
The optional format of the date this component should display, which
must be a valid date format of the moment
library.
This property only works when the type property is set to "date". Default"YYYY-MM-DD"
value of this property is .
The optional format of the time this component should display, which
must be a valid time format of the moment
library.
This property only works when the type property is set to "time". Default"HH:mm:ss"
value of this property is .
The optional name of the selection control.
The optional event handler triggered when the value of the datetime picker
was changed. If this parameter is presented and is not null, it must be a
function which accept one argument: the new date time selected by the picker,
which is a moment object.
This property is a reference to the JQuery selection of datetime control. It
could be used to call the APIs of the
Eonasdan's bootstrap datetime picker.
For example, picker.control.minDate(val) will set the minimum allowed datetimepicker
of the picker to the specified value, where is the reference to thevue-datetime-picker component.
This component could use the vue-i18n
plugin to localize the tooltips of the datetime picker control.
In order to localize this component, the localization files provided to the
vue-i18n plugin must provide the
following localization messages:
`json`
{
"datetime_picker": {
"today": "Go to today",
"clear": "Clear selection",
"close": "Close the picker",
"selectMonth": "Select Month",
"prevMonth": "Previous Month",
"nextMonth": "Next Month",
"selectYear": "Select Year",
"prevYear": "Previous Year",
"nextYear": "Next Year",
"selectDecade": "Select Decade",
"prevDecade": "Previous Decade",
"nextDecade": "Next Decade",
"prevCentury": "Previous Century",
"nextCentury": "Next Century",
"pickHour": "Pick Hour",
"incrementHour": "Increment Hour",
"decrementHour": "Decrement Hour",
"pickMinute": "Pick Minute",
"incrementMinute": "Increment Minute",
"decrementMinute": "Decrement Minute",
"pickSecond": "Pick Second",
"incrementSecond": "Increment Second",
"decrementSecond": "Decrement Second",
"togglePeriod": "Toggle Period",
"selectTime": "Select Time"
}
}
If no vue-i18n is used, or the
localization file of the plugin does not provide the above localization messages,
the default English messages will be used.
Some localization files could be found in the src/i18n directory.
- Fork it !
- Create your top branch from dev: git branch my-new-topic origin/devgit commit -am 'Add some topic'
- Commit your changes: git push origin my-new-topic
- Push to the branch: dev
- Submit a pull request to branch of Haixing-Hu/vue-datetime-picker repository !
First you should install all depended NPM packages. The NPM packages are used
for building and testing this package.
`shell`
$ npm install
Then install all depended bower packages. The bower packages are depended by
this packages.
`shell`
$ bower install
Now you can build the project.
`shell`
$ gulp build
The following command will test the project.
`shell`
$ gulp test
The following command will perform the test and generate a coverage report.
`shell`
$ gulp test:coverage
The following command will perform the test, generate a coverage report, and
upload the coverage report to coveralls.io.
`shell`
$ gulp test:coveralls
You can also run bower install and gulp build together with the following`
command:shell`
npm run build
Or run bower install and gulp test:coveralls together with the following`
command:shell``
npm run test