Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Stackorigin – The Community of Question and Answers

Stackorigin – The Community of Question and Answers Logo Stackorigin – The Community of Question and Answers Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
Home/ Questions/Q 39925
Next
In Process
ashok
  • 0
ashok
Asked: June 29, 20222022-06-29T09:03:05+00:00 2022-06-29T09:03:05+00:00In: Programming

How do I get the current time?

  • 0

How do I get the current time?

pythontime
  • 2 2 Answers
  • 42 Views
  • 0 Followers
  • 0
Share
  • Facebook

    Related Questions

    • Find the second smallest number in a python list
    • Shuffle and print a specified list in python
    • Write a Python program to access only unique key value of a Python object.
    • Write a Python program to remove repeated consecutive characters
    • HTTP 503 (Service unavailable) error when using an Application Load Balancer (ALB)
    • Which is better to learn, AngularJS or Angular
    • json.dumps() in Python
    • How to Check if a File Exists in Python with isFile() and exists()
    • AttributeError: 'super' object has no attribute
    • __init__() takes 0 positional arguments but 1 was given

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. pratap
      2022-06-29T09:04:42+00:00Added an answer on June 29, 2022 at 9:04 am
      This answer was edited.

      How do I get the current time in Python:

      Use datetime:

      >>> import datetime
      >>> now = datetime.datetime.now()
      >>> now
      datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
      >>> print(now)
      2009-01-06 15:08:24.789150
      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. pratap
      2022-06-29T09:08:48+00:00Added an answer on June 29, 2022 at 9:08 am
      This answer was edited.

      How do I get the current time in Python:

      The time module

      The time module provides functions that tell us the time in “seconds since the epoch” as well as other utilities.

      import time
      

      Unix Epoch Time

      This is the format you should get timestamps in for saving in databases. It is a simple floating-point number that can be converted to an integer. It is also good for arithmetic in seconds, as it represents the number of seconds since Jan 1, 1970, 00:00:00, and it is memory light relative to the other representations of time we’ll be looking at next:

      >>> time.time()
      1424233311.771502
      

      This timestamp does not account for leap-seconds, so it’s not linear – leap seconds are ignored. So while it is not equivalent to the international UTC standard, it is close, and therefore quite good for most cases of record-keeping. This is not ideal for human scheduling, however. If you have a future event you wish to take place at a certain point in time, you’ll want to store that time with a string that can be parsed into a datetime object or a serialized datetime object (these will be described later).

      time.ctime

      You can also represent the current time in the way preferred by your operating system (which means it can change when you change your system preferences, so don’t rely on this to be standard across all systems, as I’ve seen others expect). This is typically user friendly, but doesn’t typically result in strings one can sort chronologically:

      >>> time.ctime()
      'Tue Feb 17 23:21:56 2015'
      

      You can hydrate timestamps into human readable form with ctime as well:

      >>> time.ctime(1424233311.771502)
      'Tue Feb 17 23:21:51 2015'
      

      This conversion is also not good for record-keeping (except in text that will only be parsed by humans – and with improved Optical Character Recognition and Artificial Intelligence, I think the number of these cases will diminish).

      datetime module

      The datetime module is also quite useful here:

      >>> import datetime
      

      datetime.datetime.now

      The datetime.now is a class method that returns the current time. It uses the time.localtime without the timezone info (if not given, otherwise see timezone aware below). It has a representation (which would allow you to recreate an equivalent object) echoed on the shell, but when printed (or coerced to a str), it is in human readable (and nearly ISO) format, and the lexicographic sort is equivalent to the chronological sort:

      >>> datetime.datetime.now()
      datetime.datetime(2015, 2, 17, 23, 43, 49, 94252)
      >>> print(datetime.datetime.now())
      2015-02-17 23:43:51.782461
      

      datetime’s utcnow

      You can get a datetime object in UTC time, a global standard, by doing this:

      >>> datetime.datetime.utcnow()
      datetime.datetime(2015, 2, 18, 4, 53, 28, 394163)
      >>> print(datetime.datetime.utcnow())
      2015-02-18 04:53:31.783988
      

      UTC is a time standard that is nearly equivalent to the GMT timezone. (While GMT and UTC do not change for Daylight Savings Time, their users may switch to other timezones, like British Summer Time, during the Summer.)

      datetime timezone aware

      However, none of the datetime objects we’ve created so far can be easily converted to various timezones. We can solve that problem with the pytz module:

      >>> import pytz
      >>> then = datetime.datetime.now(pytz.utc)
      >>> then
      datetime.datetime(2015, 2, 18, 4, 55, 58, 753949, tzinfo=<UTC>)
      

      Equivalently, in Python 3 we have the timezone class with a utc timezone instance attached, which also makes the object timezone aware (but to convert to another timezone without the handy pytz module is left as an exercise to the reader):

      >>> datetime.datetime.now(datetime.timezone.utc)
      datetime.datetime(2015, 2, 18, 22, 31, 56, 564191, tzinfo=datetime.timezone.utc)
      

      And we see we can easily convert to timezones from the original UTC object.

      >>> print(then)
      2015-02-18 04:55:58.753949+00:00
      >>> print(then.astimezone(pytz.timezone('US/Eastern')))
      2015-02-17 23:55:58.753949-05:00
      

      You can also make a naive datetime object aware with the pytz timezone localize method, or by replacing the tzinfo attribute (with replace, this is done blindly), but these are more last resorts than best practices:

      >>> pytz.utc.localize(datetime.datetime.utcnow())
      datetime.datetime(2015, 2, 18, 6, 6, 29, 32285, tzinfo=<UTC>)
      >>> datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
      datetime.datetime(2015, 2, 18, 6, 9, 30, 728550, tzinfo=<UTC>)
      

      The pytz module allows us to make our datetime objects timezone aware and convert the times to the hundreds of timezones available in the pytz module. One could ostensibly serialize this object for UTC time and store that in a database, but it would require far more memory and be more prone to error than simply storing the Unix Epoch time, which I demonstrated first. The other ways of viewing times are much more error-prone, especially when dealing with data that may come from different time zones. You want there to be no confusion as to which timezone a string or serialized datetime object was intended for. If you’re displaying the time with Python for the user, ctime works nicely, not in a table (it doesn’t typically sort well), but perhaps in a clock. However, I personally recommend, when dealing with time in Python, either using Unix time, or a timezone aware UTC datetime object.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Ask A Question

    Adv 120x600

    Looking for advertising?

    Consider These Things If You Appreciate What stackorigin Does: Stackorigin is a community where you can ask questions and find answers, for free to everyone. We would appreciate it if you bought us a coffee if you liked what we were doing.

    Buy me a Coffe

    Adv 120x600

    • Random
    • Answers
      • On: August 4, 2022
      • Answers: 0

      Cheap Compazine Italy, Buy compazine online without

      • On: October 18, 2022
      • Answers: 0

      Order Super Vidalista | Cialis | Tadalafil

      • On: June 16, 2022
      • Answer: 1

      How to clean up oil spills from pavement

      • On: January 28, 2022
      • Answers: 0

      Can I take 2 Dolo in a day?

      • On: December 15, 2020
      • Answer: 1

      What's the difference between putting script in head and body?

    • strapcart_online
      strapcart_online added an answer Sildenafil citrate is a common ingredient in Cenforce 150mg tablets. This drug… March 21, 2023 at 11:42 am
    • strapcart_online
      strapcart_online added an answer Vilitra 20mg tablets are used to overcome the problem of… March 18, 2023 at 6:13 am
    • ashok
      ashok added an answer Not every girl, there are some girls out there who… March 16, 2023 at 4:15 am
    • strapcart_online
      strapcart_online added an answer Tadarise 40 Tablet is used to permanently remove the problem… March 4, 2023 at 5:48 am
    • strapcart_online
      strapcart_online added an answer Men use Cenforce D tablets for the treatment of sexual… March 1, 2023 at 5:59 am

    Adv 120x600

    Trending Categories

    Health
    2042Questions
    , 4Followers
    Programming
    1236Questions
    , 0Followers
    Technology
    216Questions
    , 3Followers
    General Knowledge
    131Questions
    , 0Followers
    Business & Finance
    84Questions
    , 4Followers

    Adv 120×600

    Stats

    • Questions 4k
    • Answers 1k
    • Best Answers 105
    • Users 100

    Users

    niyadas823

    niyadas823

    • 0 Questions
    • 0 Answers
    nirala3

    nirala3

    • 2 Questions
    • 0 Answers
    Perrywalton

    Perrywalton

    • 1 Question
    • 0 Answers
    veeraa

    veeraa

    • 0 Questions
    • 0 Answers
    Stewesmitz

    Stewesmitz

    • 0 Questions
    • 0 Answers

    Adv 120×600

    Explore

    • Recent Questions
    • Most Answered
    • Answers
    • No Answers
    • Most Visited
    • Most Voted
    • Random
    • Polls

    Footer

    Stackorigin - The Community of Question and Answers

    Stackorigin

    Stackorigin is the world’s largest Q&A networking site, Stackorigin community brings you the collaboration of all the various Questions and the related Answers given by the community.

    About

    • About Us
    • Contact Us
    • FAQ
    • Submit Guest Post Article on Technology, Education, Health, Apps, Gadgets, IoT, AI, Business, Digital Marketing and More

    Info

    • Privacy Policy
    • Terms and Conditions
    • Community Guidelines
    • Tags

    Products

    • Tutorials
    • Advertising
    • Categories
    • Corona
    • StackHow

    Follow

    © 2022 Stackorigin. All Rights Reserved.

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.