M
M
mmserebryakov2021-12-28 14:59:28
Python
mmserebryakov, 2021-12-28 14:59:28

Convert milliseconds to date of specific format?

Please help me convert milliseconds to a datetime date for a query in SalesForce
Input string or number = 1640256042000
Output format = 2011-04-26T10:00:00-08:00
In the request it will look like this - "SELECT Name FROM Account WHERE CreatedDate > 2011-04-26T10:00:00-08:00" - so I checked, it returns the data, this is a request to the SF database

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Drill, 2021-12-28
@Drill

from datetime import datetime, timezone

timestamp = 1640256042000
dt = datetime.fromtimestamp(timestamp/1000)

print(dt)
>>> 2021-12-23 12:40:42

dt = datetime.fromtimestamp(timestamp/1000, tz=timezone.utc).isoformat()
print(dt)
>>> 2021-12-23T10:40:42+00:00

R
Rodion, 2021-12-28
@rodion4dev

The string 1640256042000 doesn't look like milliseconds; maybe it's just a Unix timestamp? The format is also not clear to me, but it looks more like the ISO format.
If so then:

from datetime import datetime
parsed_time = datetime.fromtimestamp(input_time)
query_format_time = parsed_time.isoformat()
query = "SELECT Name FROM Account WHERE CreatedDate > '%s'" % query_format_time

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question