Tech Study

Python datetime (With Examples)

Python datetime

Python has an inbuilt module known as datetime module to basically deal with the operations on dates and times. It provides us with various classes for representing and manipulating dates and times. Read this article to know about Python datetime.

Ex: Program to get present data and time

import datetime 

# get the current date and time 

now = datetime.datetime.now() 

print(now)

Output: 2022-12-27 08:26:49.219717

Here, we imported datetime module using the import datetime line of the code. The datetime class is defined in the datetime module. Here now the () method is used to create a DateTime object containing the current date and time.

Example 2: Get the Current Date

import datetime

# get current date

current_date = datetime.date.today()

print(current_date)

Output: 2022-12-27

Here we used today a () method to get the data object which contains the current date.

Attributes of datetime module

Here we will discuss some generally and commonly used attributes of datetime module 

datetime.datetime – This represents both date and time. 

Ex:

from datetime import datetime


# datetime(year, month, day)

a = datetime(2023, 03, 06)

print(a)


# datetime(year, month, day, hour, minute, second, microsecond)

b = datetime(2023, 03, 06, 23, 55, 59, 342489)

print(b)

Output: 

2023-03-06 00:00:00 

2022-03-06 23:55:59.342489

DateTime. date – This represents a only date including year, month and day. 

Ex:

import datetime 

d = datetime.date(2023, 03, 06
print(d)

Output: 2023-03-06

 DateTime.time – This represents only time including hour, minute, second, and microsecond.

from datetime import time

Ex:

a = time()

print(a)

b = time(10, 34, 56)

print(b)

c = time(hour = 10, minute = 34, second = 56)

print(c)

d = time(10, 34, 56, 234555)

print(d)

Output :

a = 00:00:00 

b = 10:34:56 

c = 10:34:56 

d = 10:34:56.234555

DateTime. time delta – This represents time duration which is used to perform arithmetic and logical operations with DateTime objects.

Ex:

import datetime 

# assign  present date and time

Present_date = datetime.datetime.now() 

date_after_2_months = present_date + datetime.timedelta(days = 30*2
print(date_after_two_months

 Output :

2023-05-05 18:25:51.901405

Python datetime Examples

Python Examples are the basic programming concepts of python like python syntax, Python datetime types, python operators, python if else, python comments etc..

Here we will discuss the commonly known python Examples.

Python datetime Syntax

The syntax of python can be directly executed in the command line.

print("Hello, World!")

Output: Hello, World!

Python Variables 

Unlike C and C++, here in python, variables are created when we  assign some value to them:

Ex:

x = 5

 y = 10

print(x)

print(y)

Output:

5

10

In Python, we don’t need to declare the variables and there doesn’t exist any command for that.

Comments 

Python also facilitates us with its commenting capability in the code. We can start the comment with # and the complete remaining line is commented.

Ex:

#This is a comment. 

print("Hello, World!")

Output: Hello, World!

Python datatypes

Python data types are used to specify the type of a variable. It specifies what type of data is going to be stored in that variable.Python has many built-in data types. They are

Numeric – int, float, complex

# integer variable. 

yy=90

 print("The datatype of ", yy, " is ", type(yy)) 

# float variable. 

x=2.45

 print("The datatype of ", x, " is ", type(x)) 

# complex variable.

 zz=3+4
print("The datatype of ", zz, " is ", type(zz))

Output:

The datatype of  90  is  <class ‘int’> 

The datatype of  2.45  is  <class ‘float’> 

The datatype of  (3+4j)  is  <class ‘complex’>

String – str

Ex:

s=”Python is fun!”

Boolean – bool

Ex:

a=True

b=False

Sequence – list, tuple, range

List: It contains a sequence of items similar to that of arrays in C and C++. They are separated by commas.

Ex of the list:

list = [ 'cool', 7.6 , 23, 'prerna', 50 ]

Tuples:Tuples are read-only lists where the tuple elements and size is fixed.

tuple = ( 'cool', 7.6 , 23, 'prerna', 50 )

Mapping – dict

They are something like hashmaps and contain key-value pairs.

dict = {} 

dict['one'] = "This is one"

print (dict['one'])

Output: This is one

Python operators: Python datetime operators are basic arithmetic operators like addition, subtraction, multiplication, and division. etc.

Ex:

x = 10

y = 5

 print(x + y)

print(x - y)

print(x * y)

print(x / y)

print(x % y)

Output:

15

5

50

2

0

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more