Convert Time to EpochSecond





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am new to python. Currently, I have a timedata like "2018-11-15 13:34:40.000 EST". I want to convert it into EpochSecond.



I know how to use dateparser to get this, however, I want to know is there a simple way to do this without dateparser?



import dateparser
from datetime import datetime, timezone

mytime = "2018-11-15 13:34:40.000 EST"

dateVar = dateparser.parse(mytime)

print(dateVar)

epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)

print((dateVar - epoch).total_seconds())









share|improve this question





























    0















    I am new to python. Currently, I have a timedata like "2018-11-15 13:34:40.000 EST". I want to convert it into EpochSecond.



    I know how to use dateparser to get this, however, I want to know is there a simple way to do this without dateparser?



    import dateparser
    from datetime import datetime, timezone

    mytime = "2018-11-15 13:34:40.000 EST"

    dateVar = dateparser.parse(mytime)

    print(dateVar)

    epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)

    print((dateVar - epoch).total_seconds())









    share|improve this question

























      0












      0








      0








      I am new to python. Currently, I have a timedata like "2018-11-15 13:34:40.000 EST". I want to convert it into EpochSecond.



      I know how to use dateparser to get this, however, I want to know is there a simple way to do this without dateparser?



      import dateparser
      from datetime import datetime, timezone

      mytime = "2018-11-15 13:34:40.000 EST"

      dateVar = dateparser.parse(mytime)

      print(dateVar)

      epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)

      print((dateVar - epoch).total_seconds())









      share|improve this question














      I am new to python. Currently, I have a timedata like "2018-11-15 13:34:40.000 EST". I want to convert it into EpochSecond.



      I know how to use dateparser to get this, however, I want to know is there a simple way to do this without dateparser?



      import dateparser
      from datetime import datetime, timezone

      mytime = "2018-11-15 13:34:40.000 EST"

      dateVar = dateparser.parse(mytime)

      print(dateVar)

      epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)

      print((dateVar - epoch).total_seconds())






      python python-3.x epoch






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 18:43









      Amen KingAmen King

      226




      226
























          2 Answers
          2






          active

          oldest

          votes


















          0














          datetime.datetime.timestamp() is what you're looking for (relevant part):




          For aware datetime instances, the return value is computed as:



          (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()




          Example:



          import datetime

          now = datetime.datetime.now()
          epoch = now.timestamp()

          # 1542394106.155199


          Implemented into your example, we'll have to use another approach as datetime.datetime.strptime() doesn't quite take timezone kindly due to a bug (relevant question where I found the info). So we'll have to use another builtin to parse it (example here):



          from dateutil.parser import parse

          mytime = "2018-11-12 00:30:20.000 EST"
          dt = parse(mytime)
          epoch = dt.timestamp()


          The parsed string is still a datetime.datetime object so you can handle it the same after parsing.



          Note: however parse might complain it read the timezone but not understood it:




           UnknownTimezoneWarning: tzname EDT identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.



          You might end up needing to pass the tzinfos into the parse() method anyhow:



          from dateutil.parser import parse
          # from dateutil.tz import gettz # <-- can use if you know the tz to retrieve

          tzinfos = {'EST': -18000, 'CET': +3600}
          # another example: {"CST": gettz("America/Chicago")}
          mytime = "2018-11-12 00:30:20.000 EST"
          dt = parse(mytime, tzinfos=tzinfos)
          epoch = dt.timestamp()
          print(epoch)


          So I guess in the end it's not as simple as you might want.






          share|improve this answer


























          • Hi Idel, could you be able to explain more about how to use this ? for example, my time is "2018-11-12 00:30:20.000 EST"

            – Amen King
            Nov 16 '18 at 19:00











          • You'll have to convert your string to a datetime object first of course. If you don't know a particular format or aren't sure how to convert it, using dateparser is one way. If the format is consistent you can just use .strptime() method from datetime module and convert the string first, then convert to epoch time.

            – Idlehands
            Nov 16 '18 at 19:03











          • Note in your example with the 'EST' time zone, it becomes a bit more problematic as there is a bug in strptime() to process some time zone. If dateparser is already working for you, why not rely on that instead? otherwise you'll have to manually parse the timezone and pass it into the datetime object itself.

            – Idlehands
            Nov 16 '18 at 19:10











          • You are right, because I can not install the module on my company machine. So I am trying to see if I can parse the data manually.

            – Amen King
            Nov 16 '18 at 19:14













          • @AmenKing Okay I found another way to do this with a different built in. See my edited answer.

            – Idlehands
            Nov 16 '18 at 19:23



















          0














          Try:



          from datetime import datetime

          epoch = datetime.datetime(1970,1,1)
          i = datetime.now()

          delta_time = (i - epoch).total_seconds()





          share|improve this answer
























            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%2f53343664%2fconvert-time-to-epochsecond%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            datetime.datetime.timestamp() is what you're looking for (relevant part):




            For aware datetime instances, the return value is computed as:



            (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()




            Example:



            import datetime

            now = datetime.datetime.now()
            epoch = now.timestamp()

            # 1542394106.155199


            Implemented into your example, we'll have to use another approach as datetime.datetime.strptime() doesn't quite take timezone kindly due to a bug (relevant question where I found the info). So we'll have to use another builtin to parse it (example here):



            from dateutil.parser import parse

            mytime = "2018-11-12 00:30:20.000 EST"
            dt = parse(mytime)
            epoch = dt.timestamp()


            The parsed string is still a datetime.datetime object so you can handle it the same after parsing.



            Note: however parse might complain it read the timezone but not understood it:




             UnknownTimezoneWarning: tzname EDT identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.



            You might end up needing to pass the tzinfos into the parse() method anyhow:



            from dateutil.parser import parse
            # from dateutil.tz import gettz # <-- can use if you know the tz to retrieve

            tzinfos = {'EST': -18000, 'CET': +3600}
            # another example: {"CST": gettz("America/Chicago")}
            mytime = "2018-11-12 00:30:20.000 EST"
            dt = parse(mytime, tzinfos=tzinfos)
            epoch = dt.timestamp()
            print(epoch)


            So I guess in the end it's not as simple as you might want.






            share|improve this answer


























            • Hi Idel, could you be able to explain more about how to use this ? for example, my time is "2018-11-12 00:30:20.000 EST"

              – Amen King
              Nov 16 '18 at 19:00











            • You'll have to convert your string to a datetime object first of course. If you don't know a particular format or aren't sure how to convert it, using dateparser is one way. If the format is consistent you can just use .strptime() method from datetime module and convert the string first, then convert to epoch time.

              – Idlehands
              Nov 16 '18 at 19:03











            • Note in your example with the 'EST' time zone, it becomes a bit more problematic as there is a bug in strptime() to process some time zone. If dateparser is already working for you, why not rely on that instead? otherwise you'll have to manually parse the timezone and pass it into the datetime object itself.

              – Idlehands
              Nov 16 '18 at 19:10











            • You are right, because I can not install the module on my company machine. So I am trying to see if I can parse the data manually.

              – Amen King
              Nov 16 '18 at 19:14













            • @AmenKing Okay I found another way to do this with a different built in. See my edited answer.

              – Idlehands
              Nov 16 '18 at 19:23
















            0














            datetime.datetime.timestamp() is what you're looking for (relevant part):




            For aware datetime instances, the return value is computed as:



            (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()




            Example:



            import datetime

            now = datetime.datetime.now()
            epoch = now.timestamp()

            # 1542394106.155199


            Implemented into your example, we'll have to use another approach as datetime.datetime.strptime() doesn't quite take timezone kindly due to a bug (relevant question where I found the info). So we'll have to use another builtin to parse it (example here):



            from dateutil.parser import parse

            mytime = "2018-11-12 00:30:20.000 EST"
            dt = parse(mytime)
            epoch = dt.timestamp()


            The parsed string is still a datetime.datetime object so you can handle it the same after parsing.



            Note: however parse might complain it read the timezone but not understood it:




             UnknownTimezoneWarning: tzname EDT identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.



            You might end up needing to pass the tzinfos into the parse() method anyhow:



            from dateutil.parser import parse
            # from dateutil.tz import gettz # <-- can use if you know the tz to retrieve

            tzinfos = {'EST': -18000, 'CET': +3600}
            # another example: {"CST": gettz("America/Chicago")}
            mytime = "2018-11-12 00:30:20.000 EST"
            dt = parse(mytime, tzinfos=tzinfos)
            epoch = dt.timestamp()
            print(epoch)


            So I guess in the end it's not as simple as you might want.






            share|improve this answer


























            • Hi Idel, could you be able to explain more about how to use this ? for example, my time is "2018-11-12 00:30:20.000 EST"

              – Amen King
              Nov 16 '18 at 19:00











            • You'll have to convert your string to a datetime object first of course. If you don't know a particular format or aren't sure how to convert it, using dateparser is one way. If the format is consistent you can just use .strptime() method from datetime module and convert the string first, then convert to epoch time.

              – Idlehands
              Nov 16 '18 at 19:03











            • Note in your example with the 'EST' time zone, it becomes a bit more problematic as there is a bug in strptime() to process some time zone. If dateparser is already working for you, why not rely on that instead? otherwise you'll have to manually parse the timezone and pass it into the datetime object itself.

              – Idlehands
              Nov 16 '18 at 19:10











            • You are right, because I can not install the module on my company machine. So I am trying to see if I can parse the data manually.

              – Amen King
              Nov 16 '18 at 19:14













            • @AmenKing Okay I found another way to do this with a different built in. See my edited answer.

              – Idlehands
              Nov 16 '18 at 19:23














            0












            0








            0







            datetime.datetime.timestamp() is what you're looking for (relevant part):




            For aware datetime instances, the return value is computed as:



            (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()




            Example:



            import datetime

            now = datetime.datetime.now()
            epoch = now.timestamp()

            # 1542394106.155199


            Implemented into your example, we'll have to use another approach as datetime.datetime.strptime() doesn't quite take timezone kindly due to a bug (relevant question where I found the info). So we'll have to use another builtin to parse it (example here):



            from dateutil.parser import parse

            mytime = "2018-11-12 00:30:20.000 EST"
            dt = parse(mytime)
            epoch = dt.timestamp()


            The parsed string is still a datetime.datetime object so you can handle it the same after parsing.



            Note: however parse might complain it read the timezone but not understood it:




             UnknownTimezoneWarning: tzname EDT identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.



            You might end up needing to pass the tzinfos into the parse() method anyhow:



            from dateutil.parser import parse
            # from dateutil.tz import gettz # <-- can use if you know the tz to retrieve

            tzinfos = {'EST': -18000, 'CET': +3600}
            # another example: {"CST": gettz("America/Chicago")}
            mytime = "2018-11-12 00:30:20.000 EST"
            dt = parse(mytime, tzinfos=tzinfos)
            epoch = dt.timestamp()
            print(epoch)


            So I guess in the end it's not as simple as you might want.






            share|improve this answer















            datetime.datetime.timestamp() is what you're looking for (relevant part):




            For aware datetime instances, the return value is computed as:



            (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()




            Example:



            import datetime

            now = datetime.datetime.now()
            epoch = now.timestamp()

            # 1542394106.155199


            Implemented into your example, we'll have to use another approach as datetime.datetime.strptime() doesn't quite take timezone kindly due to a bug (relevant question where I found the info). So we'll have to use another builtin to parse it (example here):



            from dateutil.parser import parse

            mytime = "2018-11-12 00:30:20.000 EST"
            dt = parse(mytime)
            epoch = dt.timestamp()


            The parsed string is still a datetime.datetime object so you can handle it the same after parsing.



            Note: however parse might complain it read the timezone but not understood it:




             UnknownTimezoneWarning: tzname EDT identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.



            You might end up needing to pass the tzinfos into the parse() method anyhow:



            from dateutil.parser import parse
            # from dateutil.tz import gettz # <-- can use if you know the tz to retrieve

            tzinfos = {'EST': -18000, 'CET': +3600}
            # another example: {"CST": gettz("America/Chicago")}
            mytime = "2018-11-12 00:30:20.000 EST"
            dt = parse(mytime, tzinfos=tzinfos)
            epoch = dt.timestamp()
            print(epoch)


            So I guess in the end it's not as simple as you might want.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 19:46

























            answered Nov 16 '18 at 18:48









            IdlehandsIdlehands

            6,1631923




            6,1631923













            • Hi Idel, could you be able to explain more about how to use this ? for example, my time is "2018-11-12 00:30:20.000 EST"

              – Amen King
              Nov 16 '18 at 19:00











            • You'll have to convert your string to a datetime object first of course. If you don't know a particular format or aren't sure how to convert it, using dateparser is one way. If the format is consistent you can just use .strptime() method from datetime module and convert the string first, then convert to epoch time.

              – Idlehands
              Nov 16 '18 at 19:03











            • Note in your example with the 'EST' time zone, it becomes a bit more problematic as there is a bug in strptime() to process some time zone. If dateparser is already working for you, why not rely on that instead? otherwise you'll have to manually parse the timezone and pass it into the datetime object itself.

              – Idlehands
              Nov 16 '18 at 19:10











            • You are right, because I can not install the module on my company machine. So I am trying to see if I can parse the data manually.

              – Amen King
              Nov 16 '18 at 19:14













            • @AmenKing Okay I found another way to do this with a different built in. See my edited answer.

              – Idlehands
              Nov 16 '18 at 19:23



















            • Hi Idel, could you be able to explain more about how to use this ? for example, my time is "2018-11-12 00:30:20.000 EST"

              – Amen King
              Nov 16 '18 at 19:00











            • You'll have to convert your string to a datetime object first of course. If you don't know a particular format or aren't sure how to convert it, using dateparser is one way. If the format is consistent you can just use .strptime() method from datetime module and convert the string first, then convert to epoch time.

              – Idlehands
              Nov 16 '18 at 19:03











            • Note in your example with the 'EST' time zone, it becomes a bit more problematic as there is a bug in strptime() to process some time zone. If dateparser is already working for you, why not rely on that instead? otherwise you'll have to manually parse the timezone and pass it into the datetime object itself.

              – Idlehands
              Nov 16 '18 at 19:10











            • You are right, because I can not install the module on my company machine. So I am trying to see if I can parse the data manually.

              – Amen King
              Nov 16 '18 at 19:14













            • @AmenKing Okay I found another way to do this with a different built in. See my edited answer.

              – Idlehands
              Nov 16 '18 at 19:23

















            Hi Idel, could you be able to explain more about how to use this ? for example, my time is "2018-11-12 00:30:20.000 EST"

            – Amen King
            Nov 16 '18 at 19:00





            Hi Idel, could you be able to explain more about how to use this ? for example, my time is "2018-11-12 00:30:20.000 EST"

            – Amen King
            Nov 16 '18 at 19:00













            You'll have to convert your string to a datetime object first of course. If you don't know a particular format or aren't sure how to convert it, using dateparser is one way. If the format is consistent you can just use .strptime() method from datetime module and convert the string first, then convert to epoch time.

            – Idlehands
            Nov 16 '18 at 19:03





            You'll have to convert your string to a datetime object first of course. If you don't know a particular format or aren't sure how to convert it, using dateparser is one way. If the format is consistent you can just use .strptime() method from datetime module and convert the string first, then convert to epoch time.

            – Idlehands
            Nov 16 '18 at 19:03













            Note in your example with the 'EST' time zone, it becomes a bit more problematic as there is a bug in strptime() to process some time zone. If dateparser is already working for you, why not rely on that instead? otherwise you'll have to manually parse the timezone and pass it into the datetime object itself.

            – Idlehands
            Nov 16 '18 at 19:10





            Note in your example with the 'EST' time zone, it becomes a bit more problematic as there is a bug in strptime() to process some time zone. If dateparser is already working for you, why not rely on that instead? otherwise you'll have to manually parse the timezone and pass it into the datetime object itself.

            – Idlehands
            Nov 16 '18 at 19:10













            You are right, because I can not install the module on my company machine. So I am trying to see if I can parse the data manually.

            – Amen King
            Nov 16 '18 at 19:14







            You are right, because I can not install the module on my company machine. So I am trying to see if I can parse the data manually.

            – Amen King
            Nov 16 '18 at 19:14















            @AmenKing Okay I found another way to do this with a different built in. See my edited answer.

            – Idlehands
            Nov 16 '18 at 19:23





            @AmenKing Okay I found another way to do this with a different built in. See my edited answer.

            – Idlehands
            Nov 16 '18 at 19:23













            0














            Try:



            from datetime import datetime

            epoch = datetime.datetime(1970,1,1)
            i = datetime.now()

            delta_time = (i - epoch).total_seconds()





            share|improve this answer




























              0














              Try:



              from datetime import datetime

              epoch = datetime.datetime(1970,1,1)
              i = datetime.now()

              delta_time = (i - epoch).total_seconds()





              share|improve this answer


























                0












                0








                0







                Try:



                from datetime import datetime

                epoch = datetime.datetime(1970,1,1)
                i = datetime.now()

                delta_time = (i - epoch).total_seconds()





                share|improve this answer













                Try:



                from datetime import datetime

                epoch = datetime.datetime(1970,1,1)
                i = datetime.now()

                delta_time = (i - epoch).total_seconds()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 18:45









                TNoTNo

                630320




                630320






























                    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%2f53343664%2fconvert-time-to-epochsecond%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

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python