Hướng dẫn how do you display the week in python? - làm thế nào để bạn hiển thị tuần trong python?

Tôi tóm tắt cuộc thảo luận thành hai bước:

Show
  1. Chuyển đổi định dạng thô thành đối tượng
    dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
    print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
    print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
    print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
    
    8.
  2. Sử dụng chức năng của đối tượng
    dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
    print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
    print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
    print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
    
    8 hoặc đối tượng
    dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
    d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
    year, week, weekday = d.isocalendar() 
    print(year, week, weekday) # (2016,52,7) in the ISO standard
    
    0 để tính toán số tuần.

Ấm lên

from datetime import datetime, date, time
d = date(2005, 7, 14)
t = time(12, 30)
dt = datetime.combine(d, t)
print(dt)

Bước đầu tiên

Để tạo thủ công một đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
8, chúng ta có thể sử dụng
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
2 hoặc
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
3.

Nhưng trong thực tế, chúng ta thường cần phải phân tích một chuỗi hiện có. Chúng ta có thể sử dụng hàm

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
4, chẳng hạn như
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
5 trong đó bạn phải định dạng cụ thể. Chi tiết của mã định dạng khác nhau có thể được tìm thấy trong tài liệu chính thức.

Ngoài ra, một cách thuận tiện hơn là sử dụng mô -đun DateParse. Ví dụ là

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
6,
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
7 hoặc
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
8

Hai cách tiếp cận trên sẽ trả về một đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
8.

Bước thứ 2

Sử dụng đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
8 thu được để gọi
from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
1. Ví dụ,

Python

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.

Thật khó để quyết định sử dụng định dạng nào. Một cách tốt hơn là lấy một đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
0 để gọi
from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
3. Ví dụ,

Python

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard

Thật khó để quyết định sử dụng định dạng nào. Một cách tốt hơn là lấy một đối tượng

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard
0 để gọi
from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
3. Ví dụ,

Hơn 15 bài tập và câu đố dành riêng cho chủ đề

Mỗi bài tập chứa 10 câu hỏi

  • Mỗi bài kiểm tra chứa 12-15 mcq
  • Trong hướng dẫn Python này, tôi sẽ chỉ cho bạn chính xác làm thế nào để có được ngày trong tuần của một ngày nhất định. Ví dụ: bạn muốn tìm ra tên hoặc số trong ngày từ DateTime.
  • Trong hướng dẫn DateTime này, tôi sẽ bao gồm:name of the day from the date. Like Monday, Tuesday.
  • Phương pháp
    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    
    # get day of week as an integer
    x = dt.weekday()
    print('Day of a week is:', x)
    6 để có được ngày trong tuần làm số, trong đó Thứ Hai là 0 và Chủ nhật là 6.
  • Phương pháp
    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    
    # get day of week as an integer
    x = dt.weekday()
    print('Day of a week is:', x)
    7 để có được ngày trong tuần của một ngày nhất định là một số nguyên, trong đó Thứ Hai là 1 và Chủ nhật là 7.

from datetime import datetime # get current datetime dt = datetime.now() print('Datetime is:', dt) # get day of week as an integer x = dt.weekday() print('Day of a week is:', x)8 Phương pháp để có được tên của ngày kể từ ngày. Giống như Thứ Hai, Thứ Ba.

Cách sử dụng mô -đun lịch để lấy tên của ngày từ DateTime.

  1. Phương pháp Pandas
    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    
    # get day of week as an integer
    x = dt.weekday()
    print('Day of a week is:', x)
    9 để có được số và tên của ngày.

    Làm thế nào để có được ngày trong tuần từ DateTime ở Python

  2. Các bước dưới đây cho thấy cách sử dụng phương thức mô -đun DateTime
    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    
    # get day of week as an integer
    x = dt.weekday()
    print('Day of a week is:', x)
    6 để có được ngày một tuần làm số nguyên.

    Nhập mô -đun DateTime

  3. Mô -đun DateTime Python cung cấp các chức năng khác nhau để tạo và thao tác ngày và thời gian. Sử dụng câu lệnh
    Datetime is: 2022-04-13 11:21:18.052308
    Day of a week is: 2
    1 để nhập lớp
    dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
    print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
    print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
    print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
    
    8 từ mô -đun DateTime.

    Tạo đối tượng DateTimeMonday is 0 and Sunday is 6. For example, the

    Datetime is: 2022-04-13 11:21:18.052308
    Day of a week is: 2
    4 is a Monday. So its weekday number is 0.

