日期时间当前年和月python

use*_*327 50 python datetime

我必须在日期时间有当前年份和月份.我用这个:

datem = datetime.today().strftime("%Y-%m")
datem = datetime.strptime(datem, "%Y-%m")
Run Code Online (Sandbox Code Playgroud)

也许有另一种方式?

Gau*_*ave 84

尝试此解决方案:

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
Run Code Online (Sandbox Code Playgroud)

  • 如果时间几乎正好是午夜,这个答案就会遇到潜在的问题。如果一天在语句之间的几毫秒内滚动,一个变量可以包含一天的值,另一个变量可以包含第二天的值。将“now()”值保存在变量中,然后从该变量值中提取部分。这将产生一致的结果。请参阅@llogiq 的回答。 (5认同)
  • @Cadoiz - datetime 包有几个子模块 - “date”子模块(“from datetime import date”)仅处理日期,“time”子模块处理时间,以及不幸命名的“datetime”子模块两者都做。那里还有“timedelta”和“tzinfo”。人们可以通过“import datetime”来获取包含所有子模块的整个包,但是方法调用将类似于“datetime.datetime.now()”或“datetime.date.today()”。通常,出于多种原因,最好只导入您需要的组件。 (3认同)
  • 有效,谢谢!比其他答案中使用的“.now()”而不是“.today()”有优势吗?`now()` 函数更短,更常用......但是当人们想知道的是[现在是哪一年?](https://giphy.com /gifs/meme-游泳-北极熊-O1VL59MhotKlW/全屏) (2认同)
  • @hashlash 谢谢!为了节省其他人的点击:不,不是真的 (2认同)

llo*_*giq 58

怎么样

today = datetime.today()
datem = datetime(today.year, today.month, 1)
Run Code Online (Sandbox Code Playgroud)

我假设你想要这个月的第一天.

  • 对于此解决方案,请务必使用`from datetime import datetime`而不是简单的`import datetime` (14认同)
  • @user3778327:`today` 变量已经有当前的年份和月份。 (2认同)

Anu*_*dav 51

我来不及但是有人会发现这对将来很有用.

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).
Run Code Online (Sandbox Code Playgroud)

参考:https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

参考:https://docs.python.org/3/library/datetime.html#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')
Run Code Online (Sandbox Code Playgroud)

等等....

希望有人觉得这很有用.


小智 9

>>> from datetime import date
>>> date.today().month
2
>>> date.today().year
2020
>>> date.today().day
13
Run Code Online (Sandbox Code Playgroud)


小智 8

您始终可以使用子字符串方法:

import datetime;

today = str(datetime.date.today());
curr_year = int(today[:4]);
curr_month = int(today[5:7]);
Run Code Online (Sandbox Code Playgroud)

这将为您提供整数格式的当前月份和年份。如果您希望它们是字符串,您只需在为变量curr_yearcurr_month.

  • 这不好。为什么需要转换它然后进行字符串操作?```datetime.datetime.now().month``` 更好。 (6认同)

Ped*_*ito 8

迟到的答案,但您也可以使用:

import time
ym = time.strftime("%Y-%m")
Run Code Online (Sandbox Code Playgroud)

  • 晚了,但并没有错,甚至比使用 datetime 还清晰。谢谢 :)。 (3认同)

Mad*_*ist 5

您可以使用以下命令将接受的答案写成单行date.replace

datem = datetime.today().replace(day=1)
Run Code Online (Sandbox Code Playgroud)


Mat*_*ulo 5

该问题要求专门使用日期时间。

这是一种仅使用日期时间的方式:

from datetime import datetime
year = datetime.now().year
month = datetime.now().month
Run Code Online (Sandbox Code Playgroud)