Convert JS date time to MySQL datetime
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A JavaScript Date object represents a point in time, while a MySQL DATETIME column stores a textual calendar value with no timezone attached. Converting safely means choosing a timezone policy first, then formatting the date consistently before sending it through a parameterized SQL query.
Core Sections
Decide whether you are storing UTC or local wall time
The main design question is not string formatting. It is semantics. A Date in JavaScript carries an absolute timestamp, but DATETIME in MySQL is just a date-time value without timezone metadata. If one service stores UTC and another assumes local time, the data starts drifting by whole hours.
For most backend systems, storing UTC is the safer convention. You keep one canonical value in the database and only convert to local time at the display layer.
Format a JavaScript Date as MySQL DATETIME
MySQL expects the form YYYY-MM-DD HH:MM:SS. A small helper can build that from UTC parts.
Using the UTC getters is the critical detail. If you accidentally use getHours() instead of getUTCHours(), the stored value becomes dependent on the server’s local timezone.
Insert it with parameterized SQL
Do not concatenate the formatted string directly into SQL. Use query parameters so quoting and escaping are handled by the driver.
This is cleaner and safer than generating raw SQL strings.
Convert values back to JavaScript carefully
When a MySQL DATETIME string comes back from the database, JavaScript still needs to know how to interpret it. If your policy is that the stored value represents UTC, add the T separator and Z suffix before parsing.
If you skip that policy step and let each service interpret the string differently, bugs show up around timezones, servers in different regions, and daylight saving changes.
DATETIME versus TIMESTAMP
This topic often gets mixed up with MySQL column types. TIMESTAMP has stronger built-in timezone-related behavior in MySQL. DATETIME is more literal. If the value represents a real instant in time, TIMESTAMP may be a better fit in some systems. If the value represents a business-local wall time, such as a schedule entered by a human, DATETIME may be more appropriate.
The important point is consistency. Pick the meaning once, document it, and keep every service aligned with that meaning.
Test round trips, not just formatting
Date-time bugs are often invisible until a value is written, read back, and displayed in another timezone. A round-trip test catches more than a formatting test does.
Test cases near midnight, month boundaries, and daylight saving transitions are especially useful because they reveal hidden assumptions quickly.
Common Pitfalls
- Using local getters such as
getHours()when the system expects UTC causes silent timezone drift. - Concatenating the date string directly into SQL is unnecessary and error-prone compared with parameterized queries.
- Treating a MySQL
DATETIMEvalue as timezone-aware without defining a policy creates inconsistent behavior across services. - Parsing the returned database string without an explicit UTC rule can shift the value depending on the runtime environment.
- Focusing only on formatting while ignoring
DATETIMEversusTIMESTAMPsemantics leads to the wrong storage type for the business meaning.
Summary
- MySQL
DATETIMEexpects aYYYY-MM-DD HH:MM:SSstring. - The real design choice is whether the stored value represents UTC or local wall time.
- Use UTC formatting and parameterized queries for the most predictable backend behavior.
- Parse database values back into JavaScript with the same timezone policy you used when storing them.
- Validate with round-trip tests, not just one-way format checks.

