I currently have a datetime object with time that looks like 06:00:00 and am trying to convert it to a string that looks like "6:00 am".
I have been trying to use the Python Strftime libraries but am having trouble, could someone point me in the right direction? Thanks :)
You should just be able to use %I:%M %p, as per the following transcript:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2020, 3, 30, 14, 6, 49, 62354)
>>> print(now.strftime("%I:%M %p"))
02:06 PM
See here for a list of the different format codes.
If, as your question implies, you need it without a leading zero on the hour, and in lower-case, you can just use regular expressions for the former and lower() for the latter:
>>> import re
>>> print(re.sub("^0", "", now.strftime("%I:%M %p")).lower())
2:06 pm
This works for me:
>>> import datetime
>>> datetime.datetime.now().strftime( '%H:%M %p' )
'15:09 PM'
Related
I tried to be smart and create a one liner which can extract the datetime of my_string and make a datetime of it. However, it did not work quiet well.
my_string = 'London_XX65TR_20211116_112413.txt'
This is my code:
datetime= datetime.datetime.strptime(my_string .split('_')[2],'%Y%m%d_%H%M%S')
This is my output:
ValueError: time data '20211116' does not match format '%Y%m%d_%H%M%S'
You could use the maxsplit argument in str.split:
>>> from datetime import datetime
>>> region, code, date_time = my_string[:-4].split('_', maxsplit=2)
>>> datetime.strptime(date_time, "%Y%m%d_%H%M%S")
datetime.datetime(2021, 11, 16, 11, 24, 13)
Which means only split at, at most maxsplit occurrences of the _ characters from the left, leave the rest as is.
For this particular case, instead of my_string[:-4], you could use my_string.rstrip('.txt'), it is not advised in general, because it may strip some useful information as well. Whereas, from Python 3.9+ you could use str.removesuffix:
>>> my_string = 'London_XX65TR_20211116_112413.txt'
>>> region, code, date_time = my_string.removesuffix('.txt').split('_', maxsplit=2)
>>> datetime.strptime(date_time, "%Y%m%d_%H%M%S")
datetime.datetime(2021, 11, 16, 11, 24, 13)
You could use re.findall here:
from datetime import datetime
my_string = 'London_XX65TR_20211116_112413.txt'
ts = re.findall(r'_(\d{8}_\d{6})\.', my_string)[0]
dt = datetime.strptime(ts, '%Y%m%d_%H%M%S')
print(dt) # 2021-11-16 11:24:13
This approach uses a regex to extract the timestamp from the input string. The rest of your logic was already correct.
The Method you are following is correct. It's just you are not considering the HH:MIN:Sec part and need to append that before formatting,
my_string = 'London_XX65TR_20211116_112413.txt'
my_date = (my_string .split('_')[2]+my_string .split('_')[3]).replace(".txt","")
datetime= datetime.datetime.strptime(my_date,'%Y%m%d%H%M%S')
print(datetime) # 2021-11-16 11:24:13
Your code does not work because my_string .split('_') gives ['London', 'XX65TR', '20211116', '112413.txt'] so in strptime('20211116', '%Y%m%d_%H%M%S') return an error.
You should either :
limit the format to `'%Y%m%d', loosing the HMS
find another way to get the whole substring matching the format
The first part of the alternative is trivial so lets go for the second one using regex.
import regex as re
datetime = datetime.datetime.strptime(re.search(r'\d{8}_\d{6}', my_string)[0],'%Y%m%d_%H%M%S')
from datetime import datetime
date_time_str = '18/09/19 01:55:19'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
I have to find some date inside an string with a regular expresions in python
astring ='L2A_T21HUB_A023645_20210915T135520'
and i'm trying to get the part before the T with shape xxxxxxxx where every x is a number.
desiredOutput = '20210915'
I'm new in regex so I have no idea how to solve this
If the astring's format is consistent, meaning it will always have the same shape with respect to the date, you can split the string by '_' and get the last substring and get the date from there as such:
astring ='L2A_T21HUB_A023645_20210915T135520'
date_split = astring.split("_"). # --> ['L2A', 'T21HUB', 'A023645', '20210915T135520']
desiredOutput = date_split[3][:8] # --> [3] = '20210915T135520' [:8] gets first 8 chars
print(desiredOutput) # --> 20210915
If you wanted an actual datetime object
>>> from datetime import datetime
>>> astring = 'L2A_T21HUB_A023645_20210915T135520'
>>> date_str = astring.split('_')[-1]
>>> datetime.strptime(date_str, '%Y%m%dT%H%M%S')
datetime.datetime(2021, 9, 15, 13, 55, 20)
From that, you can use datetime.strftime to reformat to a new string, or you can use split('T')[0] to get the string you want.
The trouble with Regex is that there can be unexpected patterns that match your expected pattern and throw things off. However, if you know that only the date portion will ever have 8 sequential digits, you can do this:
import re
date_patt = re.compile('\d{8}')
date = date_patt.search(astring).group(0)
You can develop more robust patterns based on your knowledge of the formatting of the incoming strings. For instance, if you know that the date will always follow an underscore, you could use a look-behind assertion:
date_patt = re.compile(r'(?<=\_)\d{8}') # look for '_' before the date, but don't capture
Hope this helps. Regex can be finicky and may take some tweaking, but hope this sets you in the right direction.
from datetime import datetime
now = datetime.now()
sentence = "{:%B %d %Y}".format(now=datetime.now)
If for some reason I have to use an import from keyword how would I format this in three lines like the example above? My exact question is I don't know how to use the format parameters in this case.
You need pass datetime.now(), not datetime.now method itself.
specify now in the format string to use the keyword argument. Or you can omit now if you pass the datetime object as a positional argument.
>>> from datetime import datetime
>>> now = datetime.now()
>>> "{now:%B %d %Y}".format(now=datetime.now()) # keyword argument
'November 12 2016'
>>> "{:%B %d %Y}".format(datetime.now()) # positional argument
'November 12 2016'
>>> "{0:%B %d %Y}".format(datetime.now()) # positional argument + explicit 0 (first arg)
'November 12 2016'
as furas commented above, this works:
>>> from datetime import datetime
>>> sentence = datetime.now().strftime("%B %d %Y")
>>> print sentence
November 12 2016
>>>
At the moment I have a string similar to:
mytime = '143456.45674'
That string is giving time in :
%HH%MM:%SS . something else
I am only interested in HH:MM:SS format so I could do:
mynewTime = mytime[0:2]+":"+mytime[2:4]+":"+mytime[4:6]
'14:34:56'
It is a bit ugly and I was wondering if there was a more elegant/efficient way of doing it. Regex perhaps?
Looks like you're looking for a combination of strptime and strftime:
import datetime
mytime = '143456.45674'
ts = datetime.datetime.strptime(mytime, '%H%M%S.%f')
print ts.strftime('%H:%M:%S')
# 14:34:56
If you want to do it in regex
>>> import re
>>> val=re.sub(r"\..*$", "", "143456.45674")
>>> re.sub(r"(?<=\d)(?=(\d{2})+$)", ":", val )
'14:34:56'
A regex version for fun :)
re.sub(r"(\d{2})(\d{2})(\d{2})\.\d+", r"\1:\2:\3", mytime)
I have some dates which format is d/m/yyyy (e.g. 2/28/1987).
I would like to have it in the ISO format : 1987-02-28
I think we can do it that way, but it seems a little heavy:
str_date = '2/28/1987'
arr_str = re.split('/', str_date)
iso_date = arr_str[2]+'-'+arr_str[0][:2]+'-'+arr_str[1]
Is there another way to do it with Python?
You could use the datetime module:
datetime.datetime.strptime(str_date, '%m/%d/%Y').date().isoformat()
or, as running code:
>>> import datetime
>>> str_date = '2/28/1987'
>>> datetime.datetime.strptime(str_date, '%m/%d/%Y').date().isoformat()
'1987-02-28'
I would use the datetime module to parse it:
>>> from datetime import datetime
>>> date = datetime.strptime('2/28/1987', '%m/%d/%Y')
>>> date.strftime('%Y-%m-%d')
'1987-02-28'
What you have is very nearly how I would write it, the only major improvement I can suggest is using plain old split instead of re.split, since you do not need a regular expression:
arr_str = str_date.split('/')
If you needed to do anything more complicated like that I would recommend time.strftime, but that's significantly more expensive than string bashing.