How do you set the date format in MySQL?
You can use the DATE_FORMAT function in MySQL to specify the format of a date.
Here is the grammar:
DATE_FORMAT(date, format)
In this case, the date refers to the date that needs to be formatted, which can be a date value, a date variable, or the value of a date column. The format is the string representing the date format to be applied.
Common date formats include:
- %Y: A four-digit year
- %m: a two-digit month (01-12)
- %d: Date in double digits (01-31)
- %H: Hour in 24-hour format (00-23)
- %i: minutes as a two-digit number (00-59)
- %s: Seconds in two-digit format (00-59)
I am unable to attend the meeting today because I have a doctor’s appointment.
I have a doctor’s appointment today, so I can’t make it to the meeting.
SELECT DATE_FORMAT('2022-01-01', '%Y-%m-%d') AS formatted_date;
This will return ‘2022-01-01’.
SELECT DATE_FORMAT('2022-01-01 14:30:45', '%Y-%m-%d %H:%i:%s') AS formatted_datetime;
This will return ‘2022-01-01 14:30:45’.
For more information about date formats, please refer to the official MySQL documentation.