MySQL NOW Function | NOW In MySQL In Hindi

📔 : MySQL 🔗

NOW() function , YYYY-MM-DD HH:MM:DD की form में current date और time return करता है। हालाँकि यह एक string की तरह मिलती है।

Example

SELECT NOW();

Output

+---------------------+
| NOW()               |
+---------------------+
| 2023-03-15 06:47:34 |
+---------------------+

MySQL Change Date Time Format

हालाँकि आप need के according DATE_FORMAT() function की help से date या time का format change भी कर सकते हैं।

For Example

SELECT DATE_FORMAT(NOW(), '%H:%i:%s %p'), DATE_FORMAT(NOW(), '%M-%D-%Y %H:%i:%s'), DATE_FORMAT(NOW(), '%d-%m-%y %h:%i:%s %p');

Output

+-----------------------------------+-----------------------------------------+--------------------------------------------+
| DATE_FORMAT(NOW(), '%H:%i:%s %p') | DATE_FORMAT(NOW(), '%M-%D-%Y %H:%i:%s') | DATE_FORMAT(NOW(), '%d-%m-%y %h:%i:%s %p') |
+-----------------------------------+-----------------------------------------+--------------------------------------------+
| 06:51:15 AM                       | March-15th-2023 06:51:15                | 15-03-23 06:51:15 AM                       |
+-----------------------------------+-----------------------------------------+--------------------------------------------+

Get Only Date or Time

अगर आपको सिर्फ date और time चाहिए तो आप CURDATE() या NOW() को date() और time() function की help से format भी कर सकते हैं।

SELECT DATE(now()) , TIME(NOW());

Output

+-------------+-------------+
| DATE(now()) | TIME(NOW()) |
+-------------+-------------+
| 2023-03-15  | 06:53:47    |
+-------------+-------------+

MySQL Change Timezone

अगर आप current date और time different timezone के हिसाब से चाहिए तो आपको पहले time zone set करना पड़ेगा , नीचे दी गयी query को run करने पर current session के लिए आपका timezone set हो जायगा ।

SET time_zone = your_time_zone;

MySQL Add or Subtract Time

MySQL में available predefined keywords की help से आप current date या time में एक particular value add करके date या time में calculation कर सकते हैं।

For Example

SELECT NOW() AS current_dt, (NOW() - INTERVAL 2 HOUR) AS prev_dt, NOW() + INTERVAL 4 HOUR AS next_dt;

Output

+---------------------+---------------------+---------------------+
| current_dt          | prev_dt             | next_dt             |
+---------------------+---------------------+---------------------+
| 2023-03-15 07:00:42 | 2023-03-15 05:00:42 | 2023-03-15 11:00:42 |
+---------------------+---------------------+---------------------+

ठीक इसी तरह से आप year , month या day भी add कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers