广告
返回顶部
首页 > 资讯 > 后端开发 > Python >2-datetime 模块
  • 368
分享到

2-datetime 模块

模块datetime 2023-01-31 08:01:01 368人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

时间转字符串 在我们的使用中,我们常常需要将时间转换为字符串,用来作为文件的名字或者用于加密字符的输出等等。例子: from datetime import datetime datetime.strftime(datetime.no

时间转字符串

在我们的使用中,我们常常需要将时间转换为字符串,用来作为文件的名字或者用于加密字符的输出等等。例子:

from datetime import datetime 
datetime.strftime(datetime.now(),"%Y-%m-%d %H:%M:%S")

记忆方式也很简单,str from time

字符转时间

有时候我们需要将一个字符给转换为时间对象

from datetime import datetime 
>>> datetime.strptime('2018-09-09',"%Y-%m-%d")
datetime.datetime(2018, 9, 9, 0, 0)

时间戳的转换

import time 
from datetime import datetime 
stamp = time.time()
datetime.fromtimestamp(stamp)

timedelta

import datetime
print('microseconds:', datetime.timedelta(microseconds=1))
print('milliseconds:', datetime.timedelta(milliseconds=1))
print('seconds :', datetime.timedelta(seconds=1))
print('minutes :', datetime.timedelta(minutes=1))
print('hours :', datetime.timedelta(hours=1))
print('days :', datetime.timedelta(days=1))
print('weeks :', datetime.timedelta(weeks=1))

加 就是 延后几秒; 减 就是提前几秒

转换格式

Symbol Meaning Example
%a Abbreviated weekday name 'Wed'
%A Full weekday name 'Wednesday'
%w Weekday number: 0 (Sunday) through 6 (Saturday) '3'
%d Day of the month (zero padded) '13'
%b Abbreviated month name 'Jan'
%B Full month name 'January'
%m Month of the year '01'
%y Year without century '18'
%Y Year with century '2018'
%H Hour from 24-hour clock '17'
%I Hour from 12-hour clock '05'
%p AM/PM 'PM'
%M Minutes '00'
%S Seconds '00'
%f Microseconds '000000'
%z UTC offset for time zone–aware objects '-0500'
%Z Time zone name 'EST'
%j Day of the year '013'
%W Week of the year '02'
%c Date and time representation for the current locale 'Wed Jan 13 17:00:00 2016'
%x Date representation for the current locale '01/13/16'
%X Time representation for the current locale '17:00:00'
%% A literal % character '%'

tips

工作中经常需要用到美国时间,做一个记录。 utc晚了8个小时,所以要减去即是美国时间

datetime.strftime(datetime.utcnow()-timedelta(hours=8),'%Y-%m-%d %H:%M:%S')

《The python3 Standard Library By Example》

--结束END--

本文标题: 2-datetime 模块

本文链接: https://www.lsjlt.com/news/193255.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 2-datetime 模块
    时间转字符串 在我们的使用中,我们常常需要将时间转换为字符串,用来作为文件的名字或者用于加密字符的输出等等。例子: from datetime import datetime datetime.strftime(datetime.no...
    99+
    2023-01-31
    模块 datetime
  • datetime模块
    datetime模块一般用于时间的加减。 # datetime模块可以看成是时间加减的模块 import datetime # 返回当前时间 print(datetime.datetime.now()) 2019-03-07 16:22...
    99+
    2023-01-31
    模块 datetime
  • Python3: datetime模块
    datetime模块定义了以下几个类: datetime.date: 表示日期的类,常用的属性有year, month, day; datetime.time: 表示时间的类,常用的属性有hour, minute, second, micr...
    99+
    2023-01-31
    模块 datetime
  • python datetime模块
    看这模块之前先熟悉下time模块:time模块地址datetime.time():生成一个时间对象。这个时间可以由我们来设置,默认都是0(这个类只针对时间) #coding:utf-8 import datetime p...
    99+
    2023-01-31
    模块 python datetime
  • Python时间模块之datetime模块
    目录 简介 函数介绍及运用 date:日期类 1.获取当前时间  2.日期对象的属性 3.date类中时间和时间戳的转换: 4.修改日期使用replace方法  time:时间类  time类操作 datetime:日期时间类 timede...
    99+
    2023-09-12
    python datetime python 日期时间
  • python datetime模块详解
    目录1. 获取当前时间2. 时间间隔 timedelta3. datetime 转 字符串 strftime()4. 字符串 转 datetime对象 datetime.strpti...
    99+
    2022-11-12
  • python time与datetime模块
    在python中,与时间处理相关的模块有:time、datetime以及calendar。学会计算时间,对程序的调优非常重要,可以在程序中狂打时间戳,来具体判断程序中哪一块耗时最多,从而找到程序调优的重心处。time模块:在Python中,...
    99+
    2023-01-30
    模块 python time
  • 21 Python的datetime模块
    概述         在上一节,我们介绍了Python的time模块,包括:time模块中一些常用的属性和函数。在这一节,我们将介绍Python的datetime模块。datetime模块属于Python的内置模块,提供了一种方便的方法来处...
    99+
    2023-09-29
    python datetime模块
  • 模块使用:time、datetime、c
    ## time:时间 时间戳(timestamp):time.time() 延迟线程的运行:time.sleep(secs) (指定时间戳下的)当前时区时间:time.localtime([secs]) (指定时间戳下的)格林威治时间:...
    99+
    2023-01-31
    模块 time datetime
  • python常用的时间模块之datetime模块
    今天小编给大家分享的是python常用的时间模块之datetime模块,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。一、基本类型1、date类datetime.date(2023,5...
    99+
    2023-08-03
  • 详解python时间模块中的datetime模块
    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime。time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致。相比于time模块,dat...
    99+
    2022-06-04
    模块 详解 时间
  • python的datetime模块处理时
    python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的类。 1.创建一个新的datetime...
    99+
    2023-01-30
    模块 python datetime
  • 22. Python 模块2
    1. logging 模块日志是我们排查问题的关键利器,写好日志记录,当我们发生问题时,可以快速定位代码范围进行修改。Python有给开发者们提供好的日志模块,下面介绍一下logging模块:首先,我们先来看一个例子:import logg...
    99+
    2023-01-31
    模块 Python
  • Python中的time模块与datetime模块用法总结
    time模块 time模块是包含各方面对时间操作的函数. 尽管这些常常有效但不是所有方法在任意平台中有效. time用struct_time表示时间 import time # time.struct...
    99+
    2022-06-04
    模块 Python time
  • Python中time模块和datetime模块的用法示例
    time模块方法: time.time():获取当前时间的时间戳 time.localtime():接受一个时间戳,并把它转化为一个当前时间的元组。不给参数的话就会默认将time.time()作为参数传入 ...
    99+
    2022-06-04
    模块 示例 Python
  • Python的时间模块datetime详解
    datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime.da...
    99+
    2022-06-04
    详解 模块 时间
  • Python中datetime模块参考手册
    前言 Python提供了多个内置模块用于操作日期时间,像 calendar,time,datetime。time模块提供的接口与C标准库 time.h 基本一致。相比于 time 模块,datetime模块...
    99+
    2022-06-04
    模块 参考手册 Python
  • Pyhon 中如何使用DateTime模块
    这篇文章将为大家详细讲解有关Pyhon 中如何使用DateTime模块,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Pyhon DateTime模块的常用例子<!--[if ...
    99+
    2023-06-17
  • Python之datetime模块怎么使用
    这篇文章主要讲解了“Python之datetime模块怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python之datetime模块怎么使用”吧!1、日期时间对象日期时间对象是指具...
    99+
    2023-07-02
  • Python标准库datetime之datetime模块用法分析详解
    目录1、日期时间对象2、创建日期时间对象2.1、通过datetime.datetime.utcnow()创建2.2、通过datetime.datetime.today()函数创建2....
    99+
    2022-11-11
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作