Page 313 - CITS - CSA - TP (Volume 2) - Hindi
P. 313
कं ूटर सॉ वेयर ए ीके शन - CITS
आउटपुट:
टा 3: िदनांक-समय मॉ ूल
कोड:
from datetime import datetime, timedelta
# Example of working with dates and times
current_time = datetime.now() # Get current date and time
formatted_time = current_time.strftime(“%Y-%m-%d %H:%M:%S”) # Format the time as a string
# Example of adding and subtracting time
one_day_later = current_time + timedelta(days=1)
two_hours_earlier = current_time - timedelta(hours=2)
print(formatted_time)
print(one_day_later.strftime(“%Y-%m-%d %H:%M:%S”))
print(two_hours_earlier.strftime(“%Y-%m-%d %H:%M:%S”))
• यह पं datetime ास और timedelta ास को datetime मॉ ूल से इंपोट करती है। datetime ास का उपयोग िदनांकों और समयों
को दशा ने के िलए िकया जाता है, और timedelta ास का उपयोग दो ितिथयों या समयों के बीच अंतर को दशा ने के िलए िकया जाता है।
• यह पं वत मान िदनांक और समय का ितिनिध करने वाला datetime ऑ े बनाती है।
• strftime िविध का उपयोग datetime ऑ े को िनिद ा प के अनुसार ंग के प म फॉम ट करने के िलए िकया जाता है। इस मामले म ,
यह िदनांक और समय को “Year-Month-Day Hour:Minute:Second” फॉम ट म फॉम ट करता है।
299
CITS : IT & ITES - कं ूटर सॉ वेयर ए ीके शन - अ ास 136

