Python Identify (Convert) text to dateHow to get the datetime from a string containing '2nd' for the date in Python?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How do I get the current date in JavaScript?Does Python have a string 'contains' substring method?How to format a JavaScript datePythonic way to create a long multi-line string

Is の方 necessary here?

Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?

Motorcyle Chain needs to be cleaned every time you lube it?

Are "confidant" and "confident" homophones?

Does the "divide by 4 rule" give the upper bound marginal effect?

Find max number you can create from an array of numbers

Why no parachutes in the Orion AA2 abort test?

How did the IEC decide to create kibibytes?

How frequently do Russian people still refer to others by their patronymic (отчество)?

What's the big deal about the Nazgûl losing their horses?

2000s (or earlier) cyberpunk novel: dystopia, mega storm, omnipresent noise

Shipped package arrived - didn't order, possible scam?

Should I increase my 401(k) contributions, or increase my mortgage payments

Should I cheat if the majority does it?

How do I check that users don't write down their passwords?

Was I wrongfully denied boarding for having a Schengen visa issued from the second country on my itinerary?

Convert Front Door Entry Mailbox to a Front & Rear Door Entry Mailbox

PhD: When to quit and move on?

Why do we need a bootloader separate from our application program in microcontrollers?

Taking advantage when the HR forgets to communicate the rules

What can a novel do that film and TV cannot?

What are some bad ways to subvert tropes?

Stay in US on J-1 visa after quitting job?

How important is it for multiple POVs to run chronologically?



Python Identify (Convert) text to date


How to get the datetime from a string containing '2nd' for the date in Python?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How do I get the current date in JavaScript?Does Python have a string 'contains' substring method?How to format a JavaScript datePythonic way to create a long multi-line string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am trying to find best way to convert "Friday 1st March 2019" or "Saturday 2nd March 2019" to python datetime object.



I tried by splitting, than had thoughts on regex, but I am quite sure there is more 'elegant' way ofdoing it



From string "Friday 1st March 2019" I expect 01-03-2019 or 2019-03-01



TNX!










