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;
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
add a comment |
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
add a comment |
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
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
python date formatting
asked Mar 25 at 20:00
JovanJovan
7513 bronze badges
7513 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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'
Exactly what I have been asking for! Thank you tgikal!
– Jovan
Mar 25 at 22:18
add a comment |
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")
add a comment |
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")
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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'
Exactly what I have been asking for! Thank you tgikal!
– Jovan
Mar 25 at 22:18
add a comment |
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'
Exactly what I have been asking for! Thank you tgikal!
– Jovan
Mar 25 at 22:18
add a comment |
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'
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'
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
add a comment |
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
add a comment |
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")
add a comment |
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")
add a comment |
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")
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")
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
add a comment |
add a comment |
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")
add a comment |
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")
add a comment |
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")
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")
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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