पायथन डेटाइम (उदाहरणों के साथ)

इस लेख में, आप उदाहरणों की मदद से अजगर में तारीख और समय में हेरफेर करना सीखेंगे।

पाइथन में डेट और समय के साथ काम करने के लिए डेटाइम नाम का एक मॉड्यूल है। आइए गहराई से खोदने से पहले दिनांक और समय से संबंधित कुछ सरल कार्यक्रम बनाएं।

उदाहरण 1: वर्तमान तिथि और समय प्राप्त करें

 import datetime datetime_object = datetime.datetime.now() print(datetime_object) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट कुछ इस तरह होगा:

 2018-12-19 09: 26: 03.478039

यहां, हमने स्टेटमेंट का उपयोग करके डेटाइम मॉड्यूल आयात किया है import datetime

datetimeमॉड्यूल में परिभाषित वर्गों में से एक datetimeवर्ग है। हमने तब वर्तमान स्थानीय दिनांक और समय वाली now()एक datetimeवस्तु बनाने के लिए विधि का उपयोग किया ।

उदाहरण 2: वर्तमान तिथि प्राप्त करें

  import datetime date_object = datetime.date.today() print(date_object) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट कुछ इस तरह होगा:

 2018-12-19

इस कार्यक्रम में, हमने वर्तमान स्थानीय तिथि वाले ऑब्जेक्ट को प्राप्त करने के लिए कक्षा today()में परिभाषित विधि का उपयोग किया है ।datedate

डेटाइम के अंदर क्या है?

हम मॉड्यूल की सभी विशेषताओं वाली एक सूची प्राप्त करने के लिए dir () फ़ंक्शन का उपयोग कर सकते हैं।

 import datetime print(dir(datetime))

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 ' डेटाइम ',' डेटाइम_सीएपीआई ',' टाइम ',' टाइमडेल्टा ',' टाइमजोन ',' टिज़ीनो ’) 

आम तौर पर डेटाटाइम मॉड्यूल में उपयोग की जाने वाली कक्षाएं हैं:

  • दिनांक कक्षा
  • समय वर्ग
  • डेटाइम क्लास
  • समयबद्धता वर्ग

datetime.date क्लास

आप कक्षा dateसे वस्तुओं को तुरंत निकाल सकते हैं date। दिनांक ऑब्जेक्ट (दिनांक, वर्ष और माह) का प्रतिनिधित्व करता है।

उदाहरण 3: किसी दिनांक का प्रतिनिधित्व करने के लिए दिनांक ऑब्जेक्ट

  import datetime d = datetime.date(2019, 4, 13) print(d) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 2019-04-13

यदि आप सोच रहे हैं, date()तो उपरोक्त उदाहरण में dateकक्षा का एक निर्माता है । कंस्ट्रक्टर तीन तर्क लेता है: वर्ष, महीना और दिन।

चर एक dateवस्तु है।

हम मॉड्यूल dateसे केवल क्लास आयात कर सकते हैं datetime। ऐसे:

  from datetime import date a = date(2019, 4, 13) print(a)

उदाहरण 4: वर्तमान तिथि प्राप्त करें

आप dateएक क्लासमैथोड नाम का उपयोग करके वर्तमान दिनांक युक्त ऑब्जेक्ट बना सकते हैं today()। ऐसे:

  from datetime import date today = date.today() print("Current date =", today) 

उदाहरण 5: एक टाइमस्टैम्प से तारीख प्राप्त करें

हम dateटाइमस्टैम्प से ऑब्जेक्ट भी बना सकते हैं । यूनिक्स में एक विशेष तिथि और 1 जनवरी, 1970 के बीच एक यूनिक्स टाइमस्टैम्प सेकंड की संख्या है। आप fromtimestamp()विधि का उपयोग करके एक टाइमस्टैम्प को तिथि में बदल सकते हैं ।

  from datetime import date timestamp = date.fromtimestamp(1326244364) print("Date =", timestamp) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 दिनांक = 2012-01-11

उदाहरण 6: आज का वर्ष, महीना और दिन प्रिंट करें

हम वर्ष, माह, दिन, सप्ताह का दिन आदि दिनांक वस्तु से आसानी से प्राप्त कर सकते हैं। ऐसे:

  from datetime import date # date object of today's date today = date.today() print("Current year:", today.year) print("Current month:", today.month) print("Current day:", today.day) 

