获取本月的时间戳范围

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Henry  17607168727@163.com
import calendar
import datetime
import time


def return_time_stamp():
    # 获取当天日期
    today = datetime.datetime.today()
    year = today.year
    month = today.month
    # 查询当月总天数
    month_end_days = calendar.monthrange(year,month)[1]
    # 构造本月第一天文本格式时间
    first_day_str = """%s-%s-%s 00:00:00""" % (year,month,1)
    # 构造本月最后一天文本格式时间
    end_day_str = """%s-%s-%s 00:00:00""" % (year,month,month_end_days)
    # 转成时间数组
    first_time_array = time.strptime(first_day_str, "%Y-%m-%d %H:%M:%S")
    end_time_array = time.strptime(end_day_str, "%Y-%m-%d %H:%M:%S")
    first_time_stamp = int(time.mktime(first_time_array))
    end_time_stamp = int(time.mktime(end_time_array))
    print(year,month,month_end_days,first_time_array,end_time_array,first_time_stamp,end_time_stamp)
    return [first_time_stamp,end_time_stamp]

if __name__ == '__main__':
    
    return_time_stamp()
posted @ 2019-08-26 18:40  random_lee  阅读(881)  评论(0编辑  收藏  举报