share|improve this question




























    1















    I am trying to find best way to convert "Friday 1st March 2019" or "Saturday 2nd March 2019" to python datetime object.



    I tried by splitting, than had thoughts on regex, but I am quite sure there is more 'elegant' way ofdoing it



    From string "Friday 1st March 2019" I expect 01-03-2019 or 2019-03-01



    TNX!










    share|improve this question
























      1












      1








      1








      I am trying to find best way to convert "Friday 1st March 2019" or "Saturday 2nd March 2019" to python datetime object.



      I tried by splitting, than had thoughts on regex, but I am quite sure there is more 'elegant' way ofdoing it



      From string "Friday 1st March 2019" I expect 01-03-2019 or 2019-03-01



      TNX!










      share|improve this question














      I am trying to find best way to convert "Friday 1st March 2019" or "Saturday 2nd March 2019" to python datetime object.



      I tried by splitting, than had thoughts on regex, but I am quite sure there is more 'elegant' way ofdoing it



      From string "Friday 1st March 2019" I expect 01-03-2019 or 2019-03-01



      TNX!







      python date formatting






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 20:00









      JovanJovan

      7513 bronze badges




      7513 bronze badges






















          3 Answers
          3






          active

          oldest

          votes


















          6














          Maybe not the "best" way, but a very easy way is dateutil's parser



          from dateutil import parser
          parser.parse("Friday 1st March 2019")


          Returns:



          datetime.datetime(2019, 3, 1, 0, 0)


          It can pretty much be wrapped up as:



          from dateutil import parser
          from datetime import datetime as dt
          dt.strftime(parser.parse("Friday 1st March 2019"), "%d-%m-%Y")


          Returning:



          '01-03-2019'





          share|improve this answer

























          • Exactly what I have been asking for! Thank you tgikal!

            – Jovan
            Mar 25 at 22:18


















          1














          Please refer to an already answered question:
          How to get the datetime from a string containing '2nd' for the date in Python?



          As I can only repeat, solution is to use dateutil parser:



          from dateutil.parser import parse

          your_string = "Friday 1st March 2019"
          date_obj = parse(your_string)


          Behind the scenes, I guess the "1st", and "2nd" parts are extracted somehow (e.g. splitting+regex) and simplified to its day value only.
          According to an input like that, the common datetime library can be used with proper date format string:



          import datetime
          simplified_txt = "Friday 1 March 2019"
          datetime_obj = datetime.datetime.strptime(simplified_txt,"%A %d %B %Y")





          share|improve this answer
































            1














            You are going to face issues with 1st, 2nd.



            So, try this (without adding any external/third party library):



            import re
            from datetime import datetime as dt
            ds = "Friday 1st March 2019"
            parts = ds.split(" ")
            ds = " ".format(
            parts[0],
            re.sub('[^0-9]','', parts[1]),
            parts[2],
            parts[3]
            )
            a = dt.strptime(ds, "%A %d %B %Y")


            If you want to make it into a function, do this:



            def convdate(s):
            parts = s.split(" ")
            ds = " ".format(
            parts[0],
            re.sub('[^0-9]','', parts[1]),
            parts[2],
            parts[3]
            )
            return dt.strptime(ds, "%A %d %B %Y")





            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%2f55345550%2fpython-identify-convert-text-to-date%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6














              Maybe not the "best" way, but a very easy way is dateutil's parser



              from dateutil import parser
              parser.parse("Friday 1st March 2019")


              Returns:



              datetime.datetime(2019, 3, 1, 0, 0)


              It can pretty much be wrapped up as:



              from dateutil import parser
              from datetime import datetime as dt
              dt.strftime(parser.parse("Friday 1st March 2019"), "%d-%m-%Y")


              Returning:



              '01-03-2019'





              share|improve this answer

























              • Exactly what I have been asking for! Thank you tgikal!

                – Jovan
                Mar 25 at 22:18















              6














              Maybe not the "best" way, but a very easy way is dateutil's parser



              from dateutil import parser
              parser.parse("Friday 1st March 2019")


              Returns:



              datetime.datetime(2019, 3, 1, 0, 0)


              It can pretty much be wrapped up as:



              from dateutil import parser
              from datetime import datetime as dt
              dt.strftime(parser.parse("Friday 1st March 2019"), "%d-%m-%Y")


              Returning:



              '01-03-2019'





              share|improve this answer

























              • Exactly what I have been asking for! Thank you tgikal!

                – Jovan
                Mar 25 at 22:18













              6












              6








              6







              Maybe not the "best" way, but a very easy way is dateutil's parser



              from dateutil import parser
              parser.parse("Friday 1st March 2019")


              Returns:



              datetime.datetime(2019, 3, 1, 0, 0)


              It can pretty much be wrapped up as:



              from dateutil import parser
              from datetime import datetime as dt
              dt.strftime(parser.parse("Friday 1st March 2019"), "%d-%m-%Y")


              Returning:



              '01-03-2019'





              share|improve this answer















              Maybe not the "best" way, but a very easy way is dateutil's parser



              from dateutil import parser
              parser.parse("Friday 1st March 2019")


              Returns:



              datetime.datetime(2019, 3, 1, 0, 0)


              It can pretty much be wrapped up as:



              from dateutil import parser
              from datetime import datetime as dt
              dt.strftime(parser.parse("Friday 1st March 2019"), "%d-%m-%Y")


              Returning:



              '01-03-2019'






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 25 at 20:29

























              answered Mar 25 at 20:21









              tgikaltgikal

              9681 gold badge6 silver badges14 bronze badges




              9681 gold badge6 silver badges14 bronze badges












              • Exactly what I have been asking for! Thank you tgikal!

                – Jovan
                Mar 25 at 22:18

















              • Exactly what I have been asking for! Thank you tgikal!

                – Jovan
                Mar 25 at 22:18
















              Exactly what I have been asking for! Thank you tgikal!

              – Jovan
              Mar 25 at 22:18





              Exactly what I have been asking for! Thank you tgikal!

              – Jovan
              Mar 25 at 22:18













              1














              Please refer to an already answered question:
              How to get the datetime from a string containing '2nd' for the date in Python?



              As I can only repeat, solution is to use dateutil parser:



              from dateutil.parser import parse

              your_string = "Friday 1st March 2019"
              date_obj = parse(your_string)


              Behind the scenes, I guess the "1st", and "2nd" parts are extracted somehow (e.g. splitting+regex) and simplified to its day value only.
              According to an input like that, the common datetime library can be used with proper date format string:



              import datetime
              simplified_txt = "Friday 1 March 2019"
              datetime_obj = datetime.datetime.strptime(simplified_txt,"%A %d %B %Y")





              share|improve this answer





























                1














                Please refer to an already answered question:
                How to get the datetime from a string containing '2nd' for the date in Python?



                As I can only repeat, solution is to use dateutil parser:



                from dateutil.parser import parse

                your_string = "Friday 1st March 2019"
                date_obj = parse(your_string)


                Behind the scenes, I guess the "1st", and "2nd" parts are extracted somehow (e.g. splitting+regex) and simplified to its day value only.
                According to an input like that, the common datetime library can be used with proper date format string:



                import datetime
                simplified_txt = "Friday 1 March 2019"
                datetime_obj = datetime.datetime.strptime(simplified_txt,"%A %d %B %Y")





                share|improve this answer



























                  1












                  1








                  1







                  Please refer to an already answered question:
                  How to get the datetime from a string containing '2nd' for the date in Python?



                  As I can only repeat, solution is to use dateutil parser:



                  from dateutil.parser import parse

                  your_string = "Friday 1st March 2019"
                  date_obj = parse(your_string)


                  Behind the scenes, I guess the "1st", and "2nd" parts are extracted somehow (e.g. splitting+regex) and simplified to its day value only.
                  According to an input like that, the common datetime library can be used with proper date format string:



                  import datetime
                  simplified_txt = "Friday 1 March 2019"
                  datetime_obj = datetime.datetime.strptime(simplified_txt,"%A %d %B %Y")





                  share|improve this answer















                  Please refer to an already answered question:
                  How to get the datetime from a string containing '2nd' for the date in Python?



                  As I can only repeat, solution is to use dateutil parser:



                  from dateutil.parser import parse

                  your_string = "Friday 1st March 2019"
                  date_obj = parse(your_string)


                  Behind the scenes, I guess the "1st", and "2nd" parts are extracted somehow (e.g. splitting+regex) and simplified to its day value only.
                  According to an input like that, the common datetime library can be used with proper date format string:



                  import datetime
                  simplified_txt = "Friday 1 March 2019"
                  datetime_obj = datetime.datetime.strptime(simplified_txt,"%A %d %B %Y")






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 25 at 20:48

























                  answered Mar 25 at 20:39









                  Bence KőváriBence Kővári

                  916 bronze badges




                  916 bronze badges





















                      1














                      You are going to face issues with 1st, 2nd.



                      So, try this (without adding any external/third party library):



                      import re
                      from datetime import datetime as dt
                      ds = "Friday 1st March 2019"
                      parts = ds.split(" ")
                      ds = " ".format(
                      parts[0],
                      re.sub('[^0-9]','', parts[1]),
                      parts[2],
                      parts[3]
                      )
                      a = dt.strptime(ds, "%A %d %B %Y")


                      If you want to make it into a function, do this:



                      def convdate(s):
                      parts = s.split(" ")
                      ds = " ".format(
                      parts[0],
                      re.sub('[^0-9]','', parts[1]),
                      parts[2],
                      parts[3]
                      )
                      return dt.strptime(ds, "%A %d %B %Y")





                      share|improve this answer





























                        1














                        You are going to face issues with 1st, 2nd.



                        So, try this (without adding any external/third party library):



                        import re
                        from datetime import datetime as dt
                        ds = "Friday 1st March 2019"
                        parts = ds.split(" ")
                        ds = " ".format(
                        parts[0],
                        re.sub('[^0-9]','', parts[1]),
                        parts[2],
                        parts[3]
                        )
                        a = dt.strptime(ds, "%A %d %B %Y")


                        If you want to make it into a function, do this:



                        def convdate(s):
                        parts = s.split(" ")
                        ds = " ".format(
                        parts[0],
                        re.sub('[^0-9]','', parts[1]),
                        parts[2],
                        parts[3]
                        )
                        return dt.strptime(ds, "%A %d %B %Y")





                        share|improve this answer



























                          1












                          1








                          1







                          You are going to face issues with 1st, 2nd.



                          So, try this (without adding any external/third party library):



                          import re
                          from datetime import datetime as dt
                          ds = "Friday 1st March 2019"
                          parts = ds.split(" ")
                          ds = " ".format(
                          parts[0],
                          re.sub('[^0-9]','', parts[1]),
                          parts[2],
                          parts[3]
                          )
                          a = dt.strptime(ds, "%A %d %B %Y")


                          If you want to make it into a function, do this:



                          def convdate(s):
                          parts = s.split(" ")
                          ds = " ".format(
                          parts[0],
                          re.sub('[^0-9]','', parts[1]),
                          parts[2],
                          parts[3]
                          )
                          return dt.strptime(ds, "%A %d %B %Y")





                          share|improve this answer















                          You are going to face issues with 1st, 2nd.



                          So, try this (without adding any external/third party library):



                          import re
                          from datetime import datetime as dt
                          ds = "Friday 1st March 2019"
                          parts = ds.split(" ")
                          ds = " ".format(
                          parts[0],
                          re.sub('[^0-9]','', parts[1]),
                          parts[2],
                          parts[3]
                          )
                          a = dt.strptime(ds, "%A %d %B %Y")


                          If you want to make it into a function, do this:



                          def convdate(s):
                          parts = s.split(" ")
                          ds = " ".format(
                          parts[0],
                          re.sub('[^0-9]','', parts[1]),
                          parts[2],
                          parts[3]
                          )
                          return dt.strptime(ds, "%A %d %B %Y")






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 25 at 22:26

























                          answered Mar 25 at 20:09









                          Pablo Santa CruzPablo Santa Cruz

                          139k28 gold badges205 silver badges256 bronze badges




                          139k28 gold badges205 silver badges256 bronze badges



























                              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%2f55345550%2fpython-identify-convert-text-to-date%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

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현