डेटाइम .टाइम

timeकक्षा से तात्कालिक समय वस्तु स्थानीय समय का प्रतिनिधित्व करती है।

उदाहरण 7: समय वस्तु समय का प्रतिनिधित्व करने के लिए

  from datetime import time # time(hour = 0, minute = 0, second = 0) a = time() print("a =", a) # time(hour, minute and second) b = time(11, 34, 56) print("b =", b) # time(hour, minute and second) c = time(hour = 11, minute = 34, second = 56) print("c =", c) # time(hour, minute, second, microsecond) d = time(11, 34, 56, 234566) print("d =", d) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 a = 00:00:00 b = 11:34:56 c = 11:34:56 d = 11: 34: 56.234566 

उदाहरण 8: प्रिंट घंटे, मिनट, दूसरा और माइक्रोसेकंड

एक बार जब आप एक timeऑब्जेक्ट बनाते हैं , तो आप इसकी विशेषताओं को आसानी से प्रिंट कर सकते हैं जैसे कि घंटा, मिनट आदि।

  from datetime import time a = time(11, 34, 56) print("hour =", a.hour) print("minute =", a.minute) print("second =", a.second) print("microsecond =", a.microsecond) 

जब आप उदाहरण चलाते हैं, तो आउटपुट होगा:

 घंटे = 11 मिनट = 34 सेकंड = 56 माइक्रोसेकंड = 0 

ध्यान दें कि हमने माइक्रोसेकंड तर्क पारित नहीं किया है। इसलिए, इसका डिफ़ॉल्ट मान 0मुद्रित किया जाता है।

datetime.datetime

The datetime module has a class named dateclass that can contain information from both date and time objects.

Example 9: Python datetime object

  from datetime import datetime #datetime(year, month, day) a = datetime(2018, 11, 28) print(a) # datetime(year, month, day, hour, minute, second, microsecond) b = datetime(2017, 11, 28, 23, 55, 59, 342380) print(b) 

When you run the program, the output will be:

 2018-11-28 00:00:00 2017-11-28 23:55:59.342380 

The first three arguments year, month and day in the datetime() constructor are mandatory.

Example 10: Print year, month, hour, minute and timestamp

  from datetime import datetime a = datetime(2017, 11, 28, 23, 55, 59, 342380) print("year =", a.year) print("month =", a.month) print("hour =", a.hour) print("minute =", a.minute) print("timestamp =", a.timestamp()) 

When you run the program, the output will be:

 year = 2017 month = 11 day = 28 hour = 23 minute = 55 timestamp = 1511913359.34238 

datetime.timedelta

A timedelta object represents the difference between two dates or times.

Example 11: Difference between two dates and times

  from datetime import datetime, date t1 = date(year = 2018, month = 7, day = 12) t2 = date(year = 2017, month = 12, day = 23) t3 = t1 - t2 print("t3 =", t3) t4 = datetime(year = 2018, month = 7, day = 12, hour = 7, minute = 9, second = 33) t5 = datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13) t6 = t4 - t5 print("t6 =", t6) print("type of t3 =", type(t3)) print("type of t6 =", type(t6)) 

When you run the program, the output will be:

 t3 = 201 days, 0:00:00 t6 = -333 days, 1:14:20 type of t3 = type of t6 = 

Notice, both t3 and t6 are of type.

Example 12: Difference between two timedelta objects

  from datetime import timedelta t1 = timedelta(weeks = 2, days = 5, hours = 1, seconds = 33) t2 = timedelta(days = 4, hours = 11, minutes = 4, seconds = 54) t3 = t1 - t2 print("t3 =", t3) 

When you run the program, the output will be:

 t3 = 14 days, 13:55:39 

Here, we have created two timedelta objects t1 and t2, and their difference is printed on the screen.

Example 13: Printing negative timedelta object

  from datetime import timedelta t1 = timedelta(seconds = 33) t2 = timedelta(seconds = 54) t3 = t1 - t2 print("t3 =", t3) print("t3 =", abs(t3)) 

When you run the program, the output will be:

 t3 = -1 day, 23:59:39 t3 = 0:00:21 

Example 14: Time duration in seconds

