0
# Instance Methods
1
2
Methods available on individual datetimepicker instances for programmatic control and value access.
3
4
## Capabilities
5
6
### Get Current Value
7
8
Retrieve the current datetime value from the picker instance.
9
10
```javascript { .api }
11
/**
12
* Get the current datetime value from the picker
13
* @returns Current datetime as Date object or null if no value
14
*/
15
getValue(): Date | null;
16
```
17
18
**Usage Examples:**
19
20
```javascript
21
// Get picker instance and retrieve value
22
var picker = $('#myinput').datetimepicker().data('xdsoft_datetimepicker');
23
var currentValue = picker.getValue();
24
25
if (currentValue) {
26
console.log('Selected date:', currentValue.toISOString());
27
} else {
28
console.log('No date selected');
29
}
30
31
// Get value after user selection
32
$('#myinput').datetimepicker({
33
onSelectDate: function(current_time, input) {
34
var picker = $(input).data('xdsoft_datetimepicker');
35
var value = picker.getValue();
36
console.log('Date selected:', value);
37
}
38
});
39
```
40
41
### Update Options
42
43
Update picker configuration options after initialization.
44
45
```javascript { .api }
46
/**
47
* Update picker options after initialization
48
* @param options - New options to apply
49
* @returns void
50
*/
51
setOptions(options: Partial<DateTimePickerOptions>): void;
52
```
53
54
**Usage Examples:**
55
56
```javascript
57
// Initialize picker
58
var picker = $('#myinput').datetimepicker().data('xdsoft_datetimepicker');
59
60
// Update options dynamically
61
picker.setOptions({
62
format: 'Y-m-d',
63
timepicker: false
64
});
65
66
// Change constraints based on other inputs
67
$('#startDate').on('change', function() {
68
var startDate = $(this).val();
69
var endPicker = $('#endDate').data('xdsoft_datetimepicker');
70
endPicker.setOptions({
71
minDate: startDate
72
});
73
});
74
75
// Toggle between date and datetime modes
76
$('#modeToggle').on('change', function() {
77
var isDateOnly = $(this).is(':checked');
78
var picker = $('#myinput').data('xdsoft_datetimepicker');
79
80
picker.setOptions({
81
timepicker: !isDateOnly,
82
format: isDateOnly ? 'Y-m-d' : 'Y-m-d H:i'
83
});
84
});
85
```
86
87
### Accessing Instance Methods
88
89
Different ways to access and use instance methods.
90
91
**Usage Examples:**
92
93
```javascript
94
// Method 1: Get instance from data attribute
95
var instance = $('#myinput').datetimepicker().data('xdsoft_datetimepicker');
96
var value = instance.getValue();
97
instance.setOptions({ step: 15 });
98
99
// Method 2: Chain access during initialization
100
$('#myinput').datetimepicker({
101
onShow: function() {
102
var picker = $(this).data('xdsoft_datetimepicker');
103
console.log('Current value:', picker.getValue());
104
}
105
});
106
107
// Method 3: Access in event callbacks
108
$('#myinput').datetimepicker({
109
onChangeDateTime: function(currentTime, input) {
110
var picker = $(input).data('xdsoft_datetimepicker');
111
112
// Update other pickers based on this value
113
if (currentTime) {
114
var nextDay = new Date(currentTime);
115
nextDay.setDate(nextDay.getDate() + 1);
116
117
$('#endDate').data('xdsoft_datetimepicker').setOptions({
118
minDate: nextDay
119
});
120
}
121
}
122
});
123
124
// Method 4: Store reference for later use
125
var myPicker = $('#myinput').datetimepicker().data('xdsoft_datetimepicker');
126
127
// Use stored reference
128
$('#updateBtn').click(function() {
129
myPicker.setOptions({
130
maxDate: new Date()
131
});
132
});
133
134
$('#getValueBtn').click(function() {
135
var value = myPicker.getValue();
136
$('#display').text(value ? value.toString() : 'No value');
137
});
138
```
139
140
### Instance Method Patterns
141
142
Common patterns for working with instance methods.
143
144
**Usage Examples:**
145
146
```javascript
147
// Validation with instance methods
148
function validateDateRange() {
149
var startPicker = $('#startDate').data('xdsoft_datetimepicker');
150
var endPicker = $('#endDate').data('xdsoft_datetimepicker');
151
152
var startValue = startPicker.getValue();
153
var endValue = endPicker.getValue();
154
155
if (startValue && endValue && startValue > endValue) {
156
alert('Start date must be before end date');
157
return false;
158
}
159
return true;
160
}
161
162
// Dynamic option updates based on business logic
163
function updatePickerConstraints(userRole) {
164
var picker = $('#appointmentDate').data('xdsoft_datetimepicker');
165
166
if (userRole === 'admin') {
167
picker.setOptions({
168
minDate: false,
169
maxDate: false,
170
disabledDates: []
171
});
172
} else {
173
picker.setOptions({
174
minDate: new Date(),
175
maxDate: new Date(Date.now() + 30*24*60*60*1000), // 30 days
176
disabledWeekDays: [0, 6] // No weekends
177
});
178
}
179
}
180
181
// Sync multiple pickers
182
function createLinkedPickers() {
183
$('#startTime').datetimepicker({
184
format: 'Y-m-d H:i',
185
onChangeDateTime: function(currentTime, input) {
186
if (currentTime) {
187
var endPicker = $('#endTime').data('xdsoft_datetimepicker');
188
endPicker.setOptions({
189
minDateTime: currentTime
190
});
191
}
192
}
193
});
194
195
$('#endTime').datetimepicker({
196
format: 'Y-m-d H:i',
197
onChangeDateTime: function(currentTime, input) {
198
if (currentTime) {
199
var startPicker = $('#startTime').data('xdsoft_datetimepicker');
200
startPicker.setOptions({
201
maxDateTime: currentTime
202
});
203
}
204
}
205
});
206
}
207
```
208
209
### Error Handling
210
211
Handle cases where instance methods might fail.
212
213
**Usage Examples:**
214
215
```javascript
216
// Safe instance access
217
function safeGetValue(selector) {
218
try {
219
var picker = $(selector).data('xdsoft_datetimepicker');
220
if (picker && typeof picker.getValue === 'function') {
221
return picker.getValue();
222
}
223
} catch (e) {
224
console.warn('Could not get value from picker:', e);
225
}
226
return null;
227
}
228
229
// Safe option updates
230
function safeSetOptions(selector, options) {
231
try {
232
var picker = $(selector).data('xdsoft_datetimepicker');
233
if (picker && typeof picker.setOptions === 'function') {
234
picker.setOptions(options);
235
return true;
236
}
237
} catch (e) {
238
console.warn('Could not update picker options:', e);
239
}
240
return false;
241
}
242
243
// Check if picker is initialized
244
function isPickerInitialized(selector) {
245
var picker = $(selector).data('xdsoft_datetimepicker');
246
return picker && typeof picker.getValue === 'function';
247
}
248
```