Mô -đun DateTime có lớp DateTime chứa ngày và giờ địa phương hiện tại. Sử dụng phương thức Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 23 để có được ngày và thời gian hiện tại. Hoặc, nếu bạn có DateTime ở định dạng chuỗi, hãy tham khảo chuyển đổi một chuỗi thành một đối tượng DateTime.

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)

Output::

Datetime is: 2022-04-13 11:21:18.052308
Day of a week is: 2

Sử dụng phương thức Day () trong tuần ()

Phương thức Days () trả về ngày trong tuần với tư cách là một số nguyên, trong đó Thứ Hai là 0 và Chủ nhật là 6. Ví dụ, Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 24 là thứ Hai. Vì vậy, số ngày trong tuần của nó là 0.

Ví dụ: Nhận ngày trong một tuần từ DateTime

Đầu ra là 2, tương đương với thứ Tư vì thứ Hai là 0.Monday is 1 and Sunday is 7. i.e., To start from the weekday number from 1, we can use

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
7 in place of
from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
6.

Example::

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)
print('Weekday is:', dt.isoweekday())

Output::

Datetime is: 2022-05-02 13:24:30.636135
Weekday is: 1

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
7 để có được một ngày trong tuần của một ngày nhất định ở Python

Phương pháp from datetime import datetime # get current datetime dt = datetime.now() print('Datetime is:', dt) # get day of week as an integer x = dt.weekday() print('Day of a week is:', x)6 mà chúng tôi đã sử dụng trên trả về ngày trong tuần như một số nguyên, trong đó Thứ Hai là 0 và Chủ nhật là 6.

Sử dụng phương thức isoweekday () để có được ngày trong tuần như một số nguyên, trong đó Thứ Hai là 1 và Chủ nhật là 7. tức là, để bắt đầu từ số ngày trong tuần từ 1, chúng ta có thể sử dụng

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
7 thay cho
from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
6.
If you’d like Sunday to be day 0

Đầu ra là 1, tương đương với thứ Hai vì thứ Hai là 1.

Mã ký tự

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)
print('Weekday is:', dt.isoweekday())
2 trả về ngày trong tuần là số thập phân, trong đó 0 là Chủ nhật và 6 là thứ bảy.0 is Sunday, and 6 is Saturday.

from datetime import datetime

# get current date
d = datetime.now().date()
# get weekday
print(d.strftime('%w'))

Output::

Date: 2022-05-02
1

Lấy tên ngày trong tuần của một ngày bằng phương thức strftime ()

Trong các ví dụ trên, chúng tôi đã thấy làm thế nào để có được ngày trong tuần của một ngày là một số nguyên. Trong phần này, hãy để Lừa xem cách lấy tên ngày của một ngày nhất định (chẳng hạn như Thứ Hai, Thứ Ba, Thứ Tư, Thứ Năm, Thứ Sáu, Thứ Bảy và Chủ Nhật) từ một đối tượng DateTime ở Python.get the day name of a given date ( such as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday) from a datetime object in Python.

Ví dụ,

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)
print('Weekday is:', dt.isoweekday())
3 sẽ trả lại cho tôi thứ ba.

Sử dụng phương thức strftime () của mô -đun DateTime để lấy tên ngày bằng tiếng Anh bằng Python. Nó sử dụng một số chỉ thị tiêu chuẩn để thể hiện một DateTime ở định dạng chuỗi. Chỉ thị

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)
print('Weekday is:', dt.isoweekday())
4 trả về tên đầy đủ của ngày trong tuần. Giống như, Thứ Hai, Thứ Ba.

Example::

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get weekday name
print('day Name:', dt.strftime('%A'))

Output::

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
0

Lấy tên ngày trong tuần bằng cách sử dụng mô -đun lịch

Mô -đun Lịch Python cho phép bạn xuất lịch như chương trình Unix Cal và cung cấp các chức năng hữu ích bổ sung liên quan đến lịch.

