valFormat()
Get / set a formatted value for the instance.
Please note - this property requires the DateTime extension for DataTables.
Description
This method can be used to get or set the value for the DateTime instance, in a specific format. For example, it can be used to set a value given in a format that is not ISO8601, or you could read back a value in some other format (i.e. a localised human readable format).
The formatting options available will depend upon the date / time library being used (e.g. Moment.js or Luxon).
Types
valFormat(format)
Get the value of the DateTime instance.
Parameters:
Name | Type | Optional | |
---|---|---|---|
1 | format | No | |
The format that you want the date/time value in. |
Returns:
string
The formatted value.
valFormat(format, set)
Set the value of the DateTime instance.
Parameters:
Name | Type | Optional | |
---|---|---|---|
1 | format | No | |
The format that the date / time value string is currently in. | |||
2 | set | No | |
The value to be set, in the format given by the first parameter. |
Returns:
DateTime
Self for chaining.
Examples
Get a value from the input with a specific format:
let el = document.getElementById('example');
let dt = new DateTime(el);
el.addEventListener('change', function () {
console.log('Selected value is: ', dt.valFormat('MM-DD-YYYY'));
});
Set a value into the input from a formatted string:
let dt = new DateTime(document.getElementById('example'));
dt.valFormat('MM-DD-YY', '03-02-23');