Datetimepicker selection becomes an epoch date on save


Topic: Datetimepicker selection becomes an epoch date on save

bluefox_dev asked 3 weeks ago

The booking form displays my chosen date and time. The API receives a value that becomes a date near the epoch.

Which picker value should be serialized?


Igor Przybysz staff pro premium priority answered 3 weeks ago

Please compare the picker input's value immediately before submission with the actual request payload. A date near 1970 often indicates that conversion produced zero, an invalid fallback, or a timestamp in the wrong unit. Sending a localized display string directly to a backend timestamp parser is another possibility; the displayed selection alone does not establish what the API received.

Share one selected value, the serialized payload, the expected stored value, and the client/server conversion code. State whether the endpoint expects epoch seconds, epoch milliseconds, or an ISO date-time string, and which time zone the appointment belongs to. That will let us give a precise conversion.

https://mdbootstrap.com/docs/standard/forms/datetimepicker/


bluefox_dev commented 3 weeks ago

I traced the payload as suggested. The endpoint expects epoch milliseconds, but I copied a conversion from another integration. The selected appointment is 14:30 in Warsaw, and the API passes startsAt directly to the Date constructor before storing it. Here is the simplified flow.

const selected = new Date('2026-11-05T14:30:00+01:00');
const payload = {
  startsAt: Math.floor(selected.getTime() / 1000)
};
// The API handler uses the received value directly:
const saved = new Date(payload.startsAt);

Igor Przybysz staff pro premium priority answered 3 weeks ago

The division by 1000 is the cause: getTime() already returns milliseconds, which is what the Date constructor expects. Send that value unchanged. Your example should represent 13:30 UTC, which displays as 14:30 in Warsaw on that date; keep the stored instant and apply local formatting when displaying it.

const startsAt = selected.getTime();
if (!Number.isFinite(startsAt)) {
  throw new Error('Invalid appointment time');
}
const payload = { startsAt };
const saved = new Date(payload.startsAt);
console.log(saved.toISOString());

Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: MDB Standard
  • MDB Version: MDB5 9.3.0
  • Device: Desktop
  • Browser: Chrome
  • OS: Windows 11
  • Provided sample code: No
  • Provided link: No