Phương pháp

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)
print('Weekday is:', dt.isoweekday())
5 được sử dụng để lấy tên của ngày kể từ ngày. Phương pháp này chứa một mảng đại diện cho các ngày trong tuần trong địa phương hiện tại. Ở đây, thứ Hai được đặt tại INDEX 0th Index.the day from the date. This method contains an array that represents the days of the week in the current locale. Here, Monday is placed at index 0th index.

Thí dụ::

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
1

Output::

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
2

Kiểm tra xem một ngày là một ngày trong tuần hoặc cuối tuần

Chúng ta có thể sử dụng phương pháp

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
6 của đối tượng
from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)
print('Weekday is:', dt.isoweekday())
7 để xác định xem ngày đã cho là một ngày trong tuần hoặc cuối tuần.

Lưu ý: Phương thức Days () trả về ngày trong tuần với tư cách là một số nguyên, trong đó Thứ Hai là 0 và Chủ nhật là 6. Ví dụ, ngày (2022, 05, 02) là thứ Hai. Vì vậy, số ngày trong tuần của nó là 0.: The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, the date(2022, 05, 02) is a Monday. So its weekday number is 0.

Thí dụ::

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
3

Output::

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
4

Kiểm tra xem một ngày là một ngày trong tuần hoặc cuối tuần

Chúng ta có thể sử dụng phương pháp

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
6 của đối tượng
from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)
print('Weekday is:', dt.isoweekday())
7 để xác định xem ngày đã cho là một ngày trong tuần hoặc cuối tuần.

  • Lưu ý: Phương thức Days () trả về ngày trong tuần với tư cách là một số nguyên, trong đó Thứ Hai là 0 và Chủ nhật là 6. Ví dụ, ngày (2022, 05, 02) là thứ Hai. Vì vậy, số ngày trong tuần của nó là 0.
  • Phương pháp dấu thời gian của Pandas để có được tên của ngày trong Python

Example::

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
5

Output::

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
6

Nếu bạn đang làm việc với loạt gấu trúc hoặc dataFrame, thì phương thức from datetime import datetime # get current datetime dt = datetime.now() print('Datetime is:', dt) print('Weekday is:', dt.isoweekday())8 rất hữu ích để có được số và tên ngày.

Đầu tiên, vượt qua ngày ở định dạng

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)
print('Weekday is:', dt.isoweekday())
9 dưới dạng tham số của nó.

  • Tiếp theo, sử dụng phương pháp
    Datetime is: 2022-05-02 13:24:30.636135
    Weekday is: 1
    0 và
    Datetime is: 2022-05-02 13:24:30.636135
    Weekday is: 1
    1 để có được số ngày và tên trong tuần.
  • Bản tóm tắt
  • Trong hướng dẫn này, chúng tôi đã học được cách lấy tên của ngày bằng tiếng Anh và ngày trong tuần với tư cách là một số nguyên trong Python.
  • Sử dụng phương pháp
    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    
    # get day of week as an integer
    x = dt.weekday()
    print('Day of a week is:', x)
    6 để có được ngày trong tuần như một số nguyên. Trong phương pháp này, Thứ Hai là 0 và Chủ nhật là 6

Example::

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
7

Nếu bạn muốn bắt đầu từ 1, hãy sử dụng phương thức from datetime import datetime # get current datetime dt = datetime.now() print('Datetime is:', dt) # get day of week as an integer x = dt.weekday() print('Day of a week is:', x)7. Nó trở lại vào ngày trong tuần với tư cách là một số nguyên, trong đó Thứ Hai là 1 và Chủ nhật là 7.

Sử dụng phương thức

from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)

# get day of week as an integer
x = dt.weekday()
print('Day of a week is:', x)
8 hoặc mô -đun lịch để lấy tên của ngày bằng tiếng Anh. Giống như Thứ Hai, Thứ Ba.

  • Sử dụng phương pháp Pandas
    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    
    # get day of week as an integer
    x = dt.weekday()
    print('Day of a week is:', x)
    9 để có được số và tên của ngày.Topic-specific Exercises and Quizzes
  • Bài tập và câu đố Python
  • Các bài tập mã hóa miễn phí và các câu đố bao gồm các vấn đề cơ bản của Python, cấu trúc dữ liệu, phân tích dữ liệu, v.v.