Date and Time Functions in MySQL

Q

What Are Date and Time Functions in MySQL?

✍: FYIcenter.com

A

MySQL offers a number of functions for date and time values:

  • ADDDATE(date, INTERVAL expr unit) - Adding days to a date. Same as DATE_ADD().
  • ADDTIME(time1, time2) - Adding two time values together.
  • CURDATE() - Returning the current date. Same as CURRENT_DATE().
  • CURTIME() - Returning the current time. Same as CURRENT_TIME().
  • DATE(expression) - Returning the date from the expression.
  • DATEDIFF(date1, date2) - Returning dates difference in days.
  • DATE_ADD(date, INTERVAL expr unit) - Adding days to a date.
  • DATE_SUB(date, INTERVAL expr unit) - Subtracting days from a date.
  • DATE_FORMAT(date, format) - Returning a character string representing a date.
  • DAY(date) - Returning an integer representing the day of the month. Same as DAYOFMONTH()
  • DAYNAME(date) - Returning the name of week day.
  • DAYOFMONTH(date) - Returning an integer representing the day of the month.
  • DAYOFWEEK(date) - Returning an integer representing the day of the week.
  • DAYOFYEAR(date) - Returning an integer representing the day of the year.
  • EXTRACT(unit FROM date) - Returning a unit value of a date and time.
  • HOUR(time) - Returning the hour value of a time
  • LAST_DAY(date) - Returning a date representing the last day of the month based on the given date.
  • LOCALTIME() - Returning the current date and time. Same as NOW().
  • MAKETIME(hour,minute,second) - Returning a date based on the given time unit values.
  • MICROSECOND(time) - Returning the microsecond unit value of the given time.
  • MINUTE(time) - Returning the minute unit value of the given time.
  • MONTH(date) - Returning the month unit value of the given date.
  • MONTHNAME(date) - Returning the month name of the given date.
  • NOW() - Returning the current date and time.
  • SECOND(time) - Returning the second unit value of the given time.
  • SEC_TO_TIME(seconds) - Converting a second value to a time
  • STR_TO_DATE(str,format) - Converting a character string to a date.
  • SUBDATE(date,INTERVAL expr unit) - Subtracting days from a date.
  • SUBTIME(expr1,expr2) - Subtracting a time from another time.
  • SYSDATE() - Returning current date and time.
  • TIME(expr) - Returning a time from the given date and time.
  • TIMEDIFF(expr1,expr2) - Returning the difference between two times.
  • TIME_TO_SEC(time) - Converting a time to a second value.
  • WEEKDAY(date) - Returning the weekday unit value of a given date.
  • YEAR(date) - Returning the year unit value of a given date.

 

What Is TIMESTAMP Data Type in MySQL

Extract Unit Values from a Date and Time in MySQL

Introduction to Date and Time Handling in MySQL

⇑⇑ MySQL Database Tutorials

2018-03-13, 3616🔥, 0💬