python根据日期返回星期几的方法

yipeiwu_com6年前Python基础

本文实例讲述了python根据日期返回星期几的方法。分享给大家供大家参考。具体如下:

这个函数给定日期,输出星期几,至于0是星期一还是星期天,这和时区有关,反正我这里是0表示星期一

import time,datetime
def get_week_day(date):
  week_day_dict = {
    0 : '星期一',
    1 : '星期二',
    2 : '星期三',
    3 : '星期四',
    4 : '星期五',
    5 : '星期六',
    6 : '星期天',
  }
  day = date.weekday()
  return week_day_dict[day]

如果要输出当天是星期几,执行下面的代码

复制代码 代码如下:
print(get_week_day(datetime.datetime.now()))

输出结果为:
星期一

希望本文所述对大家的Python程序设计有所帮助。

相关文章

ubuntu 16.04下python版本切换的方法

ubuntu 16.04下python版本切换的方法

本文主要是记录ubuntu 16.04下python环境配置,具体内容如下 对于ubuntu 16.04,由于本身是自带python,这样就减少了在windows下的下载和环境变量配置,...

python 实现一次性在文件中写入多行的方法

将要写入的内容 构造 进一个list 中,使用writelines()方法 一次性写入。 file_w.writelines(list) file_w.flush() file.c...

JavaScript中的模拟事件和自定义事件实例分析

本文实例讲述了JavaScript中的模拟事件和自定义事件。分享给大家供大家参考,具体如下: 前面介绍了JavaScript中为事件指定处理程序的五种方式和JavaScript的事件对象...

python调用c++ ctype list传数组或者返回数组的方法

示例1: pycallclass.cpp: #include <iostream> using namespace std; typedef unsigned char...

SublimeText 2编译python出错的解决方法(The system cannot find the file specified)

[Error 2] The system cannot find the file specified 解决方法:1.环境变量path添加:C:\Python32\Tools\Scrip...