Tôi phải có năm và tháng hiện tại trong DateTime.
Tôi sử dụng cái này:
datem = datetime.today[].strftime["%Y-%m"]
datem = datetime.strptime[datem, "%Y-%m"]
Có thể có cách khác không?
Hỏi ngày 28 tháng 1 năm 2015 lúc 10:06Jan 28, 2015 at 10:06
1
Hãy thử giải pháp này:
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
Đã trả lời ngày 28 tháng 1 năm 2015 lúc 10:11Jan 28, 2015 at 10:11
Gaurav Davegaurav DaveGaurav Dave
6.1689 Huy hiệu vàng23 Huy hiệu bạc39 Huy hiệu Đồng9 gold badges23 silver badges39 bronze badges
5
Use:
from datetime import datetime
today = datetime.today[]
datem = datetime[today.year, today.month, 1]
Tôi cho rằng bạn muốn cái đầu tiên của tháng.
Nic Scozzaro
5,8412 Huy hiệu vàng37 Huy hiệu bạc45 Huy hiệu Đồng2 gold badges37 silver badges45 bronze badges
Đã trả lời ngày 28 tháng 1 năm 2015 lúc 10:11Jan 28, 2015 at 10:11
Gaurav Davegaurav Davellogiq
6.1689 Huy hiệu vàng23 Huy hiệu bạc39 Huy hiệu Đồng6 gold badges42 silver badges69 bronze badges
3
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].
Tôi cho rằng bạn muốn cái đầu tiên của tháng.
Nic Scozzaro
5,8412 Huy hiệu vàng37 Huy hiệu bạc45 Huy hiệu Đồng
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']
LLOGIQLOGIQ
13.3k6 Huy hiệu vàng42 Huy hiệu bạc69 Huy hiệu đồngFeb 23, 2018 at 11:41
Tham khảo: 8.1.7. hành vi strftime [] và strptime []Anup Yadav
Tham khảo: hành vi strftime [] và strptime []2 gold badges20 silver badges30 bronze badges
1
>>> from datetime import date
>>> date.today[].month
2
>>> date.today[].year
2020
>>> date.today[].day
13
Những điều trên rất hữu ích cho bất kỳ phân tích cú pháp ngày nào, không chỉ bây giờ hoặc ngày nay. Nó có thể hữu ích cho bất kỳ phân tích cú pháp ngày.Feb 13, 2020 at 8:09
Và như thế...sanfirat
Đã trả lời ngày 23 tháng 2 năm 2018 lúc 11:412 silver badges6 bronze badges
Anup Yadavanup Yadav
2.7352 Huy hiệu vàng20 Huy hiệu bạc 30 Huy hiệu Đồng
from datetime import datetime
year = datetime.now[].year
month = datetime.now[].month
Đã trả lời ngày 13 tháng 2 năm 2020 lúc 8:09
Sanfiratsanfirat1 gold badge25 silver badges44 bronze badges
1802 Huy hiệu bạc6 Huy hiệu đồngDec 27, 2020 at 22:17
Câu hỏi yêu cầu sử dụng
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
0 cụ thể.import time
ym = time.strftime["%Y-%m"]
Đây là một cách chỉ sử dụng ____1010:Mar 25, 2020 at 23:07
YuriPedro Lobito
3.9901 Huy hiệu vàng25 Huy hiệu bạc44 Huy hiệu đồng30 gold badges239 silver badges259 bronze badges
1
Đã trả lời ngày 27 tháng 12 năm 2020 lúc 22:17
import datetime;
today = str[datetime.date.today[]];
curr_year = int[today[:4]];
curr_month = int[today[5:7]];
Câu trả lời muộn, nhưng bạn cũng có thể sử dụng:
Đã trả lời ngày 25 tháng 3 năm 2020 lúc 23:07Jun 19, 2017 at 11:03
Jaymeen_JKJaymeen_JKJaymeen_JK
Pedro lobitopedro lobito4 silver badges5 bronze badges
1
89.5K30 Huy hiệu vàng239 Huy hiệu bạc259 Huy hiệu Đồng
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]]
Bạn luôn có thể sử dụng phương pháp chuỗi phụ:Apr 19, 2021 at 14:10
Điều này sẽ giúp bạn có một tháng và năm hiện tại ở định dạng số nguyên. Nếu bạn muốn chúng là chuỗi, bạn chỉ cần xóa ưu tiên "int" trong khi gán các giá trị cho các biến
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
2 và 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
3.Golden
LionĐã trả lời ngày 19 tháng 6 năm 2017 lúc 11:032 gold badges25 silver badges31 bronze badges