What are SQL date and time functions?
Why Interviewers Ask This
This question targets practical, hands-on experience with MySQL / SQL. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
Answer
MySQL provides rich date/time functions: NOW() — current datetime as DATETIME; CURDATE() / CURRENT_DATE() — current date only; CURTIME() — current time only; UTC_TIMESTAMP() — current datetime in UTC; DATE(datetime_expr) — extract the date part; TIME(datetime_expr) — extract the time part; YEAR(d), MONTH(d), DAY(d), HOUR(t), MINUTE(t), SECOND(t) — extract parts; DATE_FORMAT(d, format) — format a date: DATE_FORMAT(created_at, "%Y-%m-%d"); DATE_ADD(d, INTERVAL n unit) / DATE_SUB() — arithmetic: DATE_ADD(NOW(), INTERVAL 7 DAY); DATEDIFF(d1, d2) — difference in days; TIMESTAMPDIFF(unit, d1, d2) — difference in specified unit (YEAR, MONTH, DAY, HOUR); UNIX_TIMESTAMP() — seconds since epoch; FROM_UNIXTIME(ts) — convert epoch to DATETIME; DAYOFWEEK(d) — 1=Sunday; WEEKDAY(d) — 0=Monday; LAST_DAY(d) — last day of the month; STR_TO_DATE(str, format) — parse string to date.
Pro Tip
This topic has MySQL / SQL-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.