1.将字符串的时间转换为时间戳

方法:

a = "2013-10-10 23:40:00"
#将其转换为时间数组
import time
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
#转换为时间戳:
timeStamp = int(time.mktime(timeArray))
timeStamp == 1381419600

2.格式更改
如a = "2013-10-10 23:40:00",想改为 a = "2013/10/10 23:40:00"
方法:先转换为时间数组,然后转换为其他格式
复制代码代码如下:

timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)

3.时间戳转换为指定格式日期
方法一:利用localtime()转换为时间数组,然后格式化为需要的格式,如:
复制代码代码如下:

timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
otherStyletime == "2013-10-10 23:40:00"

方法二:

import datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
otherStyletime == "2013-10-10 23:40:00"

4.获取当前时间并转换为指定日期格式
方法一:

import time
#获得当前时间时间戳
now = int(time.time())  ->这是时间戳
#转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

方法二:

import datetime
#获得当前时间
now = datetime.datetime.now()  ->这是时间数组格式
#转换为指定的格式:
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")

5.获得三天前的时间的方法

import time
import datetime
#先获得时间数组格式的日期
threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
#转换为时间戳:
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
#转换为其他字符串格式:
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
注:timedelta()的参数有:days,hours,seconds,microseconds

6.给定时间戳,计算该时间的几天前时间

timeStamp = 1381419600
#先转换为datetime
import datetime
import time
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
threeDayAgo = dateArray - datetime.timedelta(days = 3)
#参考5,可以转换为其他的任意格式了

7、用Python计算昨天和明天的日期

>>> import datetime #导入日期时间模块
>>> today = datetime.date.today() #获得今天的日期
>>> print today #输出今天日期
2014-01-04
>>> yesterday = today - datetime.timedelta(days=1) #用今天日期减掉时间差,参数为1天,获得昨天的日期
>>> print yesterday
2014-01-03
>>> tomorrow = today + datetime.timedelta(days=1) #用今天日期加上时间差,参数为1天,获得明天的日期
>>> print tomorrow
2014-01-05
>>>
>>> print "昨天:%s, 今天:%s, 明天:%s" % (yesterday, today, tomorrow) #字符串拼接在一起输出,这3天的日期

昨天:2014-01-03, 今天:2014-01-04, 明天:2014-01-05

8、python里使用time模块来获取当前的时间

#!/usr/bin/python
import time
print (time.strftime("%H:%M:%S"))
## 12 hour format ##
print (time.strftime("%I:%M:%S"))
#:输出
#18:11:30
#6:11:30

9、打印出当前的日期的python程序

!/usr/bin/python
import time
## dd/mm/yyyy格式
print (time.strftime("%d/%m/%Y"))
#输出:
11/03/2014

10、使用datetime模块来获取当前的日期和时间

#!/usr/bin/python
import datetime
i = datetime.datetime.now()
print ("当前的日期和时间是 %s" % i)
print ("ISO格式的日期和时间是 %s" % i.isoformat() )
print ("当前的年份是 %s" %i.year)
print ("当前的月份是 %s" %i.month)
print ("当前的日期是  %s" %i.day)
print ("dd/mm/yyyy 格式是  %s/%s/%s" % (i.day, i.month, i.year) )
print ("当前小时是 %s" %i.hour)
print ("当前分钟是 %s" %i.minute)
print ("当前秒是  %s" %i.second)

11、str:'2017年3月4日',要怎么转换为datetime

# -*- coding: utf-8 -*-
from datetime import datetime

time = "2017年4月2日"
# 将str中的汉字换掉
time = time.replace(r'年', '-').replace(r'月', '-').replace(r'日', '')
print(time) # 输出2017-4-2
print(type(time)) # <type 'str'>
restime = datetime.strptime(time, '%Y-%m-%d')
print(restime) # 输出结果:2017-04-02 00:00:00
print(type(restime))# <type 'datetime.datetime'>

12、任意时间字符串转换时间对象

import time
from dateutil import parser

time_string = time.ctime() # 'Thu Dec 22 10:35:25 2016',这里可以是任意的时间格式
datetime_struct = parser.parse(time_string)
print type(datetime_struct) # <type 'datetime.datetime'>
print datetime_struct.strftime('%Y-%m-%d %H:%M:%S') # 2016-12-22 13:58:59

python日期格式化操作的更多相关文章

  1. Python 日期和时间戳的转换

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

  2. Python日期时间函数处理

    所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...

  3. Python 日期和时间(转)

    Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...

  4. (转)Python 日期和时间

    转自http://www.runoob.com/python/python-date-time.html Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见 ...

  5. 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码

    本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...

  6. Python 日期时间处理模块学习笔记

    来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...

  7. Python 日期和时间 —— datetime

    Python 日期和时间 —— datetime Python提供了多个内置模块用于操作日期时间,如calendar,time,datetime.calendar用于处理日历相关 :time提供的接口 ...

  8. 【310】◀▶ Python 日期和时间

    参考: python 时间日期计算 Python 日期和时间(菜鸟教程) 8.1. datetime — Basic date and time types python中datetime模块中dat ...

  9. 练习十六:Python日期格式应用(datetime)

    练习:关于python日期格式应用练习.用python方法如何输出指定格式形式的日期 这里用到datetime模块,datetime模块重新封装了time模块,提供了更多接口,提供的类包括:date, ...

随机推荐

  1. mybatis源码之StatementHandler

    /** * @author Clinton Begin */ public interface StatementHandler { Statement prepare(Connection conn ...

  2. LeetCode(37)-Minimum Depth of Binary Tree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  3. 使用vs2010 opencv2.4.4编译release版本程序

    大体上要注意一下几点内容: 1.vc++目录的选择上,库目录选择为opencv目录中的staticlib目录 2.在链接->输入->附加依赖库,中添加,相应的staticlib库目录中的所 ...

  4. jquery-取消冒泡

    1.通过返回false来取消默认的行为并阻止事件起泡. jQuery 代码: $("form").bind( "submit", function() { re ...

  5. Xcode9无线调试教程

    Xcode9最让人眼前一亮的莫过于同一局域网下支持无线部署调试. 从Xcode9支持无线部署调试来看,千呼万唤的iPhone8支持无线充电几乎是必然.下面简单梳理下无线调试的步骤: 1. 第一次部署调 ...

  6. two sum II

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  7. left join 原理分析

    left join 原理分析 [转贴 2006-11-15 16:19:50]     字号:大 中 小 案例分析 user表:  id   | name --------- 1   | libk   ...

  8. Markdown编辑技巧

    [前言] 保存Markdown的编辑技巧,写博客随笔,可以用下. [正文] 1.空格  //半角空格(英文)  //全角空格(中文)

  9. JavaScript头像上传器的实现

    最近做这方面的东西,刚开始准备用一个开源项目:https://github.com/yueyoum/django-upload-avatar 后来发现这个开源组件的原设计者的定制化选项设计略显复杂,发 ...

  10. 爬虫初探--PHP

    我有收藏的cms网站,偶尔会下载一些资源,老司机都懂的:-D.然后有一次好几天没上,堆了好些没弄,心想:cao,这好麻烦啊,能不能写个脚本自动帮我搞?然后忽然就想到,这是不是就是所谓的爬虫呢?心中一阵 ...