Press "Enter" to skip to content

Python program to extract the year, month, date using Lambda expression

In this post, we will see how to extract the year, month, and date from the provided date using a lambda expression. We will use datetime module to work with date objects.

Algorithm
Step 1: Import the datetime module
Step 2: Use datetime.datetime(2021,10,17) function to take current date time as a input. datetime() takes 3 parameters as an input, the first parameter is a year, the second is a month and the third is a day.
Step 3: Use the lamda expression to extract year part of the date.
Step 4: Use the lamda expression to extract month part of the date.
Step 5: Use the lamda expression to extract day part of the date.
Step 6: Print all the extracted data
Step 7: End

Example

Input
2021-10-17
Output
Year - 2021
Month - 10
Day - 17
Freshers Placement

Program

import datetime
currentDateTime = datetime.datetime(2021,10,17)
print(currentDateTime)
year = lambda x: x.year
month = lambda x: x.month
day = lambda x: x.day
print('Year - ',year(currentDateTime))
print('Month - ',month(currentDateTime))
print('Day - ',day(currentDateTime))Output

Output

2021-10-17 00:00:00
Year -  2021
Month -  10
Day -  17

Comments are closed.