我正在尝试将strptime()日期/时间字符串解析为其组件值.作为测试,我尝试解析固定的日期时间字符串并使用以下代码打印结果值:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void)
{
struct tm tm;
memset(&tm, 0, sizeof(struct tm));
strptime("2001/11/12 18:31:01", "%Y/%m/%d %H:%M:%S", &tm);
printf("year: %d; month: %d; day: %d;\n",
tm.tm_year, tm.tm_mon, tm.tm_mday);
printf("hour: %d; minute: %d; second: %d\n",
tm.tm_hour, tm.tm_min, tm.tm_sec);
exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
year: 101; month: 10; day: 12;
hour: 18; minute: 31; second: 1
Run Code Online (Sandbox Code Playgroud)
其他值看起来不错,但年和月与输入(2001/11/12 18:31:01)不匹配.这是为什么?
我有一些代码在同一个python版本的Ubuntu和MacOS编译器上莫名其妙地工作.欢迎任何解释或解决方法.
首先,在Ubuntu Python上进行设置.机器以UTC格式运行.在时间字符串中使用PST失败,但将PST更改为UTC它工作正常.
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> date_str = 'Thu Jan 1 00:32:36 PST 2015'
>>> now = datetime.strptime(date_str, '%a %b %d %H:%M:%S %Z %Y')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'Thu Jan 1 00:32:36 PST 2015' does not match format '%a …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码:
def calcDateDifferenceInMinutes(end_date,start_date):
fmt = '%Y-%m-%d %H:%M:%S'
start_date_dt = datetime.strptime(start_date, fmt)
end_date_dt = datetime.strptime(end_date, fmt)
# convert to unix timestamp
start_date_ts = time.mktime(start_date_dt.timetuple())
end_date_ts = time.mktime(end_date_dt.timetuple())
# they are now in seconds, subtract and then divide by 60 to get minutes.
return (int(end_date_ts-start_date_ts) / 60)
Run Code Online (Sandbox Code Playgroud)
来自这个问题:stackoverflow问题
但我收到这条消息:
AttributeError:'str'对象没有属性'datetime'
我已经回顾了类似的问题但除了做以下事情之外没有看到任何其他选择:
start_date_dt = datetime.datetime.strptime(start_date, fmt)
Run Code Online (Sandbox Code Playgroud)
这是完整的痕迹:
> Traceback (most recent call last): File "tabbed_all_cols.py", line
> 156, in <module>
> trip_calculated_duration = calcDateDifferenceInMinutes (end_datetime,start_datetime) File "tabbed_all_cols.py", line 41, in
> calcDateDifferenceInMinutes …Run Code Online (Sandbox Code Playgroud) 不知道我在这里做错了什么
getting_data | gunzip | jq -r '.time_field | strptime("%Y-%m-%dT%H:%M:%S.%fZ")'
Run Code Online (Sandbox Code Playgroud)
错误返回如下:
jq: error (at <stdin>:0): date "2018-03-13T14:00:17.1614661Z" does not
match format "%Y-%m-%dT%H:%M:%S.%fZ"
Run Code Online (Sandbox Code Playgroud)
期望的输出是2018-03-13 14:00:17
我正在尝试在 Python 中解析格式'2016-04-15T12:24:20.707Z' 的日期,尝试过strptime,但不起作用,我也尝试过 django parse_datetime但它只返回 none 作为值
我有一些从 JIRA API 动态输入的创建和更新的日期时间,用于自动化过程,使用 python 脚本每月获取 JIRA 票证加载到 Excel 工作表中。
一些样本日期如下:
我想以 %d-%b-%Y 之类的格式呈现(例如:2017 年 2 月 9 日)
我尝试过使用 pythondatetime.strptime和strftime.
示例代码如下:
from datetime import datetime
datetimeObj = datetime.strptime('2017-02-09T09:43:04.216+1000', '%Y-%m-%dT%H:%M:%S.%f+1000')
print(datetimeObj.strftime('%d-%b-%Y'))
Run Code Online (Sandbox Code Playgroud)
这给了我预期的结果,但现在我已经硬编码了我想要的日期等值以及我添加的日期时间格式,+1000但由于它动态更改日期时间,当日期时间以诸如+1100.
我相信最后一部分与夏令时有关,但在这种情况下我无法找到正确的格式。
无论如何,有没有办法转换最后+1000一个+1100部分的日期时间格式,而不是像'%Y-%m-%dT%H:%M:%S.%f+1000'这样进行硬编码?
ValueError:时间数据'03 -10-2011 04:35 PM'与格式'%m-%d-%Y%I:M%p'不匹配
看起来它和我匹配?
datetime = datestr + " " + timestr
date_struct = time.strptime(datetime, "%m-%d-%Y %I:M %p")
Run Code Online (Sandbox Code Playgroud) 这将在python 2.6中工作
from datetime import datetime
print (datetime.strptime('2008-12-31', "%Y-%m-%d")- datetime.strptime('2007-04-30',"%Y-%m-%d")).days
Run Code Online (Sandbox Code Playgroud)
但在2.4中显示以下错误
AttributeError: type object 'datetime.datetime' has no attribute 'strptime'
Run Code Online (Sandbox Code Playgroud)
感谢你的支持.
从datetime导入日期的时间导入strptime
x= strptime('2008-12-31', "%Y-%m-%d")
y= strptime('2007-04-30', "%Y-%m-%d")
print (date(x.tm_year,x.tm_mon,x.tm_mday)- date(y.tm_year,y.tm_mon,y.tm_mday)).days
Run Code Online (Sandbox Code Playgroud) 我正在尝试拆分日期时间...它适用于存储日期,但每当我尝试存储时间时我都会收到错误.
以下代码有效:
datetime = tweet.date.encode( 'ascii', 'ignore')
struct_date = time.strptime(datetime, "%a, %d %b %Y %H:%M:%S +0000")
date = time.strftime("%m/%d/%Y")
Run Code Online (Sandbox Code Playgroud)
但如果我添加以下行,我会收到一个错误:
time = time.strftime("%H:%M:%S")
Run Code Online (Sandbox Code Playgroud)
AttributeError:'str'对象没有属性'strptime'
我在R中使用strptime获取了我的日期字符串的NA值.我查看了各种答案,但它没有用.这是我的代码
startDate=strptime("Wed May 25 01:51:32 UTC 2016", format="%a %B %d %H:%m:%S %Z %Y", tz="UTC")
print(startDate)
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.