2016-11-24 131 views
0

datetime.datetime(2016, 11, 21, 5, 34, 38, 826339, tzinfo=<UTC>)作为Nov. 21, 2016, 11:04 a.m.变化DateTime对象到字符串

的时间转换中的日期时间对象处于UST但我希望它被转换成IST(UST + 05:30)。

我尝试使用的strftime为:

>>> datetime(2016, 11, 21, 5, 34, 38, 826339, tzinfo=<UTC>).isoformat(' ') 
    File "<stdin>", line 1 
    datetime(2016, 11, 21, 5, 34, 38, 826339, tzinfo=<UTC>).isoformat(' ') 
                ^
SyntaxError: invalid syntax 

我能得到一些帮助在这里。

PS:我使用python

编辑:

cr_date = datetime(2016, 11, 21, 5, 34, 38, 826339) #excluding the timezone 

我可以得到想要的部分reults:

cr_date.strftime('%b. %d, %Y %H:%M') 
'Oct. 31, 2013 18:23 

没有得到AM/PM虽然

回答

0

对于am/pm部分,你不能在最后一个字段使用%p吗?我不知道python,我只是假定python正在从unix date命令中获取语法。

0

添加%p为“上午或下午,”其他补充%P为“上午或下午”

0

请找到下面的代码

from datetime import datetime 
cr_date = datetime(2016, 11, 21, 5, 34, 38, 826339) 
cr_date.strftime('%b. %d, %Y %H:%M %P') 
'Nov. 21, 2016 05:34 am'