如何通过ServiceNow RestAPI为日期时间字段传入毫秒值(1568895389)
Alright, let's walk through exactly how to get that timestamp (your example 1568895389) into a ServiceNow DateTime field via their REST API. Here's what you need to know:
First, note that your given timestamp is second-level Unix time (10 digits). ServiceNow's DateTime fields expect either a millisecond-level Unix timestamp (13 digits) or an ISO 8601 formatted string with milliseconds. So first step: convert your 10-digit timestamp to 13 digits by multiplying by 1000. For your example, that gives
1568895389000.There are two straightforward ways to send this value in your API request:
Option 1: Send the Millisecond Timestamp as a Numeric Value
This is the simplest approach if you don't want to mess with date formatting. Just include the 13-digit timestamp directly in your JSON payload as a number (no quotes):
{ "your_date_time_field_name": 1568895389000 }
Option 2: Convert to an ISO 8601 String with Milliseconds
If you prefer using formatted dates, convert the millisecond timestamp to an ISO 8601 string (including .000 for milliseconds, since your original timestamp doesn't have fractional seconds). For your example, that would be 2019-09-19T12:16:29.000Z (UTC time). Include this as a quoted string in your payload:
{ "your_date_time_field_name": "2019-09-19T12:16:29.000Z" }
- A few critical notes to avoid issues:
- ServiceNow stores DateTime values in UTC by default. If your original timestamp is in a local timezone, make sure to adjust it to UTC before converting, or use an ISO string with a timezone offset (e.g.,
2019-09-19T08:16:29.000-04:00for Eastern Daylight Time). - Always set the
Content-Type: application/jsonheader in your request, and authenticate properly (Basic Auth, OAuth, etc.) as required by your ServiceNow instance. - Here's a quick curl example to test creating an incident with the timestamp:
curl -X POST \ https://your-instance.service-now.com/api/now/table/incident \ -H 'Authorization: Basic YOUR_BASE64_ENCODED_CREDENTIALS' \ -H 'Content-Type: application/json' \ -d '{ "short_description": "Test DateTime with milliseconds", "opened_at": 1568895389000 }'
- ServiceNow stores DateTime values in UTC by default. If your original timestamp is in a local timezone, make sure to adjust it to UTC before converting, or use an ISO string with a timezone offset (e.g.,
Just pick whichever option fits your workflow better—both will work as long as you stick to the millisecond requirement.
内容的提问来源于stack exchange,提问作者Moni

