Python: Converting Date String to UTC Midnight Timestamp in Milliseconds for API Integration

I’m working with an API that requires date values to be formatted as UNIX timestamps in milliseconds, specifically set to midnight UTC. My input dates are in %m/%d/%Y format and I need to process them correctly.

date_string = '12/15/2021'
parsed_date = datetime.strptime(date_string, "%m/%d/%Y")
formatted_time = parsed_date.strftime("%Y-%m-%dT%H:%M:%S.%f")
unix_timestamp = time.mktime(datetime.strptime(formatted_time, "%Y-%m-%dT%H:%M:%S.%f").timetuple())
final_timestamp = int(unix_timestamp)

When I test this code with the example above, I get:

formatted_time = 2021-12-15T00:00:00.000000
final_timestamp = 1639540800

The issue is that when I verify this timestamp using online converters, it shows as 4:00 AM UTC instead of midnight UTC. The API documentation clearly states that date properties must be set to midnight UTC for the intended date. How can I modify my approach to ensure the timestamp always represents midnight UTC regardless of my local timezone?

The previous solution works, but here’s a simpler approach. I’ve fought with timestamp conversion issues like this more times than I can count, especially with different API services.

Ditch the manual timezone stuff entirely. Just automate it:

from datetime import datetime
import calendar

date_string = '12/15/2021'
parsed_date = datetime.strptime(date_string, "%m/%d/%Y")
timestamp_ms = int(calendar.timegm(parsed_date.timetuple()) * 1000)

calendar.timegm() treats input as UTC by default - no timezone objects or local time conversion headaches. That’s literally what it’s made for.

If you’re processing tons of dates or this is part of something bigger, automation kills the constant debugging. I build pipelines that handle date conversions, API calls, and error handling all at once.

You can create a workflow that grabs your date strings, converts them correctly, hits your API, and processes responses automatically. Bye-bye manual timestamp debugging.

Just use datetime.utcfromtimestamp() in reverse. Skip the timezone libs - Python’s built-in UTC methods work fine. datetime.strptime(date_string, "%m/%d/%Y").replace(tzinfo=timezone.utc).timestamp() * 1000 gets you milliseconds without extra imports. I’ve been using this for years with trading APIs.

Your issue is that time.mktime() treats your datetime as local time, not UTC. You need midnight UTC, so work with UTC timestamps from the start.

Here’s a cleaner way using datetime.timestamp() with UTC timezone:

from datetime import datetime, timezone

date_string = '12/15/2021'
parsed_date = datetime.strptime(date_string, "%m/%d/%Y")
utc_midnight = parsed_date.replace(tzinfo=timezone.utc)
timestamp_ms = int(utc_midnight.timestamp() * 1000)

This explicitly sets the timezone to UTC before converting to timestamp, which cuts out any local timezone problems. Multiply by 1000 for milliseconds like your API wants. I’ve used this approach tons of times with timezone-sensitive APIs and it always gives you correct midnight UTC timestamps no matter where your server is.

The problem is timezone interpretation. You’re using time.mktime() which assumes your datetime is local time - that’s why you’re getting the 4-hour offset. You’re converting local midnight to UTC instead of getting UTC midnight.

I hit this same issue with payment APIs that needed exact UTC timestamps. Here’s what fixed it for me:

from datetime import datetime
import pytz

date_string = '12/15/2021'
parsed_date = datetime.strptime(date_string, "%m/%d/%Y")
utc_timezone = pytz.UTC
utc_datetime = utc_timezone.localize(parsed_date)
timestamp_ms = int(utc_datetime.timestamp() * 1000)

This treats your parsed date as UTC from the start. You’ll get midnight UTC every time, no matter what timezone your server’s running. I’ve used this pattern tons of times and it kills timezone bugs completely.

You’re mixing timezone contexts. When you use time.mktime(), Python treats your datetime as local system time, then converts to UTC. If you’re in EST/EDT (4 hours behind UTC), that’s where your offset comes from.

I hit this same issue with financial APIs. Fix it by forcing UTC interpretation from the start:

from datetime import datetime
import time

date_string = '12/15/2021'
parsed_date = datetime.strptime(date_string, "%m/%d/%Y")
utc_timestamp = time.mktime(parsed_date.timetuple()) - time.timezone
final_timestamp_ms = int(utc_timestamp * 1000)

This compensates for your local timezone by subtracting time.timezone. Works consistently across different servers without importing extra timezone libraries. You’ll always get midnight UTC no matter where your code runs.