startDate = "2018-10-01"
endDate = "2018-10-31"

###字符转化为日期
startTime = datetime.datetime.strptime(startDate, '%Y-%m-%d').time()
endTime = datetime.datetime.strptime(endDate, '%Y-%m-%d').time()

now = datetime.datetime.now()
print(now)

###日期转化为字符串
print("--1---:" + datetime.datetime.strftime(startTime, "%Y-%m-%d"))
print("--2---:" + datetime.datetime.strftime(endTime, "%Y-%m-%d"))

#String to Date(datetime)
def stringToDate(string):
#example '2013-07-22 09:44:15+00:00'
dt = datetime.strptime(string, "%Y-%m-%d %H:%M:%S+00:00")
#print dt
return dt
#Date(datetime) to String
def dateToString(date):
ds = date.strftime('%Y-%m-%d %H:%M:%S')
return ds
#return n hours after datetime
def getAfterDate(n):
dnow = datetime.datetime.now()
dafter = dnow + datetime.timedelta(hours=n)
#dafter.ctime()
return dafter
---------------------

1⃣️str -> date

import datetime

detester = ‘2017-01-01'
date = datetime.datetime.strptime(detester,’%Y-%m-%d')1234

2⃣️date -> str

import datetime

date = datetime.now()

detester = date.strftime(‘%Y-%m-%d')
---------------------
import time

# datatime 是一个时间对象
print '##'*5 + 'datetime 是一个时间对象类型' + '##'*5
from datetime import datetime
now_time = datetime.now()
print  now_time
print type(now_time)
 
#strftime() 是一个字符串类型,通过strftime 可以把时间格式转换成字符串类型
print '##'*5 + '通过strftime 可以把时间格式转换成字符串类型' + '##'*5
_time = now_time.strftime('%Y-%m-%d')
print type(_time)
 
 
#把字符串类型在转换成时间对象类型
#通过 datetime.strptime() 把时间字符串类型转换成时间对象
print '##'*5 + '通过 datetime.strptime() 把时间字符串类型转换成时间对象' + '##'*5
_dtime = datetime.strptime(_time,'%Y-%m-%d')
print type(_dtime)
 
 
 
#s时间戳,转换成时间对象
#时间戳:时间戳就是指格林或北京时间 1970年1月1日08时0分0秒起至今的总秒数
print '##'*5 + '函数 datetime.fromtimestamp 时间戳转换成时间对象' + '##'*5
_a = time.time()
print _a
_n_time = datetime.fromtimestamp(_a)
print _n_time
print type(_n_time)
 
>>>
##########datetime 是一个时间对象类型##########
2017-11-09 10:13:25.830000
<type 'datetime.datetime'>
##########通过strftime 可以把时间格式转换成字符串类型##########
<type 'str'>
##########通过 datetime.strptime() 把时间字符串类型转换成时间对象##########
<type 'datetime.datetime'>
##########函数 datetime.fromtimestamp 把时间戳转换成时间对象##########
1510193605.83
2017-11-09 10:13:25.834000
<type 'datetime.datetime'>

python 爬虫数据处理字符串时间转换格式方法的更多相关文章

  1. Python爬虫—requests库get和post方法使用

    目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...

  2. Python关于去除字符串中空格的方法

    Python关于去除字符串中空格的方法 在编写程序时我们经常会遇到需要将字符串中的空格去掉的情况,通常我们可以使用下面几种解决方法: 1.strip()方法:该方法只能把字符串头和尾的空格去掉,但是不 ...

  3. Python中常见字符串去除空格的方法总结

    Python中常见字符串去除空格的方法总结 1:strip()方法,去除字符串开头或者结尾的空格>>> a = " a b c ">>> a.s ...

  4. Python 3 格式化字符串的几种方法!

    Python 3 格式化字符串的几种方法! %s和%d,%s是用来给字符串占位置,%d是给数字占位置,简单解释下: a = 'this is %s %s' % ('an','apple') 程序输出的 ...

  5. sql 时间转换格式 convert(varchar(10),字段名,转换格式)

    convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121) CONVERT为日期转换函数,一般就是在时间类型(datetime ...

  6. JS 时间转换函数 字符串时间转换毫秒(互转)

    字符串转化为日期 let util = function(){ Date.prototype.Format = function(fmt) { var o = { "M+" : t ...

  7. Python十六进制与字符串的转换

    电脑上装了Python2.7和3.3两个版本,平时运行程序包括在Eclipse里面调试都会使用2.7,但是由于某些原因在cmd命令行中输入python得到的解释器则是3.3, 一直没对此做处理,因为这 ...

  8. 转 sql 时间转换格式 convert(varchar(10),字段名,转换格式)

    convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121)CONVERT为日期转换函数,一般就是在时间类型(datetime, ...

  9. (转)sql 时间转换格式 convert(varchar(10),字段名,转换格式)

    convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121) CONVERT为日期转换函数,一般就是在时间类型(datetime ...

随机推荐

  1. JComBox的用法

    JComboBox的使用: 类层次结构图:    java.lang.Object     --java.awt.Component      --java.awt.Container       - ...

  2. Wannafly挑战赛27-A/B

    链接:https://www.nowcoder.com/acm/contest/215/A来源:牛客网 题目描述 “White shores, and beyond. A far green coun ...

  3. PHP手册-函数参考-加密扩展

    一.Crack.CSPRNG.Hash.Mcrypt.Mhash.OpenSSL.密码散列算法的对比   Crack CSPRNG Hash Mcrypt Mhash OpenSSL 密码散列算法 简 ...

  4. MySQL5.6复制技术(4)-MySQL主从复制过滤参数

     复制的过滤主要有2种方式: 在主服务器在把事件从进二制日志中过滤掉,相关的参数是:binlog_do_db和binlog_ignore_db. 在从服务器上把事件从中继日志中过滤掉,相关的参数是re ...

  5. 在Eclipse上安装Activiti插件

    然后点击: Help ---> Install new SoftWare 然后在点击Add添加新的: 我们在Name上 写上 Activiti BPMN 2.0 designer Locatio ...

  6. Liunx 更新环境时用到的命令

    1.进入项目文件所在位置:cd/home/zhaorg/shelly62/apache-tomcat-7.0.5.2/bin 2.关闭服务:./shuntdown.sh或者 sh.shuntdown. ...

  7. Python类的构成元素

    类的构成元素 公共属性:实例化时无需__init__方法绑定到对象,就可以直接使用:普通属性:实例化时 需要__ini__方法绑定到对象之后,才可以直接使用:私有属性:__sex 双下滑杠开头,需要在 ...

  8. MSOCache office问题

    \MSOCache 链接:https://pan.baidu.com/s/1dVvjYnD0D6DG6oFK_Mww2w密码:qrvp undefied

  9. Linux第六周作业

    一 实验过程 1 先进入LinuxKernel环境下,更新menu代码到最新版,用到的命令为rm menu -rf //强制删除当前menu,git clone http://git.shiyanlo ...

  10. ubuntu16.10安装搜狗输入法

    一.搜狗输入法安装 1.首先到搜狗输入法官网下载搜狗输入法,下载的是个deb文件. 搜狗输入法Linux版下载地址:http://pinyin.sogou.com/linux/?r=pinyin 2. ...