Updated April 1, 2023
Introduction to NumPy datetime
There is no data type in python to work with the dates as objects, hence to work with dates in python as data objects, we need to import a module called datetime in python. This datetime function returns the output in a format which consists of the current year, current month, current day, current hour, current minute, current second, and current microsecond, and the datetime module can also be used to create a date consisting of value for a year, a value for a month, a value for a day, value for an hour, a value for a minute, a value for the second, a value for a microsecond and value for time zone. So in this topic, we are going ot learn about NumPy datetime.
Syntax
The syntax for the NumPy datetime function in Python is as follows:
datetime.datetime.now()
This datetime function returns the output in a format which consists of the current year, current month, current day, current hour, current minute, current second, and current microsecond.
datetime.datetime(yearvalue, monthvalue, dayvalue, hourvalue, minutevalue, secondvalue. Microsecondvalue, timezonevalue)
this datetime module can be used to create a date consisting of value for a year, a value for a month, a value for a day, value for an hour, a value for a minute, a value for the second, a value for a microsecond, and value for time zone and
the value for an hour, value for a minute, value for the second, value for microsecond and value for time zone are optional, and their default value is zero and none for time zone.
Working of datetime function in NumPy
- There is no data type in python to work with the dates as objects, hence to work with dates in python as data objects, we need to import a module called datetime in python.
- This datetime function returns the output in a format which consists of the current year, current month, current day, current hour, current minute, current second, and current microsecond.
- The datetime module can also be used to create a date consisting of value for a year, a value for a month, a value for a day, a value for an hour, a value for a minute, a value for the second, a value for a microsecond, and a value for time zone.
- The value for an hour, value for a minute, value for the second, value for a microsecond, and value for a time zone are optional in the datetime module to create a date, and their default value is zero and none for time zone.
Examples of NumPy datetime
Given below are the examples of NumPy datetime:
Example #1
Python program to demonstrate NumPy datetime function by importing the datetime module and displaying the current date and time:
Code:
#the datetime module is imported to make use of datetime function
import datetime
#datetime function is used to obtain the current date and time which is stored in a variable called currentdatetime
currentdatetime = datetime.datetime.now()
print '\n'
print('The current date and time obtained using datetime function is:')
print '\n'
#the current date and time obtained using datetime function is displayed as the output on the screen
print(currentdatetime)
Output:
First, in the above program, the datetime module is imported to make use of the datetime function. Then datetime function is then used to obtain the current date and time, stored in a variable called current date-time. Then the current date and time obtained using the datetime function are displayed as the output on the screen.
Example #2
Python program to demonstrate NumPy datetime function by importing the datetime module to create a date when the year, month, and day values are passed as a parameter to the datetime function:
Code:
#the datetime module is imported to make use of datetime function
import datetime
#datetime function is used to create the date and time for the given year, month and day values which is stored in a variable called currentdatetime
currentdatetime = datetime.datetime(2020, 9, 22)
print '\n'
print('The current date and time created using datetime function is:')
print '\n'
#the current date and time created using datetime function is displayed as the output on the screen
print(currentdatetime)
Output:
First, in the above program, the datetime module is imported to make use of the datetime function. Then datetime function is then used to create the date and time for the given values of the year, month, and day stored in a variable called current date-time. Then the date and time created using the datetime function is displayed as the output on the screen.
Example #3
Python program to demonstrate NumPy datetime function by importing the datetime module to create a date when the year, month, and day values are passed as a parameter to the datetime function:
Code:
#the datetime module is imported to make use of datetime function
import datetime
#datetime function is used to create the date and time for the given year, month and day values which is stored in a variable called currentdatetime
currentdatetime = datetime.datetime(1947, 8, 15)
print '\n'
print('The current date and time created using datetime function is:')
print '\n'
#the current date and time created using datetime function is displayed as the output on the screen
print(currentdatetime)
Output:
First, in the above program, the datetime module is imported to make use of the datetime function. Then datetime function is then used to create the date and time for the given values of the year, month, and day stored in a variable called current date-time. Then the date and time created using the datetime function is displayed as the output on the screen.
Recommended Articles
This is a guide to NumPy datetime. Here we discuss the concept of NumPy datetime function in Python through definition, syntax, and working of datetime function in python through programming examples and outputs. You may also have a look at the following articles to learn more –