Cara menggunakan week in python

In this Python tutorial, I will show you exactly how to get the day of the week of a given date. For example, you want to find out the day’s name or number from the datetime.

In this datetime guide, I’ll cover:

  • Datetime is: 2022-04-13 11:21:18.052308
    Day of a week is: 2
    5 method to get the day of the week as a number, where Monday is 0 and Sunday is 6.
  • Datetime is: 2022-04-13 11:21:18.052308
    Day of a week is: 2
    6 method to get the weekday of a given date as an integer, where Monday is 1 and Sunday is 7.
  • Datetime is: 2022-04-13 11:21:18.052308
    Day of a week is: 2
    7 method to get the name of the day from the date. Like Monday, Tuesday.
  • How to use the calendar module to get the name of the day from datetime.
  • Pandas
    Datetime is: 2022-04-13 11:21:18.052308
    Day of a week is: 2
    8 method to get the number and name of the day.

Table of contents

How to Get the day of the week from datetime in Python

The below steps show how to use the datetime module’s

Datetime is: 2022-04-13 11:21:18.052308
Day of a week is: 2
5 method to get the day of a week as an integer number.

  1. Import datetime module

    Python datetime module provides various functions to create and manipulate the date and time. Use the

    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    print('Weekday is:', dt.isoweekday())
    0 statement to import a
    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    print('Weekday is:', dt.isoweekday())
    1 class from a datetime module.

  2. Create datetime object

    The datetime module has a datetime class that contains the current local date and time. Use the

    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    print('Weekday is:', dt.isoweekday())
    2 method to get the current date and time. Or, If you have datetime in a string format, refer to converting a string into a datetime object.

  3. Use the weekday() method

    The method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, the

    from datetime import datetime
    
    # get current datetime
    dt = datetime.now()
    print('Datetime is:', dt)
    print('Weekday is:', dt.isoweekday())
    3 is a Monday. So its weekday number is 0.

Example: Get the day of a week from 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

The output is 2, equivalent to Wednesday as Monday is 0.

Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 26 to get a weekday of a given date in Python

The

Datetime is: 2022-04-13 11:21:18.052308
Day of a week is: 2
5 method we used above returns the day of the week as an integer, where Monday is 0 and Sunday is 6.

Use the method to get the day of the week as an integer, where Monday is 1 and Sunday is 7. i.e., To start from the weekday number from 1, we can use

Datetime is: 2022-04-13 11:21:18.052308
Day of a week is: 2
6 in place of
Datetime is: 2022-04-13 11:21:18.052308
Day of a week is: 2
5.

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

The output is 1, which is equivalent to Monday as Monday is 1.

Get a weekday where Sunday is 0

The strftime() approach If you’d like Sunday to be day 0

The

Datetime is: 2022-04-13 11:21:18.052308
Day of a week is: 2
7 uses some standard directives to convert a datetime into a string format. The same directives are shared between
from datetime import datetime

# get current datetime
dt = datetime.now()
print('Datetime is:', dt)
print('Weekday is:', dt.isoweekday())
9 and
Datetime is: 2022-04-13 11:21:18.052308
Day of a week is: 2
7 methods.

The

Datetime is: 2022-05-02 13:24:30.636135
Weekday is: 1
1 character code returns weekday as a decimal number, where 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

Get the Weekday Name of a Date using strftime() method

In the above examples, we saw how to get the weekday of a date as an integer. In this section, let’s see how to get the day name of a given date ( such as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday) from a datetime object in Python.

For example,

Datetime is: 2022-05-02 13:24:30.636135
Weekday is: 1
2 should return me “Tuesday”.

Use the strftime() method of a datetime module to get the day’s name in English in Python. It uses some standard directives to represent a datetime in a string format. The

Datetime is: 2022-05-02 13:24:30.636135
Weekday is: 1
3 directive returns the full name of the weekday. Like, Monday, Tuesday.

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:

Datetime is: 2022-05-02 13:40:57.060953
day Name: Monday

Get the Weekday Name from date using Calendar Module

Python allows you to output calendars like the Unix cal program and provides additional useful functions related to the calendar.

The

Datetime is: 2022-05-02 13:24:30.636135
Weekday is: 1
4 method is used to get the name of 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.

Example:

import calendar
from datetime import date

# get today's date
d = date.today()
print('Date is:', d)

# get day name in english
x = calendar.day_name[d.weekday()]
print('Weekday name is:', x)

Output:

Date is: 2022-05-02
Weekday name is: Monday

Check if a date is a weekday or weekend

We can use the

Datetime is: 2022-04-13 11:21:18.052308
Day of a week is: 2
5 method of a
Datetime is: 2022-05-02 13:24:30.636135
Weekday is: 1
6 object to determine if the given date is a weekday or weekend.

Note: 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.

Example:

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

Output:

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

Pandas Timestamp Method to Get the Name of the Day in Python

If you are working with a pandas series or dataframe, then the

Datetime is: 2022-05-02 13:24:30.636135
Weekday is: 1
7 method is helpful to get the day number and name.

  • First, pass the date in
    Datetime is: 2022-05-02 13:24:30.636135
    Weekday is: 1
    8 format as its parameter.
  • Next, use the
    Datetime is: 2022-05-02 13:24:30.636135
    Weekday is: 1
    9 and
    from datetime import datetime
    
    # get current date
    d = datetime.now().date()
    # get weekday
    print(d.strftime('%w'))
    0 method to get the weekday number and name.

Example:

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

Output:

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

Summary

In this tutorial, we learned how to get the Name of the day in English and the day of the week as an integer number in Python.