python日期时间

Python日期时间 (Python datetime)

In this post, we will study about how to use the python datetime module. With datetime module, we can get information about datetime and format dates as well.

在本文中,我们将研究如何使用python datetime 模块 。 使用datetime模块,我们还可以获得有关日期时间和格式日期的信息。

使用日期时间模块 (Using datetime module)

Let’s start working on python datetime module straightaway.

让我们直接开始使用python datetime模块。

python datetime now() (Python datetime now())

It is easy to print current time using datetime module. Let’s see the code snippet on how this can be done:

使用datetime模块可以轻松打印当前时间。 让我们看一下如何做到这一点的代码片段:

import time
import datetimeprint("Time in seconds since the epoch: %s", time.time())
print("Current date and time: " , datetime.datetime.now())

Let’s see the output for this program:

让我们看一下该程序的输出:

At first, accessing a property inside datetime module which has the same name looks odd. You can call the now() function to print the date information, as shown above. Notice that datetime now() returns the date-time information in a user-friendly way and looks much better than we get from time module.

首先,访问具有相同名称的datetime模块内部的属性看起来很奇怪。 您可以调用now()函数以打印日期信息,如上所示。 注意datetime now()以一种用户友好的方式返回日期时间信息,并且看起来比我们从time模块获得的要好得多。

Python日期时间格式 (Python datetime format)

Most of the times, single date format do not fulfill our needs in the application. We can format our code using the code snippet we show next:

在大多数情况下,单日期格式不能满足我们在应用程序中的需求。 我们可以使用下面显示的代码片段来格式化代码:

import datetimeprint("Formatted date : " , datetime.datetime.now().strftime("%y-%m-%d-%H-%M"))

Let’s see the output for this program:

Note that here, the smaller m reflects the Month and capital M shows the minute part for time. This often leads to confusions and is a point to remember.

让我们看一下该程序的输出:

请注意,此处较小的m表示月份,而大写M表示时间的分钟部分。 这通常会导致混乱,并且是要记住的一点。

Python日期时间示例 (Python datetime example)

We can do a lot more with datetime format like getting the present year, calculating weekday of the day, day of the year, etc. All of this is possible with single calls and we just need to use the strftime() function appropriately. Let’s see the code snippet on how this can be done:

我们可以使用日期时间格式做更多的事情,例如获取当前年份,计算一天中的工作日,一年中的某天等等。所有这些都可以通过一次调用来实现,而我们只需要适当地使用strftime()函数即可。 让我们看一下如何做到这一点的代码片段:

import datetimeprint("Present year: ", datetime.date.today().strftime("%Y"))
print("Month of year: ", datetime.date.today().strftime("%B"))
print("Week number of the year: ", datetime.date.today().strftime("%W"))
print("Weekday of the week: ", datetime.date.today().strftime("%w"))
print("Day of year: ", datetime.date.today().strftime("%j"))
print("Day of the month : ", datetime.date.today().strftime("%d"))
print("Day of week: ", datetime.date.today().strftime("%A"))

Let’s see the output for this program:

Now that was a lot of information which can be used.

让我们看一下该程序的输出:

现在,有很多信息可以使用。

获取工作日的日期 (Getting Weekday for a Date)

It is also possible to get a weekday for a given date. This is sometimes needed in calendar-based applications or just for curiosity!

也可以在给定日期获得工作日。 在基于日历的应用程序中有时出于好奇心而需要这样做!

Let’s see the code snippet on how this can be done:

让我们看一下如何做到这一点的代码片段:

import datetimebirthday = datetime.date(1994,5, 20)  #year, month, day
print(birthday.strftime("%A"))

Let’s see the output for this program:

让我们看一下该程序的输出:

Python字符串到日期时间 (Python String to datetime)

We can use strptime() function to get datetime object from String. Let’s see a short example for this.

我们可以使用strptime()函数从String获取datetime对象。 让我们看一个简短的例子。

import datetimetime_now = datetime.datetime.strptime("1/1/2018", "%m/%d/%Y")print(time_now)
print(type(time_now))

The output of the above program is shown below.

上面程序的输出如下所示。

2018-01-01 00:00:00
<class 'datetime.datetime'>

结论 (Conclusion)

In this post, we saw how we can use the Python datetime module to provide date information and put it to use in our application. It’s a very useful module to provide date and time information in a user-friendly way. Also, there are many functions for formatting and getting specific information. So this module comes very handily in python application dealing with dates.

