Page 314 - CTS - CSA TP - Volume 2
P. 314

COMPUTER SOFTWARE APPLICATION - CITS




           Output:


























           TASK 3: Datetime Module
           Code:
           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”))






           •  This line imports the datetime class and the timedelta class from the datetime module. the  datetime class is
              used to represent dates and times, and the timedelta class is used to represent  the difference between two
              dates or times.






           •  This line creates a datetime object representing the current date and time.





           •  The strftime method is used to format the datetime object as a string according to the specified format. In this
              case, it formats the date and time in the “Year-Month-Day Hour:Minute:Second” format.









                                                           299

 CITS : IT & ITES - Computer Software Application - Exercise 136  CITS : IT & ITES - Computer Software Application - Exercise 136
   309   310   311   312   313   314   315   316   317   318   319