strptime 字符串到日期时间对象的转换

卢卡斯

我有一个 Python2.7 脚本,其中的变量date= "21/05/2019"可以正常工作。我需要date变量是datetime.now - 60 days.

但是输出必须像 time.struct_time(tm_year=2019, tm_mon=5, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=148, tm_isdst=-1)

datetime.datetime.now()strptime将无法正常工作。

import datetime, time
from datetime import timedelta

N = 60
#pre_create_date = datetime.datetime.today() - datetime.timedelta(days=N)
pre_create_date = "28/04/2019"

# Sort out date format
pre_create_date = time.strptime(pre_create_date, "%d/%m/%Y")

print pre_create_date

我希望能够使用datetime.datetime.today() - datetime.timedelta(days=N)但输出是日期时间对象。

编辑:

看来我还没有很好地解释自己。对于那个很抱歉。

脚本是:

import datetime, time
from datetime import timedelta

N = 60
#pre_create_date = datetime.datetime.today() - datetime.timedelta(days=N)
pre_create_date = "28/04/2019"

# Sort out date format
pre_create_date = time.strptime(pre_create_date, "%d/%m/%Y")

print pre_create_date

输出:

time.struct_time(tm_year=2019, tm_mon=4, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=118, tm_isdst=-1)

脚本使用datetime.now - datetime.timedelta

import datetime, time
from datetime import timedelta

N = 60
pre_create_date = datetime.datetime.today() - datetime.timedelta(days=N)

print pre_create_date

输出:

2019-03-31 21:09:25.183459

预期输出:

time.struct_time(tm_year=2019, tm_mon=3, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=118, tm_isdst=-1)
血管瘤

尝试:-

import time
import datetime


print(time.strftime("%Y-%m-%d %H:%M:%S %w/%j"))

pre_create_date = (datetime.datetime.today() - datetime.timedelta(60)).strftime("%Y-%m-%d %H:%M:%S %w/%j")

print(pre_create_date)

输出:-

2019-05-30 12:59:27 4/150
2019-03-31 12:59:27 0/090

在职的:-

上面的代码首先打印出当前的时间戳(格式是你要求的)。然后输出从当前日期(今天 - 60)提前 60 天的时间戳。

只是用来strftime()从当前日期获取我们想要的属性。

代码解释strftime():-

%Y     Year with century as a decimal number.
%m     Month as a zero-padded decimal number.
%d     Day of the month as a zero-padded decimal.

%H     Hour (24-hour clock) as a zero-padded decimal number.
%M     Minute as a zero-padded decimal number.
%S     Second as a zero-padded decimal number.

%w     Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0.
%j     Day of the year as a zero-padded decimal number.

注意:-通过strftime()输出传递日期后将是数据类型string如果您希望输出是日期时间对象,请使用:-

datetimeobj_1 = datetime.datetime.strptime(pre_create_date, "%Y-%m-%d %H:%M:%S %w/%j")
print(type(datetimeobj_1))

输出:-

<class 'datetime.datetime'>

如果您希望输出为 'time.struct_time' 对象,请使用:-

datetimeobj_1 = time.strptime(pre_create_date, "%Y-%m-%d %H:%M:%S %w/%j")
print(type(datetimeobj_1))

输出:-

<class 'time.struct_time'>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用日期时间库和strptime方法将“ 12/01/2020”转换为日期时间对象?

R中的strptime错误:输入字符串太长

Python使用strptime解析日期

`DateTime.strptime`返回工作日/时间字符串的无效日期

替代strptime来比较日期?

如何从strptime()中仅提取时间?

如何在strptime中使用俄语日期字符串

Python中的日期时间strptime

一次转换不同的日期时间格式(strptime)

DateTime.strptime无法解析日期字符串

在Python 3.6中strptime()不会将字符串转换为datetime对象

如何使用strptime转换微秒部分的7位时间戳字符串?

将字符串转换为日期python strptime错误

datetime.datetime.strptime将字符串转换为datetime时发生未转换日期的错误要求:2

使用strptime将字符串转换为datetime对象的问题

在没有strptime的情况下将字符串转换为datetime对象

在strptime中将字符视为文字

如何使用strptime提取本地日期?

使用strptime在R中转换时区

日期时间到字符串的转换

将datetime转换为strptime

带时间戳和AM / PM的strptime

时间字符串到日期对象的转换js angularJS

使用strptime将字符串转换为具有时区偏移的python日期类型对象

无法使用 strptime 解析日期时间

使用 datetime.strptime 将字符串转换为日期时 Python 中的值错误

如何在python中将元组转换为字符串并将其用于strptime?

Pandas strptime 返回一个字符串而不是 DateTime 对象

strptime 将字符串时间转换为时间