Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql to get the current date with time 23:59:59

If I execute

select current_date;

This will give the current date.

What if I have to get the current date with 23:59:59 , So that it will be end of the day.

In SQL Server I used

Select CAST(CONVERT(VARCHAR(10), GETDATE(), 110) + ' 23:59:59' AS   
DATETIME;
like image 429
Cork Kochi Avatar asked Jan 26 '16 12:01

Cork Kochi


People also ask

How can we get current date and time in MySQL?

MySQL NOW() Function The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH:MM:SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).

How would you get the current date in MySQL?

MySQL CURDATE() Function The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.

How can get date in dd mm yyyy format in MySQL?

The following is the output. The following is the query to format the date to YYYY-MM-DD. mysql> select str_to_date(LoginDate,'%d. %m.

How can I set current date and time in column in MySQL?

To insert only date value, use curdate() in MySQL. With that, if you want to get the entire datetime, then you can use now() method. Insert both date and time with the help of now().


2 Answers

One method is to add seconds:

select date_add(CURDATE(), interval 24*60*60 - 1 second)

Another method is addtime():

select addtime(CURDATE(), '23:59:59')
like image 66
Gordon Linoff Avatar answered Sep 17 '22 13:09

Gordon Linoff


Adding interval of 1 day - 1 second] would only work if the datetime / timestamp in question has time set to 00:00:00 (that's the case of CURDATE() and CURRENT_DATE).

However, if you need to work with datetime / timestamp set to, for example, 2021-08-05 02:00:00, the aforementioned method would yield 2021-08-06 01:59:59.

In such case, you may use DATE_FORMAT:

SELECT expire_at, DATE_FORMAT(expire_at, "%Y-%m-%d 23:59:59")
FROM coupons;

(tested on MySQL 8.0.18)

like image 27
sobeslavjan Avatar answered Sep 18 '22 13:09

sobeslavjan