Python 入门之 内置模块 -- datetime模块

1、datetime模块

from datetime import datetime

(1)datetime.now() 获取当前时间和日期

print(datetime.now())  # 获取当前时间

(2)获取指定时间和日期

dt = datetime(2018,5,20,13,14)
print(dt)

(3)指定时间

current_time = datetime.datetime.now()
print(current_time.replace(year=1977)) # 直接调整到1977年
print(current_time.replace(month=1)) # 直接调整到1月份
print(current_time.replace(year=1989,month=4,day=25)) # 1989-04-25 18:49:05.898601

(4)求时间差

print(datetime(2018,10,1,10,11,12) - datetime(2011,11,1,20,10,10))

(5)datetime.timestamp() 将对象转换成时间戳

d = datetime.now()
print(d.timestamp())

(6)datetime.fromtimestamp() 将时间戳转换为对象

import time
f_t = time.time()
print(datetime.fromtimestamp(f_t))

(7)datetime.strftime 将对象转换成字符串

d = datetime.now()
print(d.strftime("%Y-%m-%d %H:%M:%S"))

(8)datetime.strptime 将字符串转换成对象

s = "2018-12-31 10:11:12"
print(datetime.strptime(s,"%Y-%m-%d %H:%M:%S"))

(9)可以进行加减运算

from datetime import datetime,timedelta
print(datetime.now() - timedelta(days=1))
print(datetime.now() - timedelta(hours=1))

Python 入门之 内置模块 -- datetime模块的更多相关文章

  1. Python 入门之 内置模块 --logging模块

    Python 入门之 内置模块 --logging模块 1.logging -- 日志 (1)日志的作用: <1> 记录用户信息 <2> 记录个人流水 <3> 记录 ...

  2. Python 入门之 内置模块 -- re模块

    Python 入门之 内置模块 -- re模块 1.re 模块 (1)什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类 ...

  3. Python 入门之 内置模块 -- collections模块

    Python 入门之 内置模块 -- collections模块 1.collections -- 基于Python自带的数据类型之上额外增加的几个数据类型 from collections ​ 在内 ...

  4. Python 入门之 内置模块 -- hashlib模块

    Python 入门之 内置模块 -- hashlib模块 1.hashlib 摘要算法,加密算法 (1)主要用途: <1> 加密 : md5 sha1 sha256 sha512 md5, ...

  5. Python 入门之 内置模块 -- sys模块

    Python 入门之 内置模块 -- sys模块 1.sys模块 sys模块是与python解释器交互的一个接口 print(sys.path) #模块查找的顺序 print(sys.argv) # ...

  6. Python 入门之 内置模块 -- os模块

    Python 入门之 内置模块 -- os模块 1.os os是和操作系统做交互,给操作发指令 os模块是与操作系统交互的一个接口,它提供的功能多与工作目录,路径,文件等相关 (1)工作路径 prin ...

  7. Python 入门之 内置模块 -- 序列化模块(json模块、pickle模块)

    Python 入门之 内置模块 -- 序列化模块(json模块.pickle模块) 1.序列化 Python中这种序列化模块有三种: ​ json模块 : ​ 不同语言都遵循的一种数据转化格式,即不同 ...

  8. Python 入门之 内置模块 -- random模块

    Python 入门之 内置模块 -- random模块 1.random模块 import random # random -- 随机数 (1)选择1-50之间随机的整数 print(random.r ...

  9. Python 入门之 内置模块 -- time模块

    Python 入门之 内置模块 -- time模块 1.time模块 ​ time翻译过来就是时间,这个模块是与时间相关的模块 import time # 内置模块 -- 标准库 (1)time.ti ...

随机推荐

  1. Miller Rabin 算法简介

    0.1 一些闲话 最近一次更新是在2019年11月12日.之前的文章有很多问题:当我把我的代码交到LOJ上,发现只有60多分.我调了一个晚上,尝试用{2, 3, 5, 7, 11, 13, 17, 1 ...

  2. 【转】HDU-6035-Colorful Tree

    转自http://blog.csdn.net/Bahuia/article/details/76141574 题意: 题目链接:http://acm.hdu.edu.cn/showproblem.ph ...

  3. 10.Python内置函数一览表

    为了提高程序员的开发效率,Python 提供了很多可以直接拿来用的函数(初学者可以先理解为方法),每个函数都可以帮助程序员实现某些具体的功能. 举个例子,在 Python 2.x 中 print 只是 ...

  4. [CSP-S模拟测试]:最小值(DP+乱搞)

    题目背景 $Maxtir$更喜欢序列的最小值. 题目传送门(内部题128) 输入格式 第一行输入一个正整数$n$和四个整数$A,B,C,D$. 第二行输入$n$个整数,第$i$个数表示$a_i$. 输 ...

  5. Xcode 4.1实用小工具:模拟网络连接和带宽

    暂无评论 适用于Mac OS X Lion的开发套件Xcode 4.1中,有个新鲜的小工具叫做Network Link Conditioner(网络连接调节器),是一款具有高度可定制性的辅助工具,让用 ...

  6. JavaScript疑难杂症系列-事件

    事件这块知识点虽然是老生长谈的,但对于我来说多多整理,多多感悟,温故知新,每次看看这块都有不同的收获.(在这里我不会长篇大论,只会挑重点;具体的小伙伴们自行查找) 什么是事件 在编程时系统内发生的动作 ...

  7. 全排列函数next_permutation(a,a+n)

    #include<iostream> #include<algorithm> using namespace std; int main(){ ]; int n; cin> ...

  8. Scala学习(二)——高级特性

    apply() 方法 apply方法是Scala提供的一个语法糖 类名+括号,调用对象的apply方法 对象名+括号,调用类的apply方法 对apply方法的简单测试:(其中,带 new -- cl ...

  9. Ajax学习--理解 Ajax 及其工作原理

    Ajax 是 Asynchronous JavaScript and XML(以及 DHTML 等)的缩写. 下面是 Ajax 应用程序所用到的基本技术:• HTML 用于建立 Web 表单并确定应用 ...

  10. java工程打成jar包 - 使用maven assembly插件打包及手动打包

    在java工程打包的过程中遇到过不少问题,现在总结一下.一种是典型的maven工程打包,依赖的jar包全都在pom.xml中指定,这种方式打包很方便:另一种是依赖了本机jar包(不能通过pom.xml ...