在本文中,我们看到了如何使用Python datetime模块提供日期信息并将其用于我们的应用程序。 这是一个非常有用的模块,以用户友好的方式提供日期和时间信息。 此外,还有许多用于格式化和获取特定信息的功能。 因此,此模块在处理日期的python应用程序中非常方便。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/18745/python-datetime

python日期时间

python日期时间_Python日期时间相关推荐

  1. python显示时间_python日期时间处理总结

    用了一段时间的django开发web项目,也是第一次用python来开发项目,所有有许多相关的知识点需要总结,本文所介绍的python的日期时间处理就是最近用到的,希望对各位有所帮助,也同时作为自己的 ...

  2. python pandas 日期格式_python+pandas+时间、日期以及时间序列处理方法

    先简单的了解下日期和时间数据类型及工具 python标准库包含于日期(date)和时间(time)数据的数据类型,datetime.time以及calendar模块会被经常用到. datetime以毫 ...

  3. python日期时间_python日期和时间的操作方法汇总

    日期和时间可以说是一种独特的数据类型,既不同于数字,也不同于字符串,而且有自己独特的运算规则.在不同的编程语言中,都会将日期和时间与常规的数据类型独立开来,单独进行操作.在python的内置模块中,时 ...

  4. python日期和时间_Python日期和时间

    datetime是Python处理日期和时间的标准库. 获取当前日期和时间 我们先看如何获取当前日期和时间: 1 2 3 4 5 6>>>from datetime import d ...

  5. python 字符串比较时间_Python日期字符串比较

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 需要用python的脚本来快速检测一个文件内的二个时间日期字符串的大小,其实实现很简单,首先一些 ...

  6. python比较日期大小_Python日期的处理——datetime模块

    本文作者:胡   婧 文字编辑:杨慧琳 技术总编:张学人 好消息!!!爬虫俱乐部将于2019年7月5日至7月8日在武汉举行首期Python编程技术定制培训.本次培训采用理论与案例相结合的方式,旨在帮助 ...

  7. python 计算时间_python的时间使用和时间计算

    python存在两个时间类time/dateTime 区别: time:在python文档中,time是归类在常规操作系统服务中,它提供的功能更加接近于操作系统层面.其所能表述的日期范围被限定在197 ...

  8. python中如何比较日期大小_python 日期大小比较

    输入的日期跟当前日期比较大小 日期在python中 存在time,datetime,string三种形式转化如下 #str转time time.strptime('2018-09-28'," ...

  9. python时间戳转换日期格式_Python 日期和时间戳的转换

    Python 日期和时间戳的转换 1. Python中处理时间的模块 Python中处理时间的模块有time.datetime和calendar. 在Python中表示时间的方式: 时间戳:10位整数 ...

最新文章

  1. eureka心跳_Eureka工作原理及心跳机制
  2. POJ 3159[差分约束]
  3. linux apache两种工作模式详解
  4. springboot+mybatis实现数据分页(三种方式)
  5. 朱峰谈概念设计(二):我们设计什么
  6. Adobe放出P图新研究:就算丢了半个头,也能逼真复原
  7. 菜鸟学做——三层交换综合模拟实验【1】
  8. DSO的记录模式Record Mode字段测试
  9. C++ queue队列如何遍历
  10. 多个容器一起打包_Docker从入门到掉坑(三):容器太多,操作好麻烦
  11. 嵌入式单片机高级篇(一)Stm32F103电容触摸按键
  12. html640设计稿,为什么写移动端的ui给的640设计稿的宽度,在写html的时候要除以2才正好。...
  13. 设计模式-模板方法(TemplateMethod)模式
  14. 为什么使用 Git-flow 工作流
  15. LA 5713 秦始皇修路
  16. L2-3 清点代码库 (25 分)
  17. 【教育小程序案例】线下培训机构辅导教育
  18. Element UI Table表格样式调整
  19. 咸鱼半学期总结+老年人康复训练
  20. 液体点滴速度监控报警装置(51单片机)

热门文章

  1. 闲来无事写写-Huffman树的生成过程
  2. 禁止minigui 3.0的屏幕保护
  3. 温故而知新:new与override的差异以及virtual方法与abstract方法的区别
  4. 一点总结,手机应用开发前景
  5. [转载] python中日期和时间格式化输出的方法
  6. UDF函数,hive调用java包简单方法
  7. nginx.conf添加lua.conf配置
  8. Python Day29 网络协议
  9. Georgia Tech - machine learning 学习笔记一
  10. Java设计模式(10)代理模式(Proxy模式)