1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 05:37:32 +02:00

fix(datetimepicker): Handle manually entered dates with UNIX epoch

This supersedes commit 19578e9fee which attempted to fix the issue
but was limited to a specific date format (dd.mm.yyyy). The new
approach uses the datetimepicker's native parsing capabilities to
handle dates in any configured format.

When users manually typed dates in fields set with
`data-date-unix="true"` without clicking on the calendar widget, the
values weren't being converted to UNIX timestamps as expected. This
resulted in the date field not appearing to save its value.

The improved solution:
- Uses the picker's `getDate()` method to parse dates according to the
  configured format
- Properly handles timezone adjustments for datetime fields
- Includes better error handling and validation
- Works with all locale formats supported by the datetimepicker

Fixes #5412
This commit is contained in:
Nick Liu
2025-04-10 19:25:03 -03:00
parent 873966c5ce
commit 23933885ca

View File

@@ -18,38 +18,42 @@ var e107 = e107 || {'settings': {}, 'behaviors': {}};
var $this = $(this); var $this = $(this);
var useUnix = $this.attr("data-date-unix"); var useUnix = $this.attr("data-date-unix");
var newValue = $this.val(); var newValue = $this.val();
if (useUnix !== "true" || newValue == "") {
var id = $this.attr("id"); var id = $this.attr("id");
var newTarget = "#" + id.replace("e-datepicker-", ""); var newTarget = "#" + id.replace("e-datepicker-", "");
// For non-UNIX fields or empty values, just copy the value
if (useUnix !== "true" || newValue === "") {
$(newTarget).val(newValue); $(newTarget).val(newValue);
return;
} }
// If UNIX timestamp is required, manually convert the date string (dd.mm.yyyy) // For UNIX timestamp conversion, use the datetimepicker instance
if (useUnix === "true") { try {
var parts = newValue.split("."); // Get date from the picker (handles format automatically)
if (parts.length === 3) { var pickerInstance = $this.data('datetimepicker');
var day = parseInt(parts[0], 10); if (!pickerInstance) return;
var month = parseInt(parts[1], 10) - 1; // Month is zero-based in JS
var year = parseInt(parts[2], 10);
// Validate date parts var date = pickerInstance.getDate();
if (!isNaN(day) && !isNaN(month) && !isNaN(year)) { if (!date || isNaN(date.getTime())) return;
var date = new Date(year, month, day);
// Convert to UNIX timestamp
var unixTimestamp = Math.floor(date.getTime() / 1000); var unixTimestamp = Math.floor(date.getTime() / 1000);
var id = $this.attr("id");
var newTarget = "#" + id.replace("e-datepicker-", ""); // Apply timezone offset if this is a datetime field
$(newTarget).val(unixTimestamp); // Update hidden UNIX field if ($this.hasClass('e-datetime')) {
var offset = parseInt($this.attr("data-date-timezone-offset"));
if (!isNaN(offset)) {
var browserOffset = date.getTimezoneOffset() * 60;
var relativeOffset = browserOffset + offset;
unixTimestamp = unixTimestamp - relativeOffset;
} }
} }
} else {
// If not using UNIX timestamp, update with raw value $(newTarget).val(unixTimestamp);
var id = $this.attr("id"); } catch (e) {
var newTarget = "#" + id.replace("e-datepicker-", ""); console.error("Error processing date:", e);
$(newTarget).val(newValue); // Update hidden field with text input value
} }
}); });
}); });
$(context).find('input.e-date').once('datetimepicker-init').each(function () { $(context).find('input.e-date').once('datetimepicker-init').each(function () {