简体   繁体   English

如何使用 Python 格式化 Hubspot API 的日期值

[英]How to format a date value for Hubspot API using Python

I have read the docs saying that to pass the value for a Hubspot date field you should format your Date as midnight UTC.我已阅读文档说要传递 Hubspot 日期字段的值,您应该将日期格式设置为午夜UTC。 However, I've had no luck doing so in Python.但是,我在 Python 中没有这样做。 I assume I am just missing the magic Python incantation that will get the right result.我想我只是错过了能得到正确结果的魔法 Python 咒语。 Here is what I have:这是我所拥有的:

    from pytz import timezone, utc
    from hubspot.crm.contacts import SimplePublicObject,

    created_dt = # datetime from sqlalchemy query
    utcdt = utc.localize(
        datetime(
            year=created_dt.year,
            month=created_dt.month,
            day=created_dt.day
            )
    )
    ts = int(utcdt.timestamp())
    props = SimplePublicObjectInput({"last_booking": str(ts)})
    return client.crm.companies.basic_api.update(
        hs_id, simple_public_object_input=props
    )

this returns this error:这将返回此错误:

{"status":"error",
 "message":"Property values were not valid: [{\"isValid\":false,\"message\":\"1570233600 is at 4:10:33.600 UTC, not midnight!\"...
 }

Ah, the answer was right there.啊,答案就在那里。 Python timestamp returns the time in seconds, and HubSpot expects microseconds. Python timestamp以秒为单位返回时间,HubSpot 需要微秒。 I just had to multiply by 1000:我只需要乘以 1000:

ts = int(utcdt.timestamp()*1000)

now all looks good.现在一切看起来都不错。

did you try adding hours and minutes to your datetime call您是否尝试在日期时间通话中添加小时和分钟

datetime(
    year=created_dt.year,
    month=created_dt.month,
    day=created_dt.day,
    hour=0,
    minute=0
)

Use the Hubspot supported "sanetime" module: https://github.com/HubSpot/sanetime使用 Hubspot 支持的“sanetime”模块: https://github.com/HubSpot/sanetime

Then to get a date:然后得到一个日期:

yourdate = datetime.datetime.date()
hubspot_date = sanetime.time(yourdate )

Or if you do not want a dependency:或者,如果您不想要依赖项:

#convert datetime to UTC
your_utc_datetime = your_datetime.astimezone(pytz.UTC)

#replace time with midnight
your_utc_date_midnight = your_utc_datetime.replace(hour=0,minute=0,second=0, microsecond=0)

# convert to epoch (Python 3.3+)
your_hubspot_date = your_utc_date_midnight.timestamp()*1000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Hubspot 的午夜将日期转换为 UNIX 毫秒 API - How to Convert a Date to UNIX Miliseconds at Midnight for Hubspot API 如何使用 Python 从 Hubspot 查询多个属性? - How to query multiple properties from Hubspot using Python? 如何使用python修复日期列格式 - how to fix date column format using python 如何使用 Pandas 在 Python 中格式化日期和时间 - How To Format Date and Time in Python Using Pandas 如何使用 Python 转换 dataframe 中的日期格式? - How to convert the date format in a dataframe using Python? 如何在工作表上以其他格式显示日期值(Python日期为文本) - How to show date value in another format (python date to text) on worksheet Python REST API日期格式 - Python REST API date format 使用 AWS Lambda Python API 调用将有效负载数据字符串发布到 Hubspot 时出错 - Error when posting Payload data string to Hubspot using an AWS Lambda Python API call 如何构建一个 function(在 Python 中)调用来自 Google Cloud Functions 中不同(hubspot)API 密钥的数据? - How to build a function (in Python) that calls the data from different (hubspot) API-keys in Google Cloud Functions? Python 格式值作为有效日期 - Python format value as a valid date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM