MySQL DAYOFYEAR() Function

The DAYOFYEAR() function in MySQL extracts the day number within the year from a date value. This function is especially useful when you need to perform calculations or analysis based on the day of the year or for working with data that's organized by day number within a year.



DAYOFYEAR(): Definition and Usage

DAYOFYEAR() takes a date or datetime value and returns an integer representing the day of the year. The day number ranges from 1 (January 1st) to 366 (December 31st in a leap year) or 365 (in a non-leap year). The function considers only the date part of the input; the time portion of a datetime value is ignored.

Syntax

Syntax

DAYOFYEAR(date)
      

Parameter Values

Parameter Description
date The date or datetime value. This is required.

Examples

Getting the Day of the Year for a Specific Date

This example shows how to get the day of the year for June 15th, 2017.

Syntax

SELECT DAYOFYEAR("2017-06-15");
      
Output

166
      

Getting the Day of the Year for Different Dates

These examples show the day of the year for different dates. Note that 2017 was not a leap year.

Syntax

SELECT DAYOFYEAR("2017-01-01");  --January 1st
SELECT DAYOFYEAR(CURDATE());   --Today's day of the year
      
Output

1
(Today's day of the year, a number between 1 and 365 or 366)
      

**Note:** The second example's output will vary depending on the current date. The day of the year will be a number between 1 and 365 (for a non-leap year) or 1 and 366 (for a leap year).