【发布时间】:2015-03-27 04:29:22
【问题描述】:
我必须在日期时间中有当前的年份和月份。
我用这个:
datem = datetime.today().strftime("%Y-%m")
datem = datetime.strptime(datem, "%Y-%m")
还有其他方法吗?
【问题讨论】:
-
你的意思是
date.today().strftime("%Y-%m")?
我必须在日期时间中有当前的年份和月份。
我用这个:
datem = datetime.today().strftime("%Y-%m")
datem = datetime.strptime(datem, "%Y-%m")
还有其他方法吗?
【问题讨论】:
date.today().strftime("%Y-%m")?
试试这个解决方案:
from datetime import datetime
currentSecond= datetime.now().second
currentMinute = datetime.now().minute
currentHour = datetime.now().hour
currentDay = datetime.now().day
currentMonth = datetime.now().month
currentYear = datetime.now().year
【讨论】:
.now() 相比,.today() 是否有优势? now() 函数更短,更常用......但是当所有人都想知道what year it is?
from datetime 有什么作用,为什么需要它?
date 子模块 (from datetime import date) 只处理日期,time 子模块处理时间,以及不幸命名的子模块datetime 子模块两者兼而有之。里面还有timedelta 和tzinfo。可以只使用import datetime 来获取包含所有子模块的整个包,但是方法调用看起来像datetime.datetime.now() 或datetime.date.today()。通常,出于多种原因,只导入您需要的组件会更好。
now() 值保存在变量中,然后从该变量值中提取部分。这将产生一致的结果。请参阅@lllogiq 的答案。
用途:
from datetime import datetime
today = datetime.today()
datem = datetime(today.year, today.month, 1)
我假设你想要一个月的第一天。
【讨论】:
today 变量已经有了当前的年份和月份。
from datetime import datetime 而不是简单的import datetime
用途:
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() and strptime() Behavior
参考:strftime() and strptime() Behavior
以上内容对于任何日期解析都很有用,不仅是现在或今天。它可用于任何日期解析。
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')
等等……
【讨论】:
迟到的答案,但您也可以使用:
import time
ym = time.strftime("%Y-%m")
【讨论】:
>>> from datetime import date
>>> date.today().month
2
>>> date.today().year
2020
>>> date.today().day
13
【讨论】:
您始终可以使用子字符串方法:
import datetime;
today = str(datetime.date.today());
curr_year = int(today[:4]);
curr_month = int(today[5:7]);
这将为您提供整数格式的当前月份和年份。如果您希望它们成为字符串,您只需在为变量 curr_year 和 curr_month 赋值时删除“ int ”优先级。
【讨论】:
datetime.datetime.now().month 更好。
您可以使用date.replace 将the accepted answer 写成单行:
datem = datetime.today().replace(day=1)
【讨论】:
问题要求具体使用datetime。
这是一种只使用datetime的方式:
from datetime import datetime
year = datetime.now().year
month = datetime.now().month
【讨论】:
使用正则表达式从字符串中提取日期和时间,然后使用 strptime 将字符串日期转换为日期时间索引
import re
s = "Audio was recorded at 21:50:00 02/07/2019 (UTC) by device 243B1F05 at gain setting 2 while battery state was 3.6V."
t = re.search(' (\d{2}:\d{2}:\d{2} \d{2}\/\d{2}\/\d{4}) ', s).group(1)
date=datetime.datetime.strptime(t,'%H:%M:%S %m/%d/%Y')
print(type(date))
【讨论】: