[PYTHON] Find out the day of the week with datetime

Use the weekday method to find out the day of the week in python's datetime module. The weekday method returns the day of the week of the date created by the object as a number 0-6. Since 0 is Monday, 1 is Tuesday, and 6 is Sunday, you can convert it to a day of the week if you prepare a list that stores Monday to Sunday.

python


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from datetime import *
from time import *

print "Enter the date in the following format Example:'2016/07/30'."
user_input_date = raw_input("date:")
yobi = ["Month","fire","water","wood","Money","soil","Day"]
while user_input_date != "bye":
    try:
        a = datetime.strptime(user_input_data,'%Y/%m/%d')
        print "{}Is{}It's the day of the week".format(user_input_date,yobi[a.weekday()])
    except ValueError:
        print "Wrong date"
    user_input_date = raw_input("your date :")
else:
    sys.exit(1)

It looks like this when written in python3.x.

python


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from datetime import *
from time import *

print ("Please enter the date'2016/07/30'.")
user_input_date = input("your date :")

yobi = ["Month","fire","water","wood","Money","soil","Day"]

while user_input_date != "bye":
    try:
        a = datetime.strptime(user_input_date,'%Y/%m/%d')
        print ("{}Is{}It's the day of the week".format(user_input_date,yobi[a.weekday()]))
    except ValueError:
        print ("Wrong date")
    user_input_date = input("your date :")
else:
    sys.exit(1)

Recommended Posts

Find out the day of the week with datetime
Find out the location of packages installed with pip
[Python] Get the day of the week (English & Japanese)
Find the sum of unique values with pandas crosstab
Find out the location of Python class definition files.
Find out the version of the language you are running
Which day of the week do you buy gold?
I tried to find the entropy of the image with python
Maya | Find out the number of polygons in the selected object
Find out the apparent width of a string in python
I tried to find the average of the sequence with TensorFlow
Python --Find out number of groups in the regex expression
Find the definition of the value of errno
The day of docker run (note)
Find the Levenshtein Distance with python
Find the optimal value of a function with a genetic algorithm (Part 2)
March 14th is Pi Day. The story of calculating pi with python
Find the transfer function of one degree of freedom system with PythonControl.
Find out the mystery change of Pokédex description by Levenshtein distance
Find the numerical solution of the second-order ordinary differential equation with scipy
Find the area of the union of overlapping rectangles
Find the inertial spindle and moment of inertia from the inertial tensor with NumPy
Align the size of the colorbar with matplotlib
How to find out the number of CPUs without using the sar command
Check the existence of the file with python
Find the SHA256 value with R (with bonus)
Find out the CentOS7 MAC address (HWADDR)
Migemo version of the: find command,: mfind
Find the general terms of the Tribonacci sequence with linear algebra and Python
[Completed version] Try to find out the number of residents in the town from the address list with Python
The second night of the loop with for
Get the last day of the specified month
Find out the name of the method that called it from the method that is python
Get a datetime instance at any time of the day in Python
Find the coefficients of the least squares polynomial
Count the number of characters with echo
Watch out for the return value of __len__
Watch out for randint
Declaration of C global variables
2015-03-25 python> Multi-line comment> Watch out for indent
The story of manipulating python global variables
Find out the day of the week with datetime
A programming beginner tried to find out the execution time of sorting etc.
Find out how to divide a file with a certain number of lines evenly
Get one day of the week from Zeller's joint ceremony ~ Incidentally, the perpetual calendar ~
Let Code Day10 Starting from Zero "1431. Kids With the Greatest Number of Candies"
Note: How to get the last day of the month with python (added the first day of the month)
The story of doing deep learning with TPU
Note: Prepare the environment of CmdStanPy with docker
Prepare the execution environment of Python3 with Docker
Combinatorial optimization to find the hand of "Millijan"
2016 The University of Tokyo Mathematics Solved with Python
Find the mood value with python (Rike Koi)
[Note] Export the html of the site with python.
Increase the font size of the graph with matplotlib
Calculate the total number of combinations with python
Watch out for the return value of __len__
Check the date of the flag duty with Python
Find the number of days in a month
Eliminate the inconveniences of QDock Widget with PySide
Find the divisor of the value entered in python
Challenge the Tower of Hanoi with recursion + stack
Rewrite the name of the namespaced tag with lxml
Fill the browser with the width of Jupyter Notebook
Predict the second round of summer 2016 with scikit-learn
Dump the contents of redis db with lua