python如何获取系统时间年月日_Python中的日期时间当前年和月

用:

from datetime import datetime

current_month = datetime.now().strftime('%m') // 02 //This is 0 padded

current_month_text = datetime.now().strftime('%h') // Feb

current_month_text = datetime.now().strftime('%B') // February

current_day = datetime.now().strftime('%d') // 23 //This is also padded

current_day_text = datetime.now().strftime('%a') // Fri

current_day_full_text = datetime.now().strftime('%A') // Friday

current_weekday_day_of_today = datetime.now().strftime('%w') //5 Where 0 is Sunday and 6 is Saturday.

current_year_full = datetime.now().strftime('%Y') // 2018

current_year_short = datetime.now().strftime('%y') // 18 without century

current_second= datetime.now().strftime('%S') //53

current_minute = datetime.now().strftime('%M') //38

current_hour = datetime.now().strftime('%H') //16 like 4pm

current_hour = datetime.now().strftime('%I') // 04 pm

current_hour_am_pm = datetime.now().strftime('%p') // 4 pm

current_microseconds = datetime.now().strftime('%f') // 623596 Rarely we need.

current_timzone = datetime.now().strftime('%Z') // UTC, EST, CST etc. (empty string if the object is naive).

参考:8.1.7。 strftime()和strptime()行为

参考:strftime()和strptime()行为

以上内容对于任何日期解析都是有用的,不仅是现在还是今天。 它对于任何日期解析都很有用。

e.g.

my_date = "23-02-2018 00:00:00"

datetime.strptime(str(my_date),'%d-%m-%Y %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S+00:00')

datetime.strptime(str(my_date),'%d-%m-%Y %H:%M:%S').strftime('%m')

等等...

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值