Net-informations.com
SiteMap  | About    

Python Date and Time

Python gives the developer several tools for working with date and time . The standard libraries contains the modules such as:

  1. datetime
  2. time
  3. calendar

datetime: datetime is a module which is designed with object-oriented programming to work with date and time in Python . It defines several classes that represent date and time.

time: time is a module that only includes functions, and constants related to date and time , there are several classes written in C/C ++ defined on this module. For example, the struct_time class.

calendar: The calendar is a module that provides functions, and several classes related to Calendar, which support generating images of the calendar as text, html,

These modules supply classes for manipulating dates and times in both simple and complex ways.

datetime Object

Datetime objects are probably one of the most used in any application. A datetime object is an instance of the datetime.datetime class that represents a single point in time. The datetime module includes functions and classes for doing date and time parsing, formatting, and arithmetic. So the datetime module can be imported like this:

How to get current time in Python?

Calendar date values are represented with the date class. Instances have attributes for year, month, and day.

example

time object

Time values are represented with the time class. Times have attributes for hour, minute, second, and microsecond.

Getting current time

example

Format the current time in Python

You can format any time as per your requirement, but a simple method can do so by calling time.strftime(format[, t]) with the current time object as the argument t. If the t argument is not provided or None, then the time_struct object returned by time.localtime is used.

example

calendar object

Python has a built-in function, calendar to work with date related tasks. It defines the Calendar class , which encapsulates calculations for values such as the dates of the weeks in a given month or year.

example

output









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.