CodeHelper CodeHelper
首页
JavaScript
PHP
Python
Git速查表 (opens new window)
博客 (opens new window)
首页
JavaScript
PHP
Python
Git速查表 (opens new window)
博客 (opens new window)
  • 开始

    • 使用
  • 函数方法

    • 字符串
    • 日期时间
      • 初始化一个日期,可加减时间
    • 文件操作
    • 请求
    • pandas操作Excel
    • SQL语句
    • ...
目录

日期时间

# 初始化一个日期,可加减时间

import time

def getDate(dateStr, dateFormat="%Y-%m-%d %H:%M:%S", returnFormat="%Y-%m-%d %H:%M:%S", addSeconds=0, reduceSeconds=0):
    """
    获取一个时间,可格式化时间
    :param dateStr: 传入一个时间字符串 ”2022-07-15 13:13:13“
    :param dateFormat: 如果dateStr时间格式不为"%Y-%m-%d %H:%M:%S", 请纠正此参数
    :param returnFormat: 返回的时间格式,默认"%Y-%m-%d %H:%M:%S"
    :param addSeconds: 加多少秒,默认0
    :param reduceSeconds: 减多少秒,默认0
    :return:
    """

    # 转换为 时间数组
    timeArray = time.strptime(str(dateStr), dateFormat)
    # 转换为 时间戳:
    timeStamp = int(time.mktime(timeArray))

    if addSeconds:
        timeStamp = timeStamp + addSeconds

    if reduceSeconds:
        timeStamp = timeStamp - reduceSeconds

    # 时间戳转回 时间数组
    timeArray = time.localtime(timeStamp)

    # 获取指定格式的 日期
    return time.strftime(returnFormat, timeArray)
上次更新: 2022/08/23, 18:58:25
字符串
文件操作

← 字符串 文件操作→

Theme by Vdoing | Copyright © 2022-2023 CodeHelper
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式