I know this has been asked in some form or another, but I'm wondering how to convert a date such as:
Saturday, May 18th, 2019
to something like 2019-05-18 so that I can compare it to other dates. I understand dateutil can do so but unfortunately I'm using Pycharm and it won't let me install that package without upgrading Python from 2.7 to 3. Is there a way to do this simply in Python?
You can use datetime with re module as well:
>>> from datetime import datetime
>>> import re
>>> s = 'Saturday, May 18th, 2019'
>>> datetime.strptime(re.sub('(\d+)(st|nd|rd|th)', '\g<1>', s), '%A, %B %d, %Y')
datetime.datetime(2019, 5, 18, 0, 0)
>>>
And print it nicer:
>>> print(datetime.strptime(re.sub('(\d+)(st|nd|rd|th)', '\g<1>', s), '%A, %B %d, %Y'))
2019-05-18 00:00:00
>>>
The regex solution is preferable in my opinion, but this is a solution without using re, for people not familiar with regexps:
from datetime import datetime
s = 'Saturday, May 18th, 2019'
sl = s.split()
sl[2] = sl[2][:-3] # remove 'th,'
datetime.strptime(' '.join(sl), '%A, %B %d %Y')
Related
I have a date string with the format 'Mon Feb 15 2010'. I want to change the format to '15/02/2010'. How can I do this?
datetime module could help you with that:
datetime.datetime.strptime(date_string, format1).strftime(format2)
For the specific example you could do
>>> import datetime
>>> datetime.datetime.strptime('Mon Feb 15 2010', '%a %b %d %Y').strftime('%d/%m/%Y')
'15/02/2010'
>>>
You can install the dateutil library. Its parse function can figure out what format a string is in without having to specify the format like you do with datetime.strptime.
from dateutil.parser import parse
dt = parse('Mon Feb 15 2010')
print(dt)
# datetime.datetime(2010, 2, 15, 0, 0)
print(dt.strftime('%d/%m/%Y'))
# 15/02/2010
convert string to datetime object
from datetime import datetime
s = "2016-03-26T09:25:55.000Z"
f = "%Y-%m-%dT%H:%M:%S.%fZ"
out = datetime.strptime(s, f)
print(out)
output:
2016-03-26 09:25:55
>>> from_date="Mon Feb 15 2010"
>>> import time
>>> conv=time.strptime(from_date,"%a %b %d %Y")
>>> time.strftime("%d/%m/%Y",conv)
'15/02/2010'
As this question comes often, here is the simple explanation.
datetime or time module has two important functions.
strftime - creates a string representation of date or time from a datetime or time object.
strptime - creates a datetime or time object from a string.
In both cases, we need a formating string. It is the representation that tells how the date or time is formatted in your string.
Now lets assume we have a date object.
>>> from datetime import datetime
>>> d = datetime(2010, 2, 15)
>>> d
datetime.datetime(2010, 2, 15, 0, 0)
If we want to create a string from this date in the format 'Mon Feb 15 2010'
>>> s = d.strftime('%a %b %d %y')
>>> print s
Mon Feb 15 10
Lets assume we want to convert this s again to a datetime object.
>>> new_date = datetime.strptime(s, '%a %b %d %y')
>>> print new_date
2010-02-15 00:00:00
Refer This document all formatting directives regarding datetime.
#codeling and #user1767754 : The following two lines will work. I saw no one posted the complete solution for the example problem that was asked. Hopefully this is enough explanation.
import datetime
x = datetime.datetime.strptime("Mon Feb 15 2010", "%a %b %d %Y").strftime("%d/%m/%Y")
print(x)
Output:
15/02/2010
You may achieve this using pandas as well:
import pandas as pd
pd.to_datetime('Mon Feb 15 2010', format='%a %b %d %Y').strftime('%d/%m/%Y')
Output:
'15/02/2010'
You may apply pandas approach for different datatypes as:
import pandas as pd
import numpy as np
def reformat_date(date_string, old_format, new_format):
return pd.to_datetime(date_string, format=old_format, errors='ignore').strftime(new_format)
date_string = 'Mon Feb 15 2010'
date_list = ['Mon Feb 15 2010', 'Wed Feb 17 2010']
date_array = np.array(date_list)
date_series = pd.Series(date_list)
old_format = '%a %b %d %Y'
new_format = '%d/%m/%Y'
print(reformat_date(date_string, old_format, new_format))
print(reformat_date(date_list, old_format, new_format).values)
print(reformat_date(date_array, old_format, new_format).values)
print(date_series.apply(lambda x: reformat_date(x, old_format, new_format)).values)
Output:
15/02/2010
['15/02/2010' '17/02/2010']
['15/02/2010' '17/02/2010']
['15/02/2010' '17/02/2010']
Just for the sake of completion: when parsing a date using strptime() and the date contains the name of a day, month, etc, be aware that you have to account for the locale.
It's mentioned as a footnote in the docs as well.
As an example:
import locale
print(locale.getlocale())
>> ('nl_BE', 'ISO8859-1')
from datetime import datetime
datetime.strptime('6-Mar-2016', '%d-%b-%Y').strftime('%Y-%m-%d')
>> ValueError: time data '6-Mar-2016' does not match format '%d-%b-%Y'
locale.setlocale(locale.LC_ALL, 'en_US')
datetime.strptime('6-Mar-2016', '%d-%b-%Y').strftime('%Y-%m-%d')
>> '2016-03-06'
use datetime library
http://docs.python.org/library/datetime.html look up 9.1.7.
especiall strptime() strftime() Behavior¶
examples
http://pleac.sourceforge.net/pleac_python/datesandtimes.html
If you dont want to define the input date format then, Install dateparser (pip install dateparser) and,
from dateparser import parse
parse("Mon Feb 15 2010").strftime("%d/%m/%Y")
I have a date string with the format 'Mon Feb 15 2010'. I want to change the format to '15/02/2010'. How can I do this?
datetime module could help you with that:
datetime.datetime.strptime(date_string, format1).strftime(format2)
For the specific example you could do
>>> import datetime
>>> datetime.datetime.strptime('Mon Feb 15 2010', '%a %b %d %Y').strftime('%d/%m/%Y')
'15/02/2010'
>>>
You can install the dateutil library. Its parse function can figure out what format a string is in without having to specify the format like you do with datetime.strptime.
from dateutil.parser import parse
dt = parse('Mon Feb 15 2010')
print(dt)
# datetime.datetime(2010, 2, 15, 0, 0)
print(dt.strftime('%d/%m/%Y'))
# 15/02/2010
convert string to datetime object
from datetime import datetime
s = "2016-03-26T09:25:55.000Z"
f = "%Y-%m-%dT%H:%M:%S.%fZ"
out = datetime.strptime(s, f)
print(out)
output:
2016-03-26 09:25:55
>>> from_date="Mon Feb 15 2010"
>>> import time
>>> conv=time.strptime(from_date,"%a %b %d %Y")
>>> time.strftime("%d/%m/%Y",conv)
'15/02/2010'
As this question comes often, here is the simple explanation.
datetime or time module has two important functions.
strftime - creates a string representation of date or time from a datetime or time object.
strptime - creates a datetime or time object from a string.
In both cases, we need a formating string. It is the representation that tells how the date or time is formatted in your string.
Now lets assume we have a date object.
>>> from datetime import datetime
>>> d = datetime(2010, 2, 15)
>>> d
datetime.datetime(2010, 2, 15, 0, 0)
If we want to create a string from this date in the format 'Mon Feb 15 2010'
>>> s = d.strftime('%a %b %d %y')
>>> print s
Mon Feb 15 10
Lets assume we want to convert this s again to a datetime object.
>>> new_date = datetime.strptime(s, '%a %b %d %y')
>>> print new_date
2010-02-15 00:00:00
Refer This document all formatting directives regarding datetime.
#codeling and #user1767754 : The following two lines will work. I saw no one posted the complete solution for the example problem that was asked. Hopefully this is enough explanation.
import datetime
x = datetime.datetime.strptime("Mon Feb 15 2010", "%a %b %d %Y").strftime("%d/%m/%Y")
print(x)
Output:
15/02/2010
You may achieve this using pandas as well:
import pandas as pd
pd.to_datetime('Mon Feb 15 2010', format='%a %b %d %Y').strftime('%d/%m/%Y')
Output:
'15/02/2010'
You may apply pandas approach for different datatypes as:
import pandas as pd
import numpy as np
def reformat_date(date_string, old_format, new_format):
return pd.to_datetime(date_string, format=old_format, errors='ignore').strftime(new_format)
date_string = 'Mon Feb 15 2010'
date_list = ['Mon Feb 15 2010', 'Wed Feb 17 2010']
date_array = np.array(date_list)
date_series = pd.Series(date_list)
old_format = '%a %b %d %Y'
new_format = '%d/%m/%Y'
print(reformat_date(date_string, old_format, new_format))
print(reformat_date(date_list, old_format, new_format).values)
print(reformat_date(date_array, old_format, new_format).values)
print(date_series.apply(lambda x: reformat_date(x, old_format, new_format)).values)
Output:
15/02/2010
['15/02/2010' '17/02/2010']
['15/02/2010' '17/02/2010']
['15/02/2010' '17/02/2010']
Just for the sake of completion: when parsing a date using strptime() and the date contains the name of a day, month, etc, be aware that you have to account for the locale.
It's mentioned as a footnote in the docs as well.
As an example:
import locale
print(locale.getlocale())
>> ('nl_BE', 'ISO8859-1')
from datetime import datetime
datetime.strptime('6-Mar-2016', '%d-%b-%Y').strftime('%Y-%m-%d')
>> ValueError: time data '6-Mar-2016' does not match format '%d-%b-%Y'
locale.setlocale(locale.LC_ALL, 'en_US')
datetime.strptime('6-Mar-2016', '%d-%b-%Y').strftime('%Y-%m-%d')
>> '2016-03-06'
use datetime library
http://docs.python.org/library/datetime.html look up 9.1.7.
especiall strptime() strftime() Behavior¶
examples
http://pleac.sourceforge.net/pleac_python/datesandtimes.html
If you dont want to define the input date format then, Install dateparser (pip install dateparser) and,
from dateparser import parse
parse("Mon Feb 15 2010").strftime("%d/%m/%Y")
I have a date string with the format 'Mon Feb 15 2010'. I want to change the format to '15/02/2010'. How can I do this?
datetime module could help you with that:
datetime.datetime.strptime(date_string, format1).strftime(format2)
For the specific example you could do
>>> import datetime
>>> datetime.datetime.strptime('Mon Feb 15 2010', '%a %b %d %Y').strftime('%d/%m/%Y')
'15/02/2010'
>>>
You can install the dateutil library. Its parse function can figure out what format a string is in without having to specify the format like you do with datetime.strptime.
from dateutil.parser import parse
dt = parse('Mon Feb 15 2010')
print(dt)
# datetime.datetime(2010, 2, 15, 0, 0)
print(dt.strftime('%d/%m/%Y'))
# 15/02/2010
convert string to datetime object
from datetime import datetime
s = "2016-03-26T09:25:55.000Z"
f = "%Y-%m-%dT%H:%M:%S.%fZ"
out = datetime.strptime(s, f)
print(out)
output:
2016-03-26 09:25:55
>>> from_date="Mon Feb 15 2010"
>>> import time
>>> conv=time.strptime(from_date,"%a %b %d %Y")
>>> time.strftime("%d/%m/%Y",conv)
'15/02/2010'
As this question comes often, here is the simple explanation.
datetime or time module has two important functions.
strftime - creates a string representation of date or time from a datetime or time object.
strptime - creates a datetime or time object from a string.
In both cases, we need a formating string. It is the representation that tells how the date or time is formatted in your string.
Now lets assume we have a date object.
>>> from datetime import datetime
>>> d = datetime(2010, 2, 15)
>>> d
datetime.datetime(2010, 2, 15, 0, 0)
If we want to create a string from this date in the format 'Mon Feb 15 2010'
>>> s = d.strftime('%a %b %d %y')
>>> print s
Mon Feb 15 10
Lets assume we want to convert this s again to a datetime object.
>>> new_date = datetime.strptime(s, '%a %b %d %y')
>>> print new_date
2010-02-15 00:00:00
Refer This document all formatting directives regarding datetime.
#codeling and #user1767754 : The following two lines will work. I saw no one posted the complete solution for the example problem that was asked. Hopefully this is enough explanation.
import datetime
x = datetime.datetime.strptime("Mon Feb 15 2010", "%a %b %d %Y").strftime("%d/%m/%Y")
print(x)
Output:
15/02/2010
You may achieve this using pandas as well:
import pandas as pd
pd.to_datetime('Mon Feb 15 2010', format='%a %b %d %Y').strftime('%d/%m/%Y')
Output:
'15/02/2010'
You may apply pandas approach for different datatypes as:
import pandas as pd
import numpy as np
def reformat_date(date_string, old_format, new_format):
return pd.to_datetime(date_string, format=old_format, errors='ignore').strftime(new_format)
date_string = 'Mon Feb 15 2010'
date_list = ['Mon Feb 15 2010', 'Wed Feb 17 2010']
date_array = np.array(date_list)
date_series = pd.Series(date_list)
old_format = '%a %b %d %Y'
new_format = '%d/%m/%Y'
print(reformat_date(date_string, old_format, new_format))
print(reformat_date(date_list, old_format, new_format).values)
print(reformat_date(date_array, old_format, new_format).values)
print(date_series.apply(lambda x: reformat_date(x, old_format, new_format)).values)
Output:
15/02/2010
['15/02/2010' '17/02/2010']
['15/02/2010' '17/02/2010']
['15/02/2010' '17/02/2010']
Just for the sake of completion: when parsing a date using strptime() and the date contains the name of a day, month, etc, be aware that you have to account for the locale.
It's mentioned as a footnote in the docs as well.
As an example:
import locale
print(locale.getlocale())
>> ('nl_BE', 'ISO8859-1')
from datetime import datetime
datetime.strptime('6-Mar-2016', '%d-%b-%Y').strftime('%Y-%m-%d')
>> ValueError: time data '6-Mar-2016' does not match format '%d-%b-%Y'
locale.setlocale(locale.LC_ALL, 'en_US')
datetime.strptime('6-Mar-2016', '%d-%b-%Y').strftime('%Y-%m-%d')
>> '2016-03-06'
use datetime library
http://docs.python.org/library/datetime.html look up 9.1.7.
especiall strptime() strftime() Behavior¶
examples
http://pleac.sourceforge.net/pleac_python/datesandtimes.html
If you dont want to define the input date format then, Install dateparser (pip install dateparser) and,
from dateparser import parse
parse("Mon Feb 15 2010").strftime("%d/%m/%Y")
I'm having a bad time with date parsing and formatting today.
Points for somebody who can parse this date format into a datetime.date or datetime.datetime (I'm not too fussy but I'd prefer .date):
5th November 2010
Using dateutil:
In [2]: import dateutil.parser as dparser
In [3]: date = dparser.parse('5th November 2010')
In [4]: date
Out[4]: datetime.datetime(2010, 11, 5, 0, 0)
Unfortunately, strptime has no format characters for "skip an ordinal suffix" -- so, I'd do the skipping first, with a little RE, and then parse the resulting "clear" string. I.e.:
>>> import re
>>> import datetime
>>> ordn = re.compile(r'(?<=\d)(st|nd|rd|th)\b')
>>> def parse(s):
... cleans = ordn.sub('', s)
... dt = datetime.datetime.strptime(cleans, '%d %B %Y')
... return dt.date()
...
>>> parse('5th November 2010')
datetime.date(2010, 11, 5)
Your preference for date vs datetime is no problem of course, that's what the .date() method of datetime objects is for;-).
Third-party extensions like dateutil can be useful if you need to do a lot of "fuzzy" date parsing (or other fancy date-related stuff;-), by the way.
If the ordinal is constant then:
datetime.strptime(s, '%dth %B %Y')
Else:
date_str = '5th November 2010'
modified_date_str = date_str[0:1] + date_str[3:]
datetime.strptime(modified_date_str, '%d %B %Y')
Or like ~unutbu said use dateutil :)
I have a date string with the format 'Mon Feb 15 2010'. I want to change the format to '15/02/2010'. How can I do this?
datetime module could help you with that:
datetime.datetime.strptime(date_string, format1).strftime(format2)
For the specific example you could do
>>> import datetime
>>> datetime.datetime.strptime('Mon Feb 15 2010', '%a %b %d %Y').strftime('%d/%m/%Y')
'15/02/2010'
>>>
You can install the dateutil library. Its parse function can figure out what format a string is in without having to specify the format like you do with datetime.strptime.
from dateutil.parser import parse
dt = parse('Mon Feb 15 2010')
print(dt)
# datetime.datetime(2010, 2, 15, 0, 0)
print(dt.strftime('%d/%m/%Y'))
# 15/02/2010
convert string to datetime object
from datetime import datetime
s = "2016-03-26T09:25:55.000Z"
f = "%Y-%m-%dT%H:%M:%S.%fZ"
out = datetime.strptime(s, f)
print(out)
output:
2016-03-26 09:25:55
>>> from_date="Mon Feb 15 2010"
>>> import time
>>> conv=time.strptime(from_date,"%a %b %d %Y")
>>> time.strftime("%d/%m/%Y",conv)
'15/02/2010'
As this question comes often, here is the simple explanation.
datetime or time module has two important functions.
strftime - creates a string representation of date or time from a datetime or time object.
strptime - creates a datetime or time object from a string.
In both cases, we need a formating string. It is the representation that tells how the date or time is formatted in your string.
Now lets assume we have a date object.
>>> from datetime import datetime
>>> d = datetime(2010, 2, 15)
>>> d
datetime.datetime(2010, 2, 15, 0, 0)
If we want to create a string from this date in the format 'Mon Feb 15 2010'
>>> s = d.strftime('%a %b %d %y')
>>> print s
Mon Feb 15 10
Lets assume we want to convert this s again to a datetime object.
>>> new_date = datetime.strptime(s, '%a %b %d %y')
>>> print new_date
2010-02-15 00:00:00
Refer This document all formatting directives regarding datetime.
#codeling and #user1767754 : The following two lines will work. I saw no one posted the complete solution for the example problem that was asked. Hopefully this is enough explanation.
import datetime
x = datetime.datetime.strptime("Mon Feb 15 2010", "%a %b %d %Y").strftime("%d/%m/%Y")
print(x)
Output:
15/02/2010
You may achieve this using pandas as well:
import pandas as pd
pd.to_datetime('Mon Feb 15 2010', format='%a %b %d %Y').strftime('%d/%m/%Y')
Output:
'15/02/2010'
You may apply pandas approach for different datatypes as:
import pandas as pd
import numpy as np
def reformat_date(date_string, old_format, new_format):
return pd.to_datetime(date_string, format=old_format, errors='ignore').strftime(new_format)
date_string = 'Mon Feb 15 2010'
date_list = ['Mon Feb 15 2010', 'Wed Feb 17 2010']
date_array = np.array(date_list)
date_series = pd.Series(date_list)
old_format = '%a %b %d %Y'
new_format = '%d/%m/%Y'
print(reformat_date(date_string, old_format, new_format))
print(reformat_date(date_list, old_format, new_format).values)
print(reformat_date(date_array, old_format, new_format).values)
print(date_series.apply(lambda x: reformat_date(x, old_format, new_format)).values)
Output:
15/02/2010
['15/02/2010' '17/02/2010']
['15/02/2010' '17/02/2010']
['15/02/2010' '17/02/2010']
Just for the sake of completion: when parsing a date using strptime() and the date contains the name of a day, month, etc, be aware that you have to account for the locale.
It's mentioned as a footnote in the docs as well.
As an example:
import locale
print(locale.getlocale())
>> ('nl_BE', 'ISO8859-1')
from datetime import datetime
datetime.strptime('6-Mar-2016', '%d-%b-%Y').strftime('%Y-%m-%d')
>> ValueError: time data '6-Mar-2016' does not match format '%d-%b-%Y'
locale.setlocale(locale.LC_ALL, 'en_US')
datetime.strptime('6-Mar-2016', '%d-%b-%Y').strftime('%Y-%m-%d')
>> '2016-03-06'
use datetime library
http://docs.python.org/library/datetime.html look up 9.1.7.
especiall strptime() strftime() Behavior¶
examples
http://pleac.sourceforge.net/pleac_python/datesandtimes.html
If you dont want to define the input date format then, Install dateparser (pip install dateparser) and,
from dateparser import parse
parse("Mon Feb 15 2010").strftime("%d/%m/%Y")