Format Unicode time value - python

I have next time value in unicode (<type 'unicode'>):
2017-08-09T15:02:58+0000.
How to convert it to friendly view (e.g. Day, Month of Year)?

This should do what you ask:
from datetime import datetime
a = '2017-08-09T15:02:58+0000'
datetime.strptime(a[:-5], '%Y-%m-%dT%H:%M:%S').strftime('%d, %b of %Y')
#09, Aug of 2017
strptime method throws error for timezone parameter that doesn't seem to interest you so I removed that part with a[:-5].
For the rest of the string you can just follow guidelines from datetime docs.
Using the same docs you can construct your datetime string using strftime() method like you wanted '%d, %b of %Y' or in plain words [day], [abbreviated month] of [Year]

try this
import datetime
today = datetime.date.today()
print today.strftime('We are the %d, %b %Y')
'We are the 22, Nov 2008'

Related

How can I change this format to be datetime format? (python)

I have a dataset which contains some columns with datetime. My problem is, I found this type datetime format:
Apr'11
Apr-11
Apr 11
How can I automatically change this format to be datetime format?
for you can use datetime module
from datetime import datetime
this is the link if you want any confusion
date_string = "Apr'11"
date = datetime.strptime(date_string, "%b'%d")
%b = Locale’s abbreviated month name. (like Apr, Mar, Jan, etc)
%d = Day of the month as a decimal number [01,31]
date_string_2 = "Apr-11"
date = datetime.strptime(date_string, "%b-%d")
date_string_3 = "Apr 11"
date = datetime.strptime(date_string, "%b %d")
You should write this "%b %d" same as like date_string otherwise it will give you, even if you give an extra space.
go to this link to learn more about this:
https://docs.python.org/3/library/time.html

Converting string to data object with strptime() not working

How to convert string '17 Nov 2021' into data object "20211117"?
I've tried:
_date=datetime.strptime(_dateAsString, '%Y%m%d')
but it does not work :/
Parse to datetime object and then convert back to string with different date format as follows:
import datetime
_date = datetime.datetime.strptime("17 Nov 2021", "%d %b %Y").strftime("%Y%m%d")
# Result: '20211117'
You can review the available strftime() and strptime() format codes if it's not clear what the various codes e.g. %d and %b represent.

get hours from full date date to python3

I work with api in python3 in this api return date like this
'Jun 29, 2018 12:44:14 AM'
but i need just hours, minute ad second like this
12:44:14
are there a fonction that can format this
It looks like the output is a string. So, you can use string slicing:
x = 'Jun 29, 2018 12:44:18 AM'
time = x[-11:-3]
It's best to use negative indexing here because the day may be single-digit or double-digit, so a solution like time = x[13:21] won't work every time.
If you're inclined, you may wish to use strptime() and strftime() to take your string, convert it into a datetime object, and then convert that into a string in HH:MM:SS format. (You may wish to consult the datetime module documentation for this approach).
Use the datetime module. Use .strptime() to convert string to datetime object and then .strftime() to convert to your required string output. Your sample datetime string is represented as '%b %d, %Y %H:%M:%S %p'
Ex:
import datetime
s = 'Jun 29, 2018 12:44:14 AM'
print( datetime.datetime.strptime(s, '%b %d, %Y %H:%M:%S %p').strftime("%H:%M:%S") )
Output:
12:44:14

Convert datetime in python

How do i convert this datetime using python?
2017-10-16T08:27:16+0000
I tried to use strptime but getting ValueError: time data '2017-10-16T08:27:16+0000' does not match format 'The %d %B %Y at. %H:%M'
import datetime
datetime.datetime.strptime("2017-10-16T08:27:16+0000", "The %d %B %Y at. %H:%M")
'''
I want my output to look like this
The 16 october 2017 at. 08:27
'''
First parse the string correctly, then print it in the desired format:
import datetime
date = datetime.datetime.strptime("2017-10-16T08:27:16+0000", "%Y-%m-%dT%H:%M:%S%z")
print(date.strftime("The %d %B %Y at. %H:%M"))
https://blogs.harvard.edu/rprasad/2011/09/21/python-string-to-a-datetime-object/
You have to first strip your date using strptime() and then rebuild it using strftime()
import datetime
time = "2017-10-16T08:27:16+0000"
stripedTime = datetime.datetime.strptime(time, '%Y-%m-%dT%I:%M:%S%z')
rebuildTime = stripedTime.strftime('The %d %B %Y at. %H:%M')
print(rebuildTime)

Convert date string in excel to date object in python

I have a date in excel which is given in: dd mmm yy format i.e.,
29 Jun 18
How do I convert this string into a date object?
I get the error:
time data '13 Jul 18' does not match format '%d %m %Y'
when I try
datetime.strptime(input, '%d %m %Y')
What should the correct date format be?
Since the year in your excell is only two digits (i.e., 18 and not 2018) you need to use %y instead of %Y in your format string:
datetime.strptime(input, '%d %b %y')
For example:
datetime.strptime( '13 Jul 18', '%d %b %y')
Results with:
datetime.datetime(2018, 7, 13, 0, 0)
See this page for more information about date/time string format.
You can use python datetime module or you can use dateutil parser to parse the string date to valid datetime object. I'd go with dateutil parser as I don't have to define string format. Here is an example
from dateutil.parser import parse
dt = parse("Thu Sep 25 10:36:28 BRST 2003")
Remember to install dateutil by pip install python-dateutil
You would have to import datetime and import xlrd
Use xlrd to open the excel workbook as
book = xlrd.open_workbook("Excel.xlsx")
sheet = book.sheet_by_name("Worksheet")
Use this to convert
obj = datetime.datetime(*xlrd.xldate_as_tuple(sheet.cell(row,column).value, book.datemode))
from datetime import datetime
datetime.strptime("29 Jun 18", "%d %b %y").date()
Here you get a datetime.date object, I don't know if that's good enough for you. I recommend you to visit the documentation on the module
You can make use of strptime which follows the pattern:
datetime.strptime(date_string, format)
example:
from datetime import datetime
dt = datetime.strptime('19 Jul 2017', '%d %b %y')
Hope this helps :)
Your format is incorrect: %b is Locale's short month and %y is two digit year
import time
time.strptime('13 Jul 18', '%d %b %y')
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=13, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=194, tm_isdst=-1)

Categories

Resources