Python中的日期时间strptime [英] Datetime strptime in python

查看:63
本文介绍了Python中的日期时间strptime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码:

import datetime
d = datetime.datetime.strptime("01/27/2012", "%m/%d/%Y")
print(d)

,输出为:

2012-01-27 00:00:00

我正在使用Linux Mint:

I'am using Linux Mint :

test@testsrv ~/pythonvault $ date
Fri Jun 16 21:40:57 EEST 2017

所以,问题是,为什么python代码的输出返回的日期为%Y /%m /%d(2012-01-27) %m /%d /%Y 格式?

So,the question is why the output of python code returns a date in "%Y/%m/%d" ( 2012-01-27 ) instead of "%m/%d/%Y" format ?

请注意,我使用的是python 2.7

Please note that I'am using python 2.7

任何帮助将不胜感激。

推荐答案

您需要做出确保您相应地提供了输入

You need to make sure you provide input accordingly

datetime.strptime(date_string,date_string_format).strftime(convert_to_date_string_format)

要以指定格式打印日期,您需要输入视频格式如下。

To print the date in specified format you need to provide format as below.

import datetime
d =datetime.datetime.strptime("01/27/2012","%m/%d/%Y").strftime('%m/%d/%Y')
print d

输出:

01/27/2012

>> Demo<<

>>Demo<<

这篇关于Python中的日期时间strptime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