Untitled Document
Datetime module - Working with dates and datetime
1. Program - creating simple date ; i.e printing in datetime format Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - creating simple date ; i.e printing in datetime format
       
       import datetime
       td = datetime.date(2016, 7, 24)
       print(td)

       
2. Program - To print todays date Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To print todays date
       
       import datetime
       tday = datetime.date.today()
       print(tday)
       
3. Program - How to print - day , year and month from the given date Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - How to print - day , year and month from the given date

       import datetime
       tday = datetime.date.today()
       print(tday.day)
       print(tday.month)
       print(tday.year)
       
4. Program - How to check which day in number from week Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - How to check which day in number from week
       
       import datetime
       tday = datetime.date.today()
       print(tday.weekday()) # Here Monday Starts with 0 and Sunday - 6 
       print(tday.isoweekday())# Here Monday Starts with 1 and Sunday - 7
       
5. Time delta series Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Time delta series
       # if you want to calculate the date difference 
       # i.e future date or back dates - you can do that via time deltas
       
       import datetime
       tday = datetime.date.today()            
       tdelta = datetime.timedelta(days=7)
       # This will print future date i.e date after 7 days              
       print(tday + tdelta)
       # This will print last last week date i.e 7 days before date ( one week ago)
       print(tday - tdelta)
       
       # IMP 
       # If we add or subtract time delta from date ; then we will get new date 
       # date2 = date1 + timedelta
       # OR
       # date2 = date1 - timedelta
       # If we add or subtract two dates then we will get time delta
       # timedelta = date1 + date2
       # OR 
       # timedelta = date1 - date2
       
       # Example - 2 calculating Number of days remaining for my birthday
       
       import datetime
       tday = datetime.date.today()
       bday = datetime.date(2021, 8, 25)
       till_bday = bday - tday
       print(till_bday)
       print(till_bday.days) # will just give yoi Number of days
       
6. Simple Example to print time ; in time format Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Simple Example to print time ; in time format
       
       import datetime
       t = datetime.time(9, 30, 45, 100000)            
       print(t)
 
       
7. combining date and time in single datetime function Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # combining date and time in single datetime function
       
       # IMP - datetime.date - gives date information 
       # datetime.time - gives time information
       # datetime.datetime - gives both date and time 
       
       import datetime
       dt = datetime.datetime(2021, 7, 26, 12, 30, 45, 100000)            
       print(dt)
       # You can get simply date or time by running following code on above output
       # prinitning date 
       print(dt.date())
       print(dt.time())
       print(dt.year)
 
       
8. Prinitng next week date with time Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Prinitng next week date with time
      
       import datetime
       dt = datetime.datetime(2021, 7, 26, 12, 30, 45, 100000)
       tdelta = datetime.timedelta(days=7)
       print(dt + tdelta)            
       
       
9. Adding hours to your datetime Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Adding hours to your datetime
              
       import datetime
       dt = datetime.datetime(2021, 7, 26, 12, 30, 45, 100000)
       tdelta = datetime.timedelta(hours=12)
       print(dt + tdelta)            
       
       
10. Printing date with timezone methods Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing date with timezone methods 
       # here we will just print the dates using the functions
       # we will see more pratical example later   
       
       import datetime
       dt_today = datetime.datetime.today()
       dt_now = datetime.datetime.now()
       dt_utcnow = datetime.datetime.utcnow()
       print(dt_today)   
       print(dt_now)
       print(dt_utcnow)
       
       #Example - 2 You can run string function inside f string 
        str1 = "Hello" 
        str2 = "User !!"
       print(f"{str1} , {str2.upper()} . Welcome" )         
       
       
11. getting date and time specicifc to timezones Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # getting date and time specicifc to timezones
       # To get the date and time for specific time zone you need to install pytz library 
       
       import datetime 
       import pytz
       dt_utcnow = datetime.datetime.now(tz=pytz.UTC)  
       # You can get the list of all the timezones using following for loop  
       for tz in pytz.all_timezones:   
       	print(tz)
       # printing India Current date and time 
       IST = dt_utcnow.astimezone(pytz.timezone('Asia/Calcutta'))
       print( " Indian Standard time is : {}".format(IST))
       
       
12. Printing dates in different format Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing dates in different format
              
       import datetime 
       import pytz
       dt_now = datetime.datetime.now()
       dt_utcnow = datetime.datetime.now(tz=pytz.UTC)  
       print(dt_now.strftime('%B %d, %Y'))   
       print(dt_utcnow.strftime('%B %d, %Y'))
       dt_str=dt_now.strftime('%B %d, %Y')
       
       # IMP - we have following two methods which will convert  
       # 1. strftime - Datetime to String
       # 2. strptime - String to Datetime
       # You can check date formatting via python online document
       
       dt = datetime.datetime.strptime(dt_str, '%B %d, %Y')
       print(dt)
       
       
Untitled Document