MySQL | LAST_DAY() Function

The LAST_DAY() function in MySQL can be used to know the last day of the month for a given date or a datetime. The LAST_DAY() function takes a date value as argument and returns the last day of month in that date. The date argument represents a valid date or datetime.

Syntax:

LAST_DAY( Date );

If the date or datetime value is invalid, the function returns NULL.



Parameters Used:
Date: The LAST_DAY() function takes a single argument Date. It is the date or datetime value for which you want to extract the last day of the month.

Return Value:
The LAST_DAY() function returns the last day of the month for a valid Date argument. If the argument Date is invalid or null, then the function will also return NULL.

Below are some common examples or usages of the LAST_DAY() function:

  1. Extracting last day from a given date:
    To know the last date of the month December 2017,the LAST_DAY() function can be executed in the following way:
    Syntax :

    Output :

    '2017-12-31'

  2. Extracting the last day from a given datetime:
    To know the last date of the month December using datetime format,the LAST_DAY() function can be executed in the following way:
    Syntax :

    Output :

    '2017-12-31'

  3. Checking whether it is a leap year or not:
    To know whether the year is a leap year or not, we can use the LAST_DAY() function to check the last day of the month February of that year. If it is the 29th day then that year is leap otherwise not.
    Syntax :

    Output :

    '2016-02-29'

  4. Extracting the last day for the current month:
    To know the last date of the current month ,the LAST_DAY() function is combined with the NOW() or CURDATE() function and can be executed in the following way:
    Using the NOW() function: The NOW() function in MySQL returns the current date-time stamp.
    Syntax :

    Output :

    '2017-12-31'

  5. Using the CURDATE() function: The CURDATE() function in MySQL return the current date in Date format.
    Syntax :

    Output :

    '2017-12-31'

  6. Extracting the last day of the next month:
    To know the last date of the next month,the last_day() function can be executed in the following way:
    Syntax :

    Output :

    '2018-01-31'



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.