Python中的日期时间当前年份和月份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28189442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datetime current year and month in Python
提问by user3778327
I must have the current year and month in datetime.
我必须在日期时间中有当前的年份和月份。
I use this:
我用这个:
datem = datetime.today().strftime("%Y-%m")
datem = datetime.strptime(datem, "%Y-%m")
Is there maybe another way?
也许有另一种方式?
采纳答案by llogiq
Use:
用:
from datetime import datetime
today = datetime.today()
datem = datetime(today.year, today.month, 1)
I assume you want the first of the month.
我假设您想要本月的第一天。
回答by Gaurav Dave
Try this solution:
试试这个解决方案:
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
回答by Jaymeen_JK
You can always use a sub-string method:
您始终可以使用子字符串方法:
import datetime;
today = str(datetime.date.today());
curr_year = int(today[:4]);
curr_month = int(today[5:7]);
This will get you the current month and year in integer format. If you want them to be strings you simply have to remove the " int " precedence while assigning values to the variables curr_year
and curr_month
.
这将为您提供整数格式的当前月份和年份。如果您希望它们是字符串,您只需在为变量curr_year
和curr_month
.
回答by Anup Yadav
Use:
用:
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).
Reference: 8.1.7. strftime() and strptime() Behavior
参考:8.1.7。strftime() 和 strptime() 行为
Reference: strftime() and strptime() Behavior
The above things are useful for any date parsing, not only now or today. It can be useful for any date parsing.
以上内容对任何日期解析都很有用,不仅是现在或今天。它可用于任何日期解析。
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')
And so on...
等等...
回答by Mad Physicist
You can write the accepted answeras a one-liner using date.replace
:
您可以使用以下方法将接受的答案写成一行date.replace
:
datem = datetime.today().replace(day=1)
回答by sanfirat
>>> from datetime import date
>>> date.today().month
2
>>> date.today().year
2020
>>> date.today().day
13
回答by CONvid19
Late answer, but you can also use:
迟到的答案,但您也可以使用:
import time
ym = time.strftime("%Y-%m")