python datetime使用,Python datetime时间的使用

Python datetime时间的使用

1、获取当前年月日时分秒

# -*- encoding=utf-8 -*-

import datetime

now = datetime.datetime.now()

print("now:{}".format(now))

year = now.year

print("year:{}".format(year))

month = now.month

print("month:{}".format(month))

day = now.day

print("day:{}".format(day))

hour = now.hour

print("hour:{}".format(hour))

minute = now.minute

print("minute:{}".format(minute))

second = now.second

print("second:{}".format(second))

2、datetime转为string

# -*- encoding=utf-8 -*-

import datetime

now = datetime.datetime.now()

print('type:{}'.format(type(now)))

print('now datetime:{}'.format(now))

now_string = now.strftime('%Y-%m-%d %H:%M:%S')

print('type:{}'.format(type(now_string)))

print('now string:{}'.format(now_string))

3、string转为datetime

# -*- encoding=utf-8 -*-

import datetime

time_str = '2021-01-28 10:51:26'

time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')

print('type:{}'.format(type(time_date)))

print(time_date)

4、时间相加

# -*- encoding=utf-8 -*-

import datetime

time_str = '2021-01-28 10:00:00'

time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')

print('原始时间:\t\t\t\t{}'.format(time_date))

add_info = datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)

add_end = time_date + add_info

print('加上1天2个小时3分钟4秒后:\t{}'.format(add_end))

5、时间相减

①两个时间差

# -*- encoding=utf-8 -*-

import datetime

time_str = '2021-01-28 10:00:00'

time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')

print('原始时间:\t{}'.format(time_date))

time_str = '2021-05-29 12:12:12'

time_date2 = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')

print('原始时间2:\t{}'.format(time_date2))

time_date3 = time_date2 - time_date

print('时间差:{}'.format(time_date3))

②减去1天2个小时3分钟4秒(加负数)

# -*- encoding=utf-8 -*-

import datetime

time_str = '2021-01-28 10:00:00'

time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')

print('原始时间:\t\t\t\t{}'.format(time_date))

add_info = datetime.timedelta(days=-1, hours=-2, minutes=-3, seconds=-4)

add_end = time_date + add_info

print('减去1天2个小时3分钟4秒后:\t{}'.format(add_end))

Python datetime时间的使用 相关文章

用python操作windows应用程序

今天要实现的是,打开或连接一个记事本 向记事本里输入内容 然后复制粘贴 查看记事本 对文字放大并还原 替换所有文字 保存 from pywinauto import applicationfrom pywinauto.keyboard import send_keysimport pyautoguiimport subprocessimport re#获取note

Python 写Excel实战:json存Excel

案例一: import xlwt data_json={ "1":["小花",99,100,98.5], "2":["小王",90,30.5,95], "3":["小明",67.5,49.6,88] } title = ["编号","姓名","语文成绩","数学成绩","英语成绩","平均分","总分"] #1、循环一下,把字典的key插入到字典的value里面,同时计

python编码规范——空格的使用

1.概述 在python代码中,需要加空格的一共有四个地方需要特别注意。即二元运算符,逗号,冒号,#号 2.逗号 逗号后面要加空格,但是如果后面是小括号,则不用 # 逗号 func = (0,) x, y = y, x print x, y 3.冒号 冒号前不加空格,冒号后要加空格,但在切片里

渣渣的Leetcode之旅(Python3)_8. 字符串转换整数 (atoi)(中等)

请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。 函数 myAtoi(string s) 的算法如下: 读入字符串并丢弃无用的前导空格 检查第一个字符(假设还未到字符末尾)为正还是负号,读取该字符(

十分钟入门 Python 教程

题记 去年和 amile 童鞋定了个小目标,决定带着点兴趣和热情,顺着全民学 Python 的热潮,随波逐流。奈何二零二零年是真的魔幻,唉,生活有你读不懂的诗,还有到不了的远方,我把那一年的辛酸过往,阉割成了一首诗。“生活似一杯苦茶,往事如逝水一般,邂逅

python 刷题时的坑 新手

说明 本版块是用来记录刷题中遇到的那些坑 力扣 具体 1. 迭代方面 因为题解是 python 类的形式,所以要迭代函数的时候需要 self 句点访问法,详见斐波那契数列 return self.fib(n-1) + self.fib(n-2) 2. 值的替代 有时候习惯了 n ,题目中有时候是别的,如 am

Python3 多进程和多线程

多进程 multiprocessing 模块就是跨平台版本的多进程模块。提供了一个 Process 类来代表一个进程对象。 from multiprocessing import Processimport os# 子进程要执行的代码def run_proc(name): print('Run child process %s (%s)...' % (name, os.getpid())

python leetcode 最长公共子序列 动态规划算法

题目链接 https://leetcode-cn.com/problems/longest-common-subsequence/ 题目介绍 最长公共子序列 给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。 一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的

python leetcode 最长连续递增序列 动态规划算法

题目链接 https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/ 题目介绍 最长连续递增序列 给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。 连续递增的子序列 可以由两个下标 l 和 r(l r)确定

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值