Merge pull request #642 from nireno/fix_641

Fix issue #641
This commit is contained in:
Valeriy 2018-02-16 15:06:43 +05:00 committed by GitHub
commit 4bbf1de9aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 12 deletions

14
doc.tpl
View File

@ -372,6 +372,18 @@ jQuery('#_datetimepicker_weekends_disable').datetimepicker({
formatDate: function (date, format) {
return moment(date).format(format);
},
//Optional if using mask input
formatMask: function(format){
return format
.replace(/Y{4}/g, '9999')
.replace(/Y{2}/g, '99')
.replace(/M{2}/g, '19')
.replace(/D{2}/g, '39')
.replace(/H{2}/g, '29')
.replace(/m{2}/g, '59')
.replace(/s{2}/g, '59');
}
});
</code></pre>
@ -381,6 +393,8 @@ jQuery('#_datetimepicker_weekends_disable').datetimepicker({
formatTime:'h:mm a',
formatDate:'DD.MM.YYYY'
});</code></pre>
<p>Because of its popularity, moment.js has a pre-defined configuration that can be enabled with:</p>
<pre><code class="language-javascript">$.datetimepicker.setDateFormatter('moment');</code></pre>
<hr id="range" />
<h4>Range between date<a href="#range">#</a></h4>
<p><strong>javaScript</strong></p>

View File

@ -633,6 +633,7 @@ var datetimepickerFactory = function ($) {
};
var dateHelper = null,
defaultDateHelper = null,
globalLocaleDefault = 'en',
globalLocale = 'en';
@ -650,12 +651,49 @@ var datetimepickerFactory = function ($) {
};
if (typeof DateFormatter === 'function') {
dateHelper = new DateFormatter({
dateHelper = defaultDateHelper = new DateFormatter({
dateSettings: $.extend({}, dateFormatterOptionsDefault, opts)
});
}
};
var dateFormatters = {
moment: {
default_options:{
format: 'YYYY/MM/DD HH:mm',
formatDate: 'YYYY/MM/DD',
formatTime: 'HH:mm',
},
formatter: {
parseDate: function (date, format) {
if(isFormatStandard(format)){
return defaultDateHelper.parseDate(date, format);
}
var d = moment(date, format);
return d.isValid() ? d.toDate() : false;
},
formatDate: function (date, format) {
if(isFormatStandard(format)){
return defaultDateHelper.formatDate(date, format);
}
return moment(date).format(format);
},
formatMask: function(format){
return format
.replace(/Y{4}/g, '9999')
.replace(/Y{2}/g, '99')
.replace(/M{2}/g, '19')
.replace(/D{2}/g, '39')
.replace(/H{2}/g, '29')
.replace(/m{2}/g, '59')
.replace(/s{2}/g, '59');
},
}
}
}
// for locale settings
$.datetimepicker = {
setLocale: function(locale){
@ -668,9 +706,18 @@ var datetimepickerFactory = function ($) {
},
setDateFormatter: function(dateFormatter) {
dateHelper = dateFormatter;
if(typeof dateFormatter === 'string' && dateFormatters.hasOwnProperty(dateFormatter)){
var df = dateFormatters[dateFormatter];
$.extend(default_options, df.default_options);
dateHelper = df.formatter;
}
else {
dateHelper = dateFormatter;
}
},
};
var standardFormats = {
RFC_2822: 'D, d M Y H:i:s O',
ATOM: 'Y-m-d\TH:i:sP',
ISO_8601: 'Y-m-d\TH:i:sO',
@ -680,7 +727,13 @@ var datetimepickerFactory = function ($) {
RFC_1123: 'D, d M Y H:i:s O',
RSS: 'D, d M Y H:i:s O',
W3C: 'Y-m-d\TH:i:sP'
};
}
var isFormatStandard = function(format){
return Object.values(standardFormats).indexOf(format) === -1 ? false : true;
}
$.extend($.datetimepicker, standardFormats);
// first init date formatter
initDateFormatter();
@ -2255,15 +2308,8 @@ var datetimepickerFactory = function ($) {
}
if (options.mask === true) {
if (typeof moment != 'undefined') {
options.mask = options.format
.replace(/Y{4}/g, '9999')
.replace(/Y{2}/g, '99')
.replace(/M{2}/g, '19')
.replace(/D{2}/g, '39')
.replace(/H{2}/g, '29')
.replace(/m{2}/g, '59')
.replace(/s{2}/g, '59');
if (dateHelper.formatMask) {
options.mask = dateHelper.formatMask(options.format)
} else {
options.mask = options.format
.replace(/Y/g, '9999')