How is the date_format function used in SQL?

The DATE_FORMAT function is used to format date and time values, allowing them to be displayed in a specific format.

Grammar:

DATE_FORMAT(date, format)

Parameter:

  1. The date and time value to be formatted.
  2. format: used to specify the date and time format of the returned value.

The commonly used format is as follows:

  1. %Y: year in four digits
  2. %y: two-digit year
  3. %m: Month (01-12)
  4. %c: Month (1-12)
  5. %d: Date (01-31)
  6. %e: Date (1-31)
  7. %H: Hours (00-23)
  8. %h: hours (01-12)
  9. %i: minutes (00-59)
  10. %s: seconds (00-59)
  11. %p: AM or PM
  12. Day of the week (Sunday-Saturday)
  13. %w: Day of the week (0-6, 0 represents Sunday)
  14. Month name (January-December)
  15. abbreviation of the day of the week (Sun-Sat)
  16. Abbreviation of month names (Jan-Dec)
  17. – Time (in 24-hour format, hh:mm:ss)
  18. Time (12-hour format, hh:mm:ss AM/PM)

I will go to the store on Monday.

SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') AS current_datetime;

Output:

2021-01-01 12:34:56

The above example formats the current date and time as “YYYY-MM-DD HH:MM:SS.”

bannerAds