Difference between Python datetime vs time modules










97















I am trying to figure out the differences between the datetime and time modules, and what each should be used for.



I know that datetime provides both dates and time. What is the use of the time module?



Examples would be appreciated and differences concerning timezones would especially be of interest.










share|improve this question




























    97















    I am trying to figure out the differences between the datetime and time modules, and what each should be used for.



    I know that datetime provides both dates and time. What is the use of the time module?



    Examples would be appreciated and differences concerning timezones would especially be of interest.










    share|improve this question


























      97












      97








      97


      12






      I am trying to figure out the differences between the datetime and time modules, and what each should be used for.



      I know that datetime provides both dates and time. What is the use of the time module?



      Examples would be appreciated and differences concerning timezones would especially be of interest.










      share|improve this question
















      I am trying to figure out the differences between the datetime and time modules, and what each should be used for.



      I know that datetime provides both dates and time. What is the use of the time module?



      Examples would be appreciated and differences concerning timezones would especially be of interest.







      python datetime time






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 15 '17 at 17:59









      alex

      2,51653167




      2,51653167










      asked Sep 20 '11 at 3:26









      myusuf3myusuf3

      7,930186099




      7,930186099






















          4 Answers
          4






          active

          oldest

          votes


















          77














          the time module is principally for working with unix time stamps; expressed as a floating point number taken to be seconds since the unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.






          share|improve this answer


















          • 10





            Further, what are the differences between time and datetime.time?

            – SparkAndShine
            Nov 30 '15 at 23:13












          • Would be nice to detail properties like if it's realtime/monotonic. Like in the POSIX standard you have: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_THREAD, etc... On POSIX clocks see: stackoverflow.com/questions/3523442/…

            – Vajk Hermecz
            Jan 28 '16 at 18:30



















          4














          The time module can be used when you just need the time of a particular record - like lets say you have a seperate table/file for the transactions for each day, then you would just need the time.
          However the time datatype is usually used to store the time difference between 2 points of time.



          This can also be done using datetime, but if we are only dealing with time for a particular day, then time module can be used.



          Datetime is used to store a particular data and time for a record. Like in a rental agency. The due date would be a datetime datatype.






          share|improve this answer






























            4














            If you are interested in timezones, you should consider the use of pytz.






            share|improve this answer


















            • 4





              it sounds like hes most interested in the time and datetime modules and the differences of timezones between them. with all due respect, bringing another dependency into this is pretty much irrelevant.

              – sbartell
              Sep 20 '11 at 8:42







            • 3





              The support of timezones in both the time and datetime modules is really very poor ... if he's truly interested in handling timezones in a useful manner then he really should know about pytz. You bring in dependencies to solve the issue that your current dependencies aren't helping you solve your problem ...

              – donkopotamus
              Sep 20 '11 at 21:55


















            4














            Stick to time to prevent DST ambiguity.



            Personally, I prefer to use only the system time module in order to prevent ambiguity issues with daylight savings time (DST).



            Conversion to any time format including local time from there is pretty easy:



            import time
            t = time.time()
            t_str = time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t))


            If the system additionally runs the network time protocol (NTP) dæmon, one ends up with a pretty solid time base.






            share|improve this answer

























            • In your example, you use time.localtime(), which of course does have DST baked in. If we're going to be purists, shouldn't we use time.gmtime() instead? :)

              – Seamus
              May 6 '18 at 13:15











            • @Seamus Simply testing both commands in ipython shows that time.gmtime() yields a tuple, whereas time.time() gives the UNIX epoch time as a single decimal value of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970. The function time.localtime(t) converts the epoch time to a local time tuple. So, the answer to your question is «no».

              – Serge Stroobandt
              May 6 '18 at 17:07







            • 1





              @SergeStroobrandt R-i-i-i-ght, yes... but that wasn't really my point.

              – Seamus
              May 8 '18 at 1:04











            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7479777%2fdifference-between-python-datetime-vs-time-modules%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            77














            the time module is principally for working with unix time stamps; expressed as a floating point number taken to be seconds since the unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.






            share|improve this answer


















            • 10





              Further, what are the differences between time and datetime.time?

              – SparkAndShine
              Nov 30 '15 at 23:13












            • Would be nice to detail properties like if it's realtime/monotonic. Like in the POSIX standard you have: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_THREAD, etc... On POSIX clocks see: stackoverflow.com/questions/3523442/…

              – Vajk Hermecz
              Jan 28 '16 at 18:30
















            77














            the time module is principally for working with unix time stamps; expressed as a floating point number taken to be seconds since the unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.






            share|improve this answer


















            • 10





              Further, what are the differences between time and datetime.time?

              – SparkAndShine
              Nov 30 '15 at 23:13












            • Would be nice to detail properties like if it's realtime/monotonic. Like in the POSIX standard you have: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_THREAD, etc... On POSIX clocks see: stackoverflow.com/questions/3523442/…

              – Vajk Hermecz
              Jan 28 '16 at 18:30














            77












            77








            77







            the time module is principally for working with unix time stamps; expressed as a floating point number taken to be seconds since the unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.






            share|improve this answer













            the time module is principally for working with unix time stamps; expressed as a floating point number taken to be seconds since the unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 20 '11 at 4:00









            SingleNegationEliminationSingleNegationElimination

            113k19216262




            113k19216262







            • 10





              Further, what are the differences between time and datetime.time?

              – SparkAndShine
              Nov 30 '15 at 23:13












            • Would be nice to detail properties like if it's realtime/monotonic. Like in the POSIX standard you have: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_THREAD, etc... On POSIX clocks see: stackoverflow.com/questions/3523442/…

              – Vajk Hermecz
              Jan 28 '16 at 18:30













            • 10





              Further, what are the differences between time and datetime.time?

              – SparkAndShine
              Nov 30 '15 at 23:13












            • Would be nice to detail properties like if it's realtime/monotonic. Like in the POSIX standard you have: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_THREAD, etc... On POSIX clocks see: stackoverflow.com/questions/3523442/…

              – Vajk Hermecz
              Jan 28 '16 at 18:30








            10




            10





            Further, what are the differences between time and datetime.time?

            – SparkAndShine
            Nov 30 '15 at 23:13






            Further, what are the differences between time and datetime.time?

            – SparkAndShine
            Nov 30 '15 at 23:13














            Would be nice to detail properties like if it's realtime/monotonic. Like in the POSIX standard you have: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_THREAD, etc... On POSIX clocks see: stackoverflow.com/questions/3523442/…

            – Vajk Hermecz
            Jan 28 '16 at 18:30






            Would be nice to detail properties like if it's realtime/monotonic. Like in the POSIX standard you have: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_THREAD, etc... On POSIX clocks see: stackoverflow.com/questions/3523442/…

            – Vajk Hermecz
            Jan 28 '16 at 18:30














            4














            The time module can be used when you just need the time of a particular record - like lets say you have a seperate table/file for the transactions for each day, then you would just need the time.
            However the time datatype is usually used to store the time difference between 2 points of time.



            This can also be done using datetime, but if we are only dealing with time for a particular day, then time module can be used.



            Datetime is used to store a particular data and time for a record. Like in a rental agency. The due date would be a datetime datatype.






            share|improve this answer



























              4














              The time module can be used when you just need the time of a particular record - like lets say you have a seperate table/file for the transactions for each day, then you would just need the time.
              However the time datatype is usually used to store the time difference between 2 points of time.



              This can also be done using datetime, but if we are only dealing with time for a particular day, then time module can be used.



              Datetime is used to store a particular data and time for a record. Like in a rental agency. The due date would be a datetime datatype.






              share|improve this answer

























                4












                4








                4







                The time module can be used when you just need the time of a particular record - like lets say you have a seperate table/file for the transactions for each day, then you would just need the time.
                However the time datatype is usually used to store the time difference between 2 points of time.



                This can also be done using datetime, but if we are only dealing with time for a particular day, then time module can be used.



                Datetime is used to store a particular data and time for a record. Like in a rental agency. The due date would be a datetime datatype.






                share|improve this answer













                The time module can be used when you just need the time of a particular record - like lets say you have a seperate table/file for the transactions for each day, then you would just need the time.
                However the time datatype is usually used to store the time difference between 2 points of time.



                This can also be done using datetime, but if we are only dealing with time for a particular day, then time module can be used.



                Datetime is used to store a particular data and time for a record. Like in a rental agency. The due date would be a datetime datatype.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 20 '11 at 3:56









                Jan SJan S

                1,7031320




                1,7031320





















                    4














                    If you are interested in timezones, you should consider the use of pytz.






                    share|improve this answer


















                    • 4





                      it sounds like hes most interested in the time and datetime modules and the differences of timezones between them. with all due respect, bringing another dependency into this is pretty much irrelevant.

                      – sbartell
                      Sep 20 '11 at 8:42







                    • 3





                      The support of timezones in both the time and datetime modules is really very poor ... if he's truly interested in handling timezones in a useful manner then he really should know about pytz. You bring in dependencies to solve the issue that your current dependencies aren't helping you solve your problem ...

                      – donkopotamus
                      Sep 20 '11 at 21:55















                    4














                    If you are interested in timezones, you should consider the use of pytz.






                    share|improve this answer


















                    • 4





                      it sounds like hes most interested in the time and datetime modules and the differences of timezones between them. with all due respect, bringing another dependency into this is pretty much irrelevant.

                      – sbartell
                      Sep 20 '11 at 8:42







                    • 3





                      The support of timezones in both the time and datetime modules is really very poor ... if he's truly interested in handling timezones in a useful manner then he really should know about pytz. You bring in dependencies to solve the issue that your current dependencies aren't helping you solve your problem ...

                      – donkopotamus
                      Sep 20 '11 at 21:55













                    4












                    4








                    4







                    If you are interested in timezones, you should consider the use of pytz.






                    share|improve this answer













                    If you are interested in timezones, you should consider the use of pytz.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 20 '11 at 4:42









                    donkopotamusdonkopotamus

                    12.5k2439




                    12.5k2439







                    • 4





                      it sounds like hes most interested in the time and datetime modules and the differences of timezones between them. with all due respect, bringing another dependency into this is pretty much irrelevant.

                      – sbartell
                      Sep 20 '11 at 8:42







                    • 3





                      The support of timezones in both the time and datetime modules is really very poor ... if he's truly interested in handling timezones in a useful manner then he really should know about pytz. You bring in dependencies to solve the issue that your current dependencies aren't helping you solve your problem ...

                      – donkopotamus
                      Sep 20 '11 at 21:55












                    • 4





                      it sounds like hes most interested in the time and datetime modules and the differences of timezones between them. with all due respect, bringing another dependency into this is pretty much irrelevant.

                      – sbartell
                      Sep 20 '11 at 8:42







                    • 3





                      The support of timezones in both the time and datetime modules is really very poor ... if he's truly interested in handling timezones in a useful manner then he really should know about pytz. You bring in dependencies to solve the issue that your current dependencies aren't helping you solve your problem ...

                      – donkopotamus
                      Sep 20 '11 at 21:55







                    4




                    4





                    it sounds like hes most interested in the time and datetime modules and the differences of timezones between them. with all due respect, bringing another dependency into this is pretty much irrelevant.

                    – sbartell
                    Sep 20 '11 at 8:42






                    it sounds like hes most interested in the time and datetime modules and the differences of timezones between them. with all due respect, bringing another dependency into this is pretty much irrelevant.

                    – sbartell
                    Sep 20 '11 at 8:42





                    3




                    3





                    The support of timezones in both the time and datetime modules is really very poor ... if he's truly interested in handling timezones in a useful manner then he really should know about pytz. You bring in dependencies to solve the issue that your current dependencies aren't helping you solve your problem ...

                    – donkopotamus
                    Sep 20 '11 at 21:55





                    The support of timezones in both the time and datetime modules is really very poor ... if he's truly interested in handling timezones in a useful manner then he really should know about pytz. You bring in dependencies to solve the issue that your current dependencies aren't helping you solve your problem ...

                    – donkopotamus
                    Sep 20 '11 at 21:55











                    4














                    Stick to time to prevent DST ambiguity.



                    Personally, I prefer to use only the system time module in order to prevent ambiguity issues with daylight savings time (DST).



                    Conversion to any time format including local time from there is pretty easy:



                    import time
                    t = time.time()
                    t_str = time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t))


                    If the system additionally runs the network time protocol (NTP) dæmon, one ends up with a pretty solid time base.






                    share|improve this answer

























                    • In your example, you use time.localtime(), which of course does have DST baked in. If we're going to be purists, shouldn't we use time.gmtime() instead? :)

                      – Seamus
                      May 6 '18 at 13:15











                    • @Seamus Simply testing both commands in ipython shows that time.gmtime() yields a tuple, whereas time.time() gives the UNIX epoch time as a single decimal value of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970. The function time.localtime(t) converts the epoch time to a local time tuple. So, the answer to your question is «no».

                      – Serge Stroobandt
                      May 6 '18 at 17:07







                    • 1





                      @SergeStroobrandt R-i-i-i-ght, yes... but that wasn't really my point.

                      – Seamus
                      May 8 '18 at 1:04
















                    4














                    Stick to time to prevent DST ambiguity.



                    Personally, I prefer to use only the system time module in order to prevent ambiguity issues with daylight savings time (DST).



                    Conversion to any time format including local time from there is pretty easy:



                    import time
                    t = time.time()
                    t_str = time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t))


                    If the system additionally runs the network time protocol (NTP) dæmon, one ends up with a pretty solid time base.






                    share|improve this answer

























                    • In your example, you use time.localtime(), which of course does have DST baked in. If we're going to be purists, shouldn't we use time.gmtime() instead? :)

                      – Seamus
                      May 6 '18 at 13:15











                    • @Seamus Simply testing both commands in ipython shows that time.gmtime() yields a tuple, whereas time.time() gives the UNIX epoch time as a single decimal value of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970. The function time.localtime(t) converts the epoch time to a local time tuple. So, the answer to your question is «no».

                      – Serge Stroobandt
                      May 6 '18 at 17:07







                    • 1





                      @SergeStroobrandt R-i-i-i-ght, yes... but that wasn't really my point.

                      – Seamus
                      May 8 '18 at 1:04














                    4












                    4








                    4







                    Stick to time to prevent DST ambiguity.



                    Personally, I prefer to use only the system time module in order to prevent ambiguity issues with daylight savings time (DST).



                    Conversion to any time format including local time from there is pretty easy:



                    import time
                    t = time.time()
                    t_str = time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t))


                    If the system additionally runs the network time protocol (NTP) dæmon, one ends up with a pretty solid time base.






                    share|improve this answer















                    Stick to time to prevent DST ambiguity.



                    Personally, I prefer to use only the system time module in order to prevent ambiguity issues with daylight savings time (DST).



                    Conversion to any time format including local time from there is pretty easy:



                    import time
                    t = time.time()
                    t_str = time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t))


                    If the system additionally runs the network time protocol (NTP) dæmon, one ends up with a pretty solid time base.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 14 '18 at 23:28

























                    answered Jul 18 '17 at 9:57









                    Serge StroobandtSerge Stroobandt

                    11.3k75561




                    11.3k75561












                    • In your example, you use time.localtime(), which of course does have DST baked in. If we're going to be purists, shouldn't we use time.gmtime() instead? :)

                      – Seamus
                      May 6 '18 at 13:15











                    • @Seamus Simply testing both commands in ipython shows that time.gmtime() yields a tuple, whereas time.time() gives the UNIX epoch time as a single decimal value of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970. The function time.localtime(t) converts the epoch time to a local time tuple. So, the answer to your question is «no».

                      – Serge Stroobandt
                      May 6 '18 at 17:07







                    • 1





                      @SergeStroobrandt R-i-i-i-ght, yes... but that wasn't really my point.

                      – Seamus
                      May 8 '18 at 1:04


















                    • In your example, you use time.localtime(), which of course does have DST baked in. If we're going to be purists, shouldn't we use time.gmtime() instead? :)

                      – Seamus
                      May 6 '18 at 13:15











                    • @Seamus Simply testing both commands in ipython shows that time.gmtime() yields a tuple, whereas time.time() gives the UNIX epoch time as a single decimal value of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970. The function time.localtime(t) converts the epoch time to a local time tuple. So, the answer to your question is «no».

                      – Serge Stroobandt
                      May 6 '18 at 17:07







                    • 1





                      @SergeStroobrandt R-i-i-i-ght, yes... but that wasn't really my point.

                      – Seamus
                      May 8 '18 at 1:04

















                    In your example, you use time.localtime(), which of course does have DST baked in. If we're going to be purists, shouldn't we use time.gmtime() instead? :)

                    – Seamus
                    May 6 '18 at 13:15





                    In your example, you use time.localtime(), which of course does have DST baked in. If we're going to be purists, shouldn't we use time.gmtime() instead? :)

                    – Seamus
                    May 6 '18 at 13:15













                    @Seamus Simply testing both commands in ipython shows that time.gmtime() yields a tuple, whereas time.time() gives the UNIX epoch time as a single decimal value of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970. The function time.localtime(t) converts the epoch time to a local time tuple. So, the answer to your question is «no».

                    – Serge Stroobandt
                    May 6 '18 at 17:07






                    @Seamus Simply testing both commands in ipython shows that time.gmtime() yields a tuple, whereas time.time() gives the UNIX epoch time as a single decimal value of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970. The function time.localtime(t) converts the epoch time to a local time tuple. So, the answer to your question is «no».

                    – Serge Stroobandt
                    May 6 '18 at 17:07





                    1




                    1





                    @SergeStroobrandt R-i-i-i-ght, yes... but that wasn't really my point.

                    – Seamus
                    May 8 '18 at 1:04






                    @SergeStroobrandt R-i-i-i-ght, yes... but that wasn't really my point.

                    – Seamus
                    May 8 '18 at 1:04


















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7479777%2fdifference-between-python-datetime-vs-time-modules%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo