1. 将当前时间转成字符串 strftime 方法,并输出

import datetime
# 获取当前时间 datetime.datetime.now()
print(datetime.datetime.now()) # 输出时间格式数据:2019-11-28 20:39:25.485711
now_time=str(datetime.datetime.now().strftime('%Y%m%d%H%M%S')) #将时间转化成字符串 print(now_time)

2. 将字符串形式的时间转成时间格式 time.strptime 方法,并获取时间戳 time.mktime 方法

import time
time_str = '2019-09-02 00:00:01.002' #时间字符串
time_struct = time.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f") #得到时间格式数据,%f表示的是微秒
print(time_struct)
time_stamp = int(time.mktime(time_struct)) #将时间格式数据转化成时间戳
print(time_stamp)

3. 将时间格式的数据转成字符串

#转格式,time_struct来自上面
time_ = time.strftime("%a %b %d %H:%M:%S %Y", time_struct)
print(time_)

4. 将字符串形式的时间转成时间格式 datetime.datetime.strptime 方法,时间格式转字符串 datetime.datetime.strftime 方法

time_str = '2019-09-02 00:00:01.002'        #时间字符串
time_2 = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f") # 将格式字符串转换为时间格式
time_3 = datetime.datetime.strftime(time_2, "%Y-%m-%d %H:%M:%S") #str类型

5. 计算当前时间戳

import time
time_stamp = int(time.time()) #计算当前的本地时间戳
print(time_stamp)

6. 由时间戳转时间格式数据

#time_stamp 来自上面
time_format = datetime.datetime.fromtimestamp(time_stamp) #直接由时间戳转时间格式
print('----------------',time_format, '--------------------')

7. 获取单独的时间、日期

#获取当前时间日期
now_datetime = datetime.datetime.now()
print ("当前的日期和时间是 %s" % now_datetime)
print ("当前的年份是 %s" %now_datetime.year)
print ("当前的月份是 %s" %now_datetime.month)
print ("当前的日期是 %s" %now_datetime.day)
print ("当前小时是 %s" %now_datetime.hour)
print ("当前分钟是 %s" %now_datetime.minute)
print ("当前秒是 %s" %now_datetime.second)
# 星期
print ("当前星期是 %s" %datetime.datetime.strptime('2019-11-25', "%Y-%m-%d").weekday()) #周一为0,周二为1,。。。
print ("当前星期是 %s" %datetime.datetime.now().strftime('%A'))

python中时间日期格式化符号:

    %y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% 为%号本身

参考:

https://www.runoob.com/python/python-date-time.html

https://www.cnblogs.com/pyxiaomangshe/p/7918850.html

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

  1. Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块

    Python第十五天  datetime模块 time模块   thread模块  threading模块  Queue队列模块  multiprocessing模块  paramiko模块  fab ...

  2. Python之路(第十六篇)xml模块、datetime模块

    一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, xml比较早,早期许多软件都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...

  3. (转)python time模块和datetime模块详解

    python time模块和datetime模块详解 原文:http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要 ...

  4. python time模块 sys模块 collections模块 random模块 os模块 序列化 datetime模块

    一 collections模块 collections模块在内置的数据类型,比如:int.str.list.dict等基础之上额外提供了几种数据类型. 参考博客 http://www.pythoner ...

  5. python中time模块和datetime模块

    time模块和datetime模块 时间分为三种模式(time 模块) 时间戳   (time.time()) 格式化字符串 (time.strftime(%Y-%m-%d %H:%M:%S %p)) ...

  6. Time模块和datetime模块

    Time模块和datetime模块 一. 调用 import time       #调用time模块 二.使用方法 1.time.time 拿到时间戳.以Linux诞生年份1970年开始计算到程序执 ...

  7. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  8. python time模块和datetime模块

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  9. python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)

    logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...

  10. python五十四课——datetime模块

    3.datetime模块:理解:datetime可以认为是time模块的补充/扩展datetime模块中有一些常用类:datetime类:记录了日期和时间数据信息date类:记录了日期数据信息time ...

随机推荐

  1. python初级(302) 2 easygui简单使用

    一.复习之前的两个练习,巩固计数循环和条件循环 1.系统生成一个随机数1到5,然后让用户的猜测,若猜对了,提示恭喜你,猜对了,否则提示,对不起,你猜错了(提示,1到5的随机数为:secret = ra ...

  2. Java 解析XML数据

    实例一:获取指定两个标签之间的数据 XML数据格式: <?xml version="1.0" encoding="utf-8"?> <soap ...

  3. Python - Django - SweetAlert 插件的使用

    SweetAlert Github:https://github.com/lipis/bootstrap-sweetalert 下载完后放入 /static/ 目录下 sweetalert.html: ...

  4. 【Ubuntu升级python3.5到python3.6】dpkg-deb: error: subprocess paste was killed by signal (Broken pipe) Errors were encountered while processing: E: Sub-process /usr/bin/dpkg returned an error code (1) 问题解决

    Ubuntu16.04上将系统自带的python3.5升级到3.6 安装aioredis时提示Python版本需>=3.5.3,所以进行升级命令如下: $ sudo add-apt-reposi ...

  5. vs中调试程序查看变量在内存中的内容的方法

    vs中调试程序 查看变量在内存中的内容的方法 https://blog.csdn.net/guojg1988/article/details/42922149 原文链接:http://www.sows ...

  6. RDB和AOF持久化

    RDB和AOF持久化 https://www.cnblogs.com/Tu9oh0st/p/11229317.html Redis提供了不同的持久化选项: RDB持久化以指定的时间间隔保存那个时间点的 ...

  7. Spring MVC原理图及其重要组件

    一.Spring MVC原理图: ps: springmvc的运行流程为图中数字序号 二.springmvc的重要组件: 1)前端控制器:DispatchServlet(不需要程序员开发) 接收请求, ...

  8. 【转帖】国产PCIe SSD主控芯片获得中国芯大奖 3500MB/s读取

    国产PCIe SSD主控芯片获得中国芯大奖 3500MB/s读取 https://www.cnbeta.com/articles/tech/906033.htm 国产主控 在日前的2019“中国芯”集 ...

  9. 线性DP详解

    顾名思义,线性DP就是在一条线上进行DP,这里举一些典型的例子. LIS问题(最长上升子序列问题) 题目 给定一个长度为N的序列A,求最长的数值单调递增的子序列的长度. 上升子序列B可表示为B={Ak ...

  10. Django RuntimeError: Model class app_anme.models.Ad doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.报错

    报错内容 RuntimeError: Model class app_anme.models.Ad doesn't declare an explicit app_label and isn't in ...