Gonzalez
Gonzalez

Reputation: 41

How to find number of days in the current year in MySQL

I have this problem I want to calculate number days of the current year. somewhat like this

SELECT DAYOFYEAR('2016-12-31');
Ans:366

but without mentioning the date I tried this but it is giving me current day of the year

SELECT DAYOFYEAR(CURDATE());  

Please help

Upvotes: 4

Views: 5071

Answers (5)

Andris
Andris

Reputation: 5370

This is a simple solution to find the number of days in a given year you specify using any date within that year. Calculating the date difference between a year's first day, to the next year's first day.

DATEDIFF(MAKEDATE(YEAR(:date) + 1, 1), MAKEDATE(YEAR(:date), 1))

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94642

I am sure there are other ways but this is simple and works

SELECT DAYOFYEAR(CONCAT(YEAR(CURDATE()),'-12-31')) as days_in_year;

Upvotes: 3

William
William

Reputation: 305

SELECT DAYOFYEAR(DATE_FORMAT('2018-09-14', '%Y-12-31')); -- 365 days
SELECT DAYOFYEAR(DATE_FORMAT('2019-09-14', '%Y-12-31')); -- 365 days
SELECT DAYOFYEAR(DATE_FORMAT('2020-09-14', '%Y-12-31')); -- 366 days!!!
SELECT DAYOFYEAR(DATE_FORMAT('2021-09-14', '%Y-12-31')); -- 365 days
SELECT DAYOFYEAR(DATE_FORMAT('2022-09-14', '%Y-12-31')); -- 365 days

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269573

How about something like this?

select datediff(date(concat(year(curdate()) + 1, '-01-01')),
                date(concat(year(curdate()), '-01-01'))
               )

Here is an example of it working.

Upvotes: 0

arkascha
arkascha

Reputation: 42895

This should work too:

SELECT DAYOFYEAR(
    LAST_DAY(DATE_ADD(NOW(), INTERVAL 12-MONTH(NOW()) MONTH))
) AS NumberOfDaysInCurrentYear;

Upvotes: 2

Related Questions