首页 > 技术文章 > python中常见的日期换算

wutongyuhou 2014-02-25 19:27 原文

  time模块提供各种操作时间的函数
  说明:一般有两种表示时间的方式:
       第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
       第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同
    year (four digits, e.g. 1998)
    month (1-12)
    day (1-31)
    hours (0-23)
    minutes (0-59)
    seconds (0-59)
    weekday (0-6, Monday is 0)
    Julian day (day in the year, 1-366)

1.判断今年是否为闰年。

闰年定义

import time
thisYear = time.localtime()[0]
print time.localtime()
if thisYear%4==0 and thisYear%100!=0:
print "This year is not a typical Year"
else:
print "This year is a typical year."

2.任意输入日期,判断这是该年的第几天

year=0
month=0
date=0
year = int(raw_input("Please input the year>"))
month = int(raw_input("Please input the month>"))
date = int(raw_input("Please input the date>"))
day=date
normal=[31,28,31,30,31,30,31,31,30,31,30,31]
run=[31,29,31,30,31,30,31,31,30,31,30,31]
if year%4==0 and year%100!=0 or year%400==0:
  list=run
else:
  list=normal

for i in list[:month-1]:
  day=i+day
print day

推荐阅读