简单示例:

常用函数封装:

# -*- coding: utf-8 -*-
# @Time : 2019/8/6 14:37
# @Author : wangmengmeng
import datetime
import time
import random class Tool:
@staticmethod
def get_ymd(d, h):
"""
获取日期,格式为%Y-%m-%d
:param d: d可取0(表示当前日期),正(表示当前日期+d天),负(表示当前日期-d天)
:param h: 可取h0(表示当前日期),正(表示当前时间点+小时),负(表示当前日期-h小时)
:return:
"""
date = ((datetime.datetime.now() + datetime.timedelta(days=d)) + datetime.timedelta(hours=h)).strftime(
"%Y-%m-%d")
return date @staticmethod
def get_date(d, h):
"""
获取日期,格式为%Y-%m-%d %H:%M:%S
:param d:
:param h:
:return:
"""
date = ((datetime.datetime.now() + datetime.timedelta(days=d)) + datetime.timedelta(hours=h)).strftime(
"%Y-%m-%d %H:%M:%S")
return date @staticmethod
def get_ts(d, h):
"""
获取13位时间戳
:param d:
:param h:
:return:
"""
date = ((datetime.datetime.now() + datetime.timedelta(days=d)) + datetime.timedelta(hours=h)).strftime(
"%Y-%m-%d %H:%M:%S")
# ts = int(time.mktime(time.strptime(date, "%Y-%m-%d %H:%M:%S"))) # 获取10位时间戳
ts = int(time.mktime(time.strptime(date, "%Y-%m-%d %H:%M:%S"))) * 1000 # 获取13位时间戳
return ts @staticmethod
def get_t(d, h):
"""
获取10位时间戳
:param d:
:param h:
:return:
"""
date = ((datetime.datetime.now() + datetime.timedelta(days=d)) + datetime.timedelta(hours=h)).strftime(
"%Y-%m-%d %H:%M:%S")
# ts = int(time.mktime(time.strptime(date, "%Y-%m-%d %H:%M:%S"))) # 获取10位时间戳
ts = int(time.mktime(time.strptime(date, "%Y-%m-%d %H:%M:%S"))) * 1000 # 获取13位时间戳
return ts @staticmethod
def get_endtoday():
now = datetime.datetime.now()
zeroToday = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,
microseconds=now.microsecond)
lastToday = zeroToday + datetime.timedelta(hours=23, minutes=59, seconds=59)
return lastToday @staticmethod
def get_random(a, b):
"""
生成一个指定范围内的整数
:param a:
:param b:
:return:
"""
return random.randint(a, b)

python模块-time、datetime的更多相关文章

  1. Python模块学习 ---- datetime

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于time模块, ...

  2. python模块之datetime

    相比于time模块,datetime模块的接口则更直观.更容易调用 datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day: ...

  3. python模块time&datetime&json & picle&14.logging等

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  4. Python模块 - time,datetime,calendar

    time模块 localtime 当前时间的struct_time形式 >>> time.localtime() time.struct_time(tm_year=2015, tm_ ...

  5. python模块之datetime方法详细介绍

    datetime Python提供了许多内置模块用于操作时间日期,如calendar,time,datetime,这篇文章主要是对datetime进行汇总,datetime模块的借口实现原则更加直观, ...

  6. Python模块--time&datetime

    一.Python中时间的表示方式 1.时间戳  如 1552623413.043036 2.格式化的时间字符串  如 2015-12-02 3.struct_time  是一个元组 共有九个元素 二. ...

  7. 【转载】【Python模块】datetime

    原文地址 一.datetime模块介绍 (一).datetime模块中包含如下类: 类名 功能说明 date 日期对象,常用的属性有year, month, day time 时间对象 datetim ...

  8. python模块:datetime

    # Stubs for datetime # NOTE: These are incomplete! import sys from typing import Optional, SupportsA ...

  9. python模块--time & datetime

    time模块 #获取当前时间的时间戳 import time >>> time.time() 1535004894.0959966 #日期字符串转化成时间戳 >>> ...

  10. python模块之time和datetime

    33.python模块之time      1.>>> time.time() 1470900847.8458395 ==>时间戳,从1970年到现在.      2.> ...

随机推荐

  1. 04 (OC)* weak的实现原理

    一:Weak 表 1: Runtime 维护了一个 Weak 表,用于存储所有 Weak 指针.Weak 表是一个哈希表,Key 是对象的地址,Value 是一个数组,数组里面放的是 Weak 指针的 ...

  2. mysql 存储过程 (ps:用法自己看 :)

    delimiter // drop procedure if exists operate_tables // create procedure operate_tables (in db_name ...

  3. elasticsearch集群扩容和容灾

    elasticsearch专栏:https://www.cnblogs.com/hello-shf/category/1550315.html 一.集群健康 Elasticsearch 的集群监控信息 ...

  4. FPipe端口转发

    目录 0x01 FPipe介绍 0x02 端口转发 0x03 msf正向上线 注: 边界机器 win08 192.168.222.175 内网机器 win7 192.168.222.137 msf机器 ...

  5. Dungeon Master POJ-2251 三维BFS

    题目链接:http://poj.org/problem?id=2251 题目大意 你被困在了一个三维的迷宫,找出能通往出口的最短时间.如果走不到出口,输出被困. 思路 由于要找最短路径,其实就是BFS ...

  6. 数据结构之二叉树篇卷三 -- 二叉树非递归遍历(With Java)

    Nonrecursive Traversal of Binary Tree First I wanna talk about why we should <code>Stack</c ...

  7. CF #579 (Div. 3) C.Common Divisors

    C.Common Divisors time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...

  8. 通过python代码对域名ssl证书进行监控

    根据公司要求,要求用zabbix对域名的ssl证书进行到期监控 直接上代码 #!/usr/bin/env python3 from urllib3.contrib import pyopenssl f ...

  9. gym102201E_Eat Economically

    题意 给\(2n\)个物品,分别有\(a,b\)属性,对于\(i=1...n\),选择\(i\)个\(a\)属性和\(i\)个\(b\)属性,且每个物品只能作为一种属性的贡献,求最小的值. 分析 看了 ...

  10. 阿里云服务器CentOS6.9 tomcat配置https安全访问

    应用场景 上线微信小程序的时候,域名要求https安全格式,否则获取数据异常. 第一步.SSL证书获取 获取SSL证书方式很多种,包括网页生成.工具生成等,这里我使用阿里云平台获取免费ssl证书的方法 ...