If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
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 | +---------------------+
हालाँकि आप 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 | +-----------------------------------+-----------------------------------------+--------------------------------------------+
अगर आपको सिर्फ 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 | +-------------+-------------+
अगर आप current date और time different timezone के हिसाब से चाहिए तो आपको पहले time zone set करना पड़ेगा , नीचे दी गयी query को run करने पर current session के लिए आपका timezone set हो जायगा ।
SET time_zone = your_time_zone;
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 कर सकते हैं।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook