Python दिनांकसमय, समयDelta, Strftime(प्रारूप) उदाहरणों के साथ
⚡ स्मार्ट सारांश
Python datetime, along with the date, time, and timedelta classes, handles dates, times, and intervals as objects. The datetime module formats output with strftime(), parses strings, and calculates future or past moments using timedelta arithmetic.

In Python, दिनांक, समय और दिनांकसमय classes provide a number of functions to deal with dates, times and time intervals. Date and datetime in Python are objects, so when you manipulate them, you are actually manipulating objects and not strings or timestamps. Whenever you manipulate dates or time, you need to import the datetime module.
दिनांक समय कक्षाएं Python are categorized into 5 main classes.
- तारीख - केवल तारीख में हेरफेर करें (महीना, दिन, वर्ष)
- समय - दिन से स्वतंत्र समय (घंटा, मिनट, सेकंड, माइक्रोसेकंड)
- datetime - समय और तारीख का संयोजन (महीना, दिन, वर्ष, घंटा, सेकंड, माइक्रोसेकंड)
- टाइमडेल्टा - तारीखों में हेरफेर करने के लिए उपयोग की जाने वाली समय अवधि
- tzinfo— एक एब्सtracसमय क्षेत्रों से निपटने के लिए टी क्लास
दिनांक और दिनांक-समय वर्ग का उपयोग कैसे करें
चरण 1) इससे पहले कि आप datetime प्रारूप के लिए कोड चलाएँ Python, यह महत्वपूर्ण है कि आप आयात करें Python दिनांक समय मॉड्यूल जैसा कि नीचे स्क्रीनशॉट में दिखाया गया है।
ये आयात कथन कार्यक्षमता के पूर्व-परिभाषित भाग हैं Python library that let you manipulate dates and times, without writing any code.
कार्यान्वयन से पहले निम्नलिखित बिंदुओं पर विचार करें Python दिनांकसमय प्रारूप कोड
from datetime import date
यह पंक्ति बताती है Python datetime मॉड्यूल से date क्लास को आयात करने के लिए इंटरप्रेटर Python. We are not writing the code for this date functionality but are simply importing it for our use.
date.today() का उपयोग करके दिनांक प्रिंट करें
The date.today() function has several properties associated with it. We can print the individual day/month/year and many other things.
एक उदाहरण देखते हैं
आज का सप्ताह दिवस अंक
The date.today() function also gives you the weekday number. Here is the Weekday Table which starts with Monday as 0 and Sunday as 6.
| दिन | सप्ताह के दिन की संख्या |
|---|---|
| Monday | 0 |
| मंगलवार | 1 |
| बुधवार | 2 |
| गुरुवार | 3 |
| शुक्रवार | 4 |
| शनिवार | 5 |
| रविवार | 6 |
सप्ताह के दिन की संख्या उन सरणियों के लिए उपयोगी है जिनका सूचकांक सप्ताह के दिन पर निर्भर करता है।
चरण 2) इसके बाद, हम दिनांक ऑब्जेक्ट का एक उदाहरण बनाते हैं।
चरण 3) इसके बाद, हम तारीख प्रिंट करते हैं और कोड चलाते हैं।
The output is as expected.
Python वर्तमान दिनांक और समय: अब() आज()
चरण 1) दिनांक ऑब्जेक्ट्स की तरह, हम इसका भी उपयोग कर सकते हैं “दिनांक-समय ऑब्जेक्ट” in Python. Python तारीख और समय ऑब्जेक्ट समय के साथ तारीख भी देते हैं घंटे, मिनट, सेकंड और मिलीसेकंड।
जब हम datetime के लिए कोड निष्पादित करते हैं, तो यह वर्तमान दिनांक और समय के साथ आउटपुट देता है।
चरण 2) With “DATETIME OBJECT”, you can also call the time class. Suppose we want to print just the current time without the date.
t = datetime.time(datetime.now())
- हमने टाइम क्लास को आयात किया था। हम इसे datetime.now() का उपयोग करके समय का वर्तमान मान असाइन करेंगे।
- हम वर्तमान समय का मान चर t को दे रहे हैं।
और इससे मुझे समय मिल जाएगा। तो चलिए इस प्रोग्राम को चलाते हैं।
Okay, so you can see that here I got the date and time. And then the next line, I’ve got just the time by itself.
चरण 3) We will apply our weekday indexer to our weekday’s array list to know which day is today.
- Weekdays operator (wd) is assigned the number from (0-6) depending on what the current weekday is. Here we declared the array of the list for days (Mon, Tue, Wed…Sun).
- यह जानने के लिए कि आज कौन सा दिन है, उस इंडेक्स वैल्यू का इस्तेमाल करें। हमारे मामले में, यह #2 है, और यह बुधवार को दर्शाता है, इसलिए आउटपुट में यह “बुधवार कौन सा है” प्रिंट करेगा।
datetime now का उपयोग करके वर्तमान दिनांक और समय प्राप्त करने के लिए पूरा कोड यहां दिया गया है
from datetime import date from datetime import time from datetime import datetime def main(): ##DATETIME OBJECTS #Get today's date from datetime class today=datetime.now() #print (today) # Get the current time #t = datetime.time(datetime.now()) #print "The current time is", t #weekday returns 0 (monday) through 6 (sunday) wd=date.weekday(today) #Days start at 0 for monday days= ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"] print("Today is day number %d" % wd) print("which is a " + days[wd]) if __name__== "__main__": main()
Strftime() के साथ दिनांक और समय आउटपुट को कैसे प्रारूपित करें
So far, we have learned how to use datetime and date में वस्तु Pythonहम एक कदम आगे बढ़ेंगे और सीखेंगे कि समय और दिनांक को प्रारूपित करने के लिए फ़ॉर्मेटिंग फ़ंक्शन का उपयोग कैसे करें।
चरण 1) सबसे पहले हम वर्ष को प्रारूपित करने का सरल चरण देखेंगे। इसे उदाहरण से समझना बेहतर होगा।
- हम इस्तेमाल किया “strftime function” स्वरूपण के लिए.
- यह फ़ंक्शन अलग-अलग नियंत्रण कोड आउटपुट देने के लिए.
- Each control code resembles different parameters like year, month, weekday and date [(%y/%Y – वर्ष), (%a/%A- सप्ताह का दिन), (%b/%B- महीना), (%d – महीने का दिन)] .
- हमारे मामले में, यह (“%वाई”) जो वर्ष जैसा दिखता है, यह शताब्दी के साथ पूरा वर्ष प्रिंट करता है (उदाहरण के लिए, 2018)।
चरण 2) अब यदि आप (“%Y”) को लोअरकेस, यानी, ( “%y) से बदल देते हैं और कोड निष्पादित करते हैं तो आउटपुट में केवल (18) दिखाई देगा, (2018) नहीं। नीचे स्क्रीनशॉट में दिखाए अनुसार वर्ष की शताब्दी प्रदर्शित नहीं होगी।
चरण 3) Strf function can declare the date, day, month and year separately. Also with small changes in the control code in strftime function you can format the style of the text.
Inside the strftime function if you replace (%a) with capital A, i.e., (%A) the output will print out as “Friday” instead of just an abbreviation “Fri”.
चरण 4) “Strftime” फ़ंक्शन की सहायता से हम स्थानीय सिस्टम समय, दिनांक या दोनों को भी प्राप्त कर सकते हैं।
- %C- स्थानीय दिनांक और समय को इंगित करता है
- %x- स्थानीय तारीख को इंगित करता है
- %X- स्थानीय समय दर्शाता है
आउटपुट में, आप अपेक्षित परिणाम देख सकते हैं
चरण 5) "स्ट्रफ़टाइम फ़ंक्शन" आपको समय को किसी भी प्रारूप में 24 घंटे या 12 घंटे कहने की अनुमति देता है।
घंटे के लिए %I/H, मिनट के लिए %M, सेकंड के लिए %S जैसे नियंत्रण कोड को परिभाषित करके, कोई भी व्यक्ति विभिन्न प्रारूपों के लिए समय को कॉल कर सकता है
12 घंटे समय घोषित किया गया है [अभी प्रिंट करें.strftime(“%I:%M:%S %p) ]
24 घंटे समय घोषित किया गया है [अभी प्रिंट करें.strftime(“%H:%M”)]
यहां datetime को स्ट्रिंग ऑब्जेक्ट में परिवर्तित करने के लिए पूरा कोड दिया गया है।
# #Example file for formatting time and date output # from datetime import datetime def main(): #Times and dates can be formatted using a set of predefined string #Control codes now= datetime.now() #get the current date and time #%c - local date and time, %x-local's date, %X- local's time print(now.strftime("%c")) print(now.strftime("%x")) print(now.strftime("%X")) ##### Time Formatting #### #%I/%H - 12/24 Hour, %M - minute, %S - second, %p - local's AM/PM print(now.strftime("%I:%M:%S %p")) # 12-Hour:Minute:Second:AM print(now.strftime("%H:%M")) # 24-Hour:Minute if __name__== "__main__": main()
टाइमडेल्टा क्या है? Python?
टाइमडेल्टा इन Python timedelta एक ऑब्जेक्ट है जो अवधि को दर्शाता है। इसका मुख्य उपयोग दो तिथियों और समयों के बीच की अवधि की गणना करने के लिए किया जाता है। इसका उपयोग कुछ अंतर तिथि और समय वाले ऑब्जेक्ट को प्राप्त करने के लिए भी किया जाता है। timedelta ऑब्जेक्ट जोड़, घटाव आदि जैसी गणितीय संक्रियाओं का समर्थन करता है।tracभाग, गुणा, भाग आदि।
टाइमडेल्टा ऑब्जेक्ट्स का उपयोग कैसे करें
- Python टाइमडेल्टा ऑब्जेक्ट्स के माध्यम से, आप भविष्य और अतीत दोनों के लिए समय का अनुमान लगा सकते हैं। दूसरे शब्दों में, यह किसी विशेष दिन, तारीख या समय की भविष्यवाणी करने की समयावधि है।
याद रखें कि यह फ़ंक्शन समय या दिनांक प्रिंट करने के लिए नहीं है, बल्कि भविष्य या अतीत के बारे में गणना करने के लिए है. Let’s see a Python इसे बेहतर ढंग से समझने के लिए timedelta उदाहरण देखें।
चरण 1) टाइमडेल्टा ऑब्जेक्ट्स को चलाने के लिए, आपको पहले आयात कथन घोषित करना होगा और फिर कोड निष्पादित करना होगा
- टाइमडेल्टा के लिए आयात कथन लिखें
- अब स्क्रीन शॉट में दिखाए अनुसार टाइम डेल्टा से ऑब्जेक्ट प्रिंट करने के लिए कोड लिखें
- कोड चलाएँ। टाइमडेल्टा 365 दिन, 8 घंटे और 15 मिनट की अवधि को दर्शाता है और उसी को प्रिंट करता है
उलझन में हैं? अगला कदम मदद करेगा-
चरण 2) आइए आज की तारीख और समय लें ताकि हम जाँच सकें कि हमारा आयात कथन ठीक से काम कर रहा है या नहीं। जब कोड निष्पादित होता है, तो यह आज की तारीख प्रिंट करता है जिसका अर्थ है कि हमारा आयात कथन ठीक से काम कर रहा है
चरण 3) हम देखेंगे कि हम डेल्टा ऑब्जेक्ट के माध्यम से अब से एक वर्ष बाद की तारीख कैसे प्राप्त कर सकते हैं। जब हम कोड चलाते हैं, तो यह अपेक्षित आउटपुट देता है।
चरण 4) वर्तमान दिनांक और समय से भविष्य की तारीख की गणना करने के लिए समय डेल्टा का उपयोग कैसे किया जा सकता है इसका एक और उदाहरण
चरण 5) आइए एक और जटिल उदाहरण देखें। मैं यह निर्धारित करना चाहता हूँ कि नया साल कितने दिन बाद आया है। यहाँ बताया गया है कि हम कैसे आगे बढ़ेंगे
- today= date.today() का उपयोग करके हमें आज की तारीख मिलेगी
- हम जानते हैं कि नया साल हमेशा 1-जनवरी को होता है, लेकिन साल अलग भी हो सकता है। nyd= date(today.year,1,1) का उपयोग करके हम नए साल को nyd वेरिएबल में स्टोर करते हैं
- if nyd < today: compares whether the current date is greater than the new year. If yes, it enters the while loop
- ((today-nyd).days) वर्तमान तिथि और नए वर्ष के बीच का अंतर दिनों में बताता है
आउटपुट से पता चलता है कि "नया साल 11 दिन पहले ही बीत चुका है।"
यहां पूर्ण कार्यशील कोड है
# # Example file for working with timedelta objects # from datetime import date from datetime import time from datetime import datetime from datetime import timedelta # construct a basic timedelta and print it print (timedelta(days=365, hours=8, minutes=15)) # print today's date print ("today is: " + str(datetime.now())) # print today's date one year from now print ("one year from now it will be:" + str(datetime.now() + timedelta(days=365))) # create a timedelta that uses more than one argument # print (in one week and 4 days it will be " + str(datetime.now() + timedelta(weeks=1, days=4))) # How many days until New Year's Day? today = date.today() # get todays date nyd = date(today.year, 1, 1) # get New Year Day for the same year # use date comparison to see if New Year Day has already gone for this year # if it has, use the replace() function to get the date for next year if nyd < today: print ("New Year day is already went by %d days ago" % ((today - nyd).days))
Python 2 उदाहरण
The following examples show the same date and time operations written for Python 2, where print is a statement rather than a function. Use these versions only when maintaining legacy Python 2 code.
from datetime import date from datetime import time from datetime import datetime def main(): ##DATETIME OBJECTS #Get today's date from datetime class today=datetime.now() #print today # Get the current time #t = datetime.time(datetime.now()) #print "The current time is", t #weekday returns 0 (monday) through 6 (sunday) wd = date.weekday(today) #Days start at 0 for monday days= ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"] print "Today is day number %d" % wd print "which is a " + days[wd] if __name__== "__main__": main()
# #Example file for formatting time and date output # from datetime import datetime def main(): #Times and dates can be formatted using a set of predefined string #Control codes now= datetime.now() #get the current date and time #%c - local date and time, %x-local's date, %X- local's time print now.strftime("%c") print now.strftime("%x") print now.strftime("%X") ##### Time Formatting #### #%I/%H - 12/24 Hour, %M - minute, %S - second, %p - local's AM/PM print now.strftime("%I:%M:%S %p") # 12-Hour:Minute:Second:AM print now.strftime("%H:%M") # 24-Hour:Minute if __name__== "__main__": main()
# # Example file for working with timedelta objects # from datetime import date from datetime import time from datetime import datetime from datetime import timedelta # construct a basic timedelta and print it print timedelta(days=365, hours=8, minutes=15) # print today's date print "today is: " + str(datetime.now()) # print today's date one year from now print "one year from now it will be:" + str(datetime.now() + timedelta(days=365)) # create a timedelta that uses more than one argument # print "in one week and 4 days it will be " + str(datetime.now() + timedelta(weeks=1, days=4)) # How many days until New Year's Day? today = date.today() # get todays date nyd = date(today.year, 1, 1) # get New Year Day for the same year # use date comparison to see if New Year Day has already gone for this year # if it has, use the replace() function to get the date for next year if nyd < today: print "New Year day is already went by %d days ago" % ((today - nyd).days)



