You can get the total number of seconds in a timedelta object using total_seconds() method.

  from datetime import timedelta t = timedelta(days = 5, hours = 1, seconds = 33, microseconds = 233423) print("total seconds =", t.total_seconds()) 

When you run the program, the output will be:

 total seconds = 435633.233423 

You can also find sum of two dates and times using + operator. Also, you can multiply and divide a timedelta object by integers and floats.

Python format datetime

The way date and time is represented may be different in different places, organizations etc. It's more common to use mm/dd/yyyy in the US, whereas dd/mm/yyyy is more common in the UK.

Python has strftime() and strptime() methods to handle this.

Python strftime() - datetime object to string

The strftime() method is defined under classes date, datetime and time. The method creates a formatted string from a given date, datetime or time object.

Example 15: Format date using strftime()

  from datetime import datetime # current date and time now = datetime.now() t = now.strftime("%H:%M:%S") print("time:", t) s1 = now.strftime("%m/%d/%Y, %H:%M:%S") # mm/dd/YY H:M:S format print("s1:", s1) s2 = now.strftime("%d/%m/%Y, %H:%M:%S") # dd/mm/YY H:M:S format print("s2:", s2) 

When you run the program, the output will be something like:

 time: 04:34:52 s1: 12/26/2018, 04:34:52 s2: 26/12/2018, 04:34:52 

Here, %Y, %m, %d, %H etc. are format codes. The strftime() method takes one or more format codes and returns a formatted string based on it.

In the above program, t, s1 and s2 are strings.

  • %Y - year (0001,… , 2018, 2019,… , 9999)
  • %m - month (01, 02,… , 11, 12)
  • %d - day (01, 02,… , 30, 31)
  • %H - hour (00, 01,… , 22, 23
  • %M - minute (00, 01,… , 58, 59)
  • %S - second (00, 01,… , 58, 59)

To learn more about strftime() and format codes, visit: Python strftime().

Python strptime() - string to datetime

The strptime() method creates a datetime object from a given string (representing date and time).

Example 16: strptime()

  from datetime import datetime date_string = "21 June, 2018" print("date_string =", date_string) date_object = datetime.strptime(date_string, "%d %B, %Y") print("date_object =", date_object) 

When you run the program, the output will be:

 date_string = 21 June, 2018 date_object = 2018-06-21 00:00:00 

The strptime() method takes two arguments:

  1. तारीख और समय का प्रतिनिधित्व करने वाला एक तार
  2. पहले तर्क के बराबर प्रारूप कोड

वैसे, %d, %Bऔर %Yप्रारूप कोड क्रमशः दिन, महीने (पूरा नाम) और वर्ष के लिए किया जाता है।

अधिक जानने के लिए पायथन स्ट्रैपटाइम () पर जाएँ।

पायथन में टाइमजोन को संभालना

मान लीजिए, आप एक परियोजना पर काम कर रहे हैं और उनके समयक्षेत्र के आधार पर तारीख और समय प्रदर्शित करने की आवश्यकता है। अपने आप को टाइमज़ोन को संभालने की कोशिश करने के बजाय, हम आपको तीसरे पक्ष के pytZ मॉड्यूल का उपयोग करने का सुझाव देते हैं।

  from datetime import datetime import pytz local = datetime.now() print("Local:", local.strftime("%m/%d/%Y, %H:%M:%S")) tz_NY = pytz.timezone('America/New_York') datetime_NY = datetime.now(tz_NY) print("NY:", datetime_NY.strftime("%m/%d/%Y, %H:%M:%S")) tz_London = pytz.timezone('Europe/London') datetime_London = datetime.now(tz_London) print("London:", datetime_London.strftime("%m/%d/%Y, %H:%M:%S")) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट कुछ इस तरह होगा:

 स्थानीय समय: 2018-12-20 13: 10: 44.260462 अमेरिका / न्यू_यॉर्क समय: 2018-12-20 13: 10: 44.260462 यूरोप / लंदन समय: 2018-12-20 13: 10: 44.260462 

यहाँ, datetime_NY और datetime_London डेटाटाइम ऑब्जेक्ट्स हैं जो अपने संबंधित समय क्षेत्र की वर्तमान तिथि और समय से युक्त हैं।

दिलचस्प लेख...