I saved a datetime.datetime.now() as a string.
Now I have a string value, i.e.
2010-10-08 14:26:01.220000
How can I convert this string to
Oct 8th 2010
?
Thanks
from datetime import datetime
datetime.strptime('2010-10-08 14:26:01.220000'[:-7],
'%Y-%m-%d %H:%M:%S').strftime('%b %d %Y')
You don't need to create an intermediate string.
You can go directly from a datetime to a string with strftime():
>>> datetime.now().strftime('%b %d %Y')
'Oct 08 2010'
There's no one-liner way, because of your apparent requirement of the grammatical ordinal.
It appears you're using a 2.6 release of Python, or perhaps later. In such a case,
datetime.datetime.strptime("2010-10-08 14:26:01.220000", "%Y-%m-%d %H:%M:%S.%f").strftime("%b %d %Y")
comes close, yielding
Oct 08 2010
To insist on '8th' rather than '08' involves calculating the %b and %Y parts as above, and writing a decimal-to-ordinal function to intercalate between them.
Related
I have found a question at this link that almost answers what I need but not quite. What I need to know, how using this method could I convert a string of the format u'Saturday, Feb 27 2016' into a Python date variable in the format 27/02/2016?
Thanks
You have to first remove the weekday name (it's not much use anyway) and parse the rest:
datetime.datetime.strptime('Saturday, Feb 27 2016'.split(', ', 1)[1], '%b %d %Y').date()
Alternatively, use dateutil:
dateutil.parser.parse('Saturday, Feb 27 2016').date()
EDIT
My mistake, you don't need to remove the Weekday (I'd missed it in the list of options):
datetime.datetime.strptime('Saturday, Feb 27 2016', '%A, %b %d %Y').date()
You don't have to remove anything, you can parse it as is and use strftime to get the format you want:
from datetime import datetime
s = u'Saturday, Feb 27 2016'
dt = datetime.strptime(s,"%A, %b %d %Y")
print(dt)
print(dt.strftime("%d/%m/%Y"))
2016-02-27 00:00:00
27/02/2016
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%d Day of the month as a decimal number [01,31].
%Y Year with century as a decimal number.
The full listing of directives are here
I have this date from Twitter:
created_at = "Wed Aug 29 17:12:58 +0000 2012"
I want to convert it to a time using something like:
time.mktime(created_at)
But I get this error:
TypeError: argument must be 9-item sequence, not str
What am I doing wrong?
You need to convert the incoming string to a Python time tuple using strptime before you can do anything with it.
This code will take the input string, convert it to a tuple and then converts that to a Unix-epoch time float using time.mktime:
import time
created_at = "Wed Aug 29 17:12:58 +0000 2012"
print time.mktime(time.strptime(created_at,"%a %b %d %H:%M:%S +0000 %Y"))
I don't if it too late, use arrow package instead could fewer imports and a lot less code.
pip install arrow
Then:
>>> arrow.Arrow.strptime("Wed Aug 29 17:12:58 +0000 2012", "%a %b %d %H:%M:%S %z %Y")
<Arrow [2012-08-29T17:00:58+00:00]>
>>> arrow.Arrow.strptime("Wed Aug 29 17:12:58 +0000 2012", "%a %b %d %H:%M:%S %z %Y").timestamp
1346259658
Read the documentation of time.mktime
It requires struct_time, or you can alternatively represent it using a 9-tuple.
The required entries are:
Year
Month
Date
Hour
Minute
Second
Day in week
Day in year
Daylight Savings Time
This is not the function you need, however. It seems that you want to use strptime instead.
According to the documentation:
Parse a string representing a time according to a format.
The return value is a struct_time as returned by gmtime() or localtime().
>>> import time
>>> time.strptime("30 Nov 00", "%d %b %y")
time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0,
tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
So, you can do:
time.strptime(created_at)
Why does
o.create_order.strftime("%d %B %Y")
returns nothing when
time.strftime("%d %B %Y")
returns the date "10 february 2013"???
o.create_order is a timestamp according to postgresql.
It contains "30/11/2012 09:38:34" as seen on the openErp sale order - Other information tab.
It is stored as "2012-11-30 08:38:34.272" when querying the database.
So I would expect to see "30 November 2012" but get nothing instead.
Am I misinterpreting the syntax?
I tested this from python 3.3:
>>> d1=datetime.datetime.today()
>>> print(d1.strftime("%d %B %Y"))
10 february 2013
How do I get it to work in OpenOffice Writer?
And by the way how do I get "February" instead of "february"?
Because o.create_order returns a string and not a datetime object, even if, internally, the database column is a timestamp. The OpenERP ORM returns a string in ISO 8601 format.
You need to use the formatLang method which is available in RML reports or create a datetime object using the datetime python module.
Try this:
datetime.strftime('%d %B %Y', o.create_order')
It is because o.create_order returns a string. So first you have to convert your string date into datetime format and then again you can convert it into any format you want as a string.
Try this:
#Converts string into datetime format.
dt1 = datetime.strptime(o.create_order,"%Y-%m-%d %H:%M:%S")
#Converts datetime into your given formate and returns string.
dt2 = datetime.strftime(dt,"%d %B %Y")
Hope this will solve your problem.
How can I get Python to output
'Mon Jun 04'
'Tue Jun 05'
etc, for a week of given time?
ex
today = datetime.datetime.today()
### do some magic
days = ['Tue Jun 05',...]
What do I do with 'today' to generate the results? I'm not even sure if I'm the right module, calender seems to share similar features.
days = [today.strftime("%a %b %y"), ...]
We use strftime to take a datetime object and format it to a string
http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
Look at the strftime method of datetime objects.
I have these date strings:
Fri Oct 7 16:00:09 CEST 2011
I want to convert them to UTC. I have tried with this implementation:
def LocalToUtc(localtime):
return datetime.strptime(localtime, "%a %m %d %H:%M:%S %Z %Y").isoformat() + 'Z'
But I get a ValueError:
ValueError: time data 'Fri Oct 7 16:00:09 CEST 2011' does not match format '%a %m %d %H:%M:%S %Z %Y'
Any ideas?
Use the parsedatetime library.
There are two problems here:
You're using "%m" instead of "%b"
The standard lib can't parse "CEST", it understands only very few time zone names.
See also here: What possible values does datetime.strptime() accept for %Z?