美文网首页
Python获取自然月时间并转换时间戳

Python获取自然月时间并转换时间戳

作者: 别谈以后Axy | 来源:发表于2019-08-23 10:10 被阅读0次
  • 引入模块
import os,sys
import MySQLdb
import datetime
import time
reload(sys)
sys.setdefaultencoding('utf8')

  • 获取当前月份第一天到最后一天
def getMonth():
       year = str(datetime.date.today().year)
       d = datetime.date.today()
       month = '%02d' % d.month
       if month in ['01','03','05','07','08','10','12']:
               s = str("-01 00:00:00")
               e = str("-31 23:59:59")
               startTime = year + "-" + str(month) + s
               endTime = year + "-" +  str(month) + e
               list = [startTime,endTime]
               return list
       elif month == '02':
               s = str("-01 00:00:00")
               e = str("-28 23:59:59")
               startTime = year + "-" + str(month) + s
               endTime = year + "-" +  str(month) + e
               list = [startTime,endTime]
               return list
       else:
               s = str("-01 00:00:00")
               e = str("-30 23:59:59")
               startTime = year + "-" + str(month) +s
               endTime = year + "-" +  str(month) + e
               list = [startTime,endTime]
               return list
  • 转换当前时间为时间戳
list_time = getMonth()
starTimestamp = int(time.mktime(time.strptime(list_time[0], '%Y-%m-%d %H:%M:%S')))
endTimestamp = int(time.mktime(time.strptime(list_time[1], '%Y-%m-%d %H:%M:%S')))
  • 效果
[root@mailvip python]# py recording.py
1564588800
1567180799
[root@mailvip python]#

相关文章

网友评论

      本文标题:Python获取自然月时间并转换时间戳

      本文链接:https://www.haomeiwen.com/subject/nckzsctx.html