Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
id() function in Python
Next article icon

now() function in Python

Last Updated : 15 Oct, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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 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 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 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.



Next Article
id() function in Python
author
suman_709
Improve
Article Tags :
  • Python
  • Python-Built-in-functions
Practice Tags :
  • python

Similar Reads

  • ord() function in Python
    Python ord() function returns the Unicode code of a given single character. It is a modern encoding standard that aims to represent every character in every language. Unicode includes: ASCII characters (first 128 code points)Emojis, currency symbols, accented characters, etc.For example, unicode of
    2 min read
  • 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. [GFGTABS] Python age = "21"
    4 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: [GFGTABS] Python print(pow(3,2)) [/GFGTABS]Output9 Explanation: pow(3, 2) calculates 32 = 9, where the
    2 min read
  • id() function in Python
    In Python, id() function is a built-in function that returns the unique identifier of an object. The identifier is an integer, which represents the memory address of the object. The id() function is commonly used to check if two variables or objects refer to the same memory location. Python id() Fun
    3 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 input() Function
    Python input() function is used to take user input. By default, it returns the user input in form of a string. input() Function Syntax:  input(prompt)prompt [optional]: any string value to display as input message Ex: input("What is your name? ") Returns: Return a string value as input by the user.
    4 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: [GFGTABS] Python s = "G
    2 min read
  • Python oct() Function
    Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal
    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. [GFGTABS] Python arr = [1, 5, 2] print(sum(arr)) [/GFGTABS]Output8 Sum() Function in Python Syntax Syntax : sum(iterable, start) iterable : iterable can be anything
    3 min read
  • hex() function in Python
    hex() function in Python is used to convert an integer to its hexadecimal equivalent. It takes an integer as input and returns a string representing the number in hexadecimal format, starting with "0x" to indicate that it's in base-16. Example: [GFGTABS] Python a = 255 res = hex(a) print(res) [/GFGT
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences