1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-01 20:30:39 +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 useUnix = $this.attr("data-date-unix");
var newValue = $this.val();
if (useUnix !== "true" || newValue == "") {
var id = $this.attr("id");
var newTarget = "#" + id.replace("e-datepicker-", "");
// For non-UNIX fields or empty values, just copy the value
if (useUnix !== "true" || newValue === "") {
$(newTarget).val(newValue);
return;
}
// If UNIX timestamp is required, manually convert the date string (dd.mm.yyyy)
if (useUnix === "true") {
var parts = newValue.split(".");
if (parts.length === 3) {
var day = parseInt(parts[0], 10);
var month = parseInt(parts[1], 10) - 1; // Month is zero-based in JS
var year = parseInt(parts[2], 10);
// For UNIX timestamp conversion, use the datetimepicker instance
try {
// Get date from the picker (handles format automatically)
var pickerInstance = $this.data('datetimepicker');
if (!pickerInstance) return;
// Validate date parts
if (!isNaN(day) && !isNaN(month) && !isNaN(year)) {
var date = new Date(year, month, day);
var date = pickerInstance.getDate();
if (!date || isNaN(date.getTime())) return;
// Convert to UNIX timestamp
var unixTimestamp = Math.floor(date.getTime() / 1000);
var id = $this.attr("id");
var newTarget = "#" + id.replace("e-datepicker-", "");
$(newTarget).val(unixTimestamp); // Update hidden UNIX field
// Apply timezone offset if this is a datetime 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
var id = $this.attr("id");
var newTarget = "#" + id.replace("e-datepicker-", "");
$(newTarget).val(newValue); // Update hidden field with text input value
$(newTarget).val(unixTimestamp);
} catch (e) {
console.error("Error processing date:", e);
}
});
});
$(context).find('input.e-date').once('datetimepicker-init').each(function () {