Python library defines a function that can be primarily used to get current time and date.
now() function Return the current local date and time, which is defined under
datetime
module.
Syntax : datetime.now(tz)
Parameters :
tz : Specified time zone of which current time and date is required. (Uses Greenwich Meridian time by default.)
Returns : Returns the current date and time in time format.
Code #1 :
Python3
# Python3 code to demonstrate
# Getting current time using
# now().
# importing datetime module for now()
import datetime
# using now() to get current time
current_time = datetime.datetime.now()
# Printing value of now.
print ("Time now at greenwich meridian is : "
, end = "")
print (current_time)
Output :
Time now at greenwich meridian is : 2018-03-29 10:26:23.473031
Attributes of now() :
now() has different attributes, same as attributes of time such as year, month, date, hour, minute, second.
Code #2 : Demonstrate attributes of now().
Python3 1==
# Python3 code to demonstrate
# attributes of now()
# importing datetime module for now()
import datetime
# using now() to get current time
current_time = datetime.datetime.now()
# Printing attributes of now().
print ("The attributes of now() are : ")
print ("Year : ", end = "")
print (current_time.year)
print ("Month : ", end = "")
print (current_time.month)
print ("Day : ", end = "")
print (current_time.day)
print ("Hour : ", end = "")
print (current_time.hour)
print ("Minute : ", end = "")
print (current_time.minute)
print ("Second : ", end = "")
print (current_time.second)
print ("Microsecond : ", end = "")
print (current_time.microsecond)
The attributes of now() are :
Year : 2018
Month : 3
Day : 26
Hour : 20
Minute : 9
Second : 4
Microsecond : 499806
Getting time of particular timezone :
Sometimes, the need is just to get the current time of a particular timezone. now() takes timezone as input to give timezone oriented output time. But these time zones are defined in
pytz library.
Code #3 : Use of now() to handle specific timezone.
Python3 1==
# Python3 code to demonstrate
# attributes of now() for timezone
# for now()
import datetime
# for timezone()
import pytz
# using now() to get current time
current_time = datetime.datetime.now(pytz.timezone('Asia / Calcutta'))
# printing current time in india
print ("The current time in india is : ")
print (current_time)
Output:
The current time in india is :
2018-03-29 03:09:33.878000+05:30
Note : Above code won't work on online IDE due to absence of
pytz module.
Application :
While developing any real world application, we might need to show real time of any timezone. now() function can do the work here very efficiently and easily.
Similar Reads
Python int() Function The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.Example:In this example, we passed a string as an argument to the int() function and printed it.Pythonage = "21" print("age =", int(age)
3 min read
pow() Function - Python pow() function in Python is a built-in tool that calculates one number raised to the power of another. It also has an optional third part that gives the remainder when dividing the result. Example:Pythonprint(pow(3,2))Output9 Explanation: pow(3, 2) calculates 32 = 9, where the base is positive and t
2 min read
Python print() function The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s
2 min read
Python len() Function The len() function in Python is used to get the number of items in an object. It is most commonly used with strings, lists, tuples, dictionaries and other iterable or container types. It returns an integer value representing the length or the number of elements. Example:Pythons = "GeeksforGeeks" # G
2 min read
sum() function in Python The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. Pythonarr = [1, 5, 2] print(sum(arr))Output8 Sum() Function in Python Syntax Syntax : sum(iterable, start) iterable : iterable can be anything list , tuples or dict
3 min read