See the following example:

import datetime 
now = datetime.datetime.now()
now.strftime('%Y-%m-%dT%H:%M:%S') + ('-%02d' % (now.microsecond / 10000))

This could result in the following: '2017-09-20T11:52:32-98'

Answer from Henk Dekker on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ datetime.html
datetime โ€” Basic date and time types
Because naive datetime objects are treated by many datetime methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing a specific timestamp in UTC is by calling datetime.fromtimestamp(timestamp, tz=timezone.utc).
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python datetime โ€บ timestamp in python
Python Timestamp With Examples โ€“ PYnative
December 5, 2021 - from datetime import datetime # Getting the current date and time dt = datetime.now() # getting the timestamp ts = datetime.timestamp(dt) print("Date and time is:", dt) print("Timestamp is:", ts)Code language: Python (python) Run ... Note: Note: It returns timestamp in float type to get timestamp without decimal value convert it to an integer using the int(ts) constructor. The time moduleโ€˜s time() method returns the current time in the timestamp format, which is nothing but the time elapsed from the epoch time, January 1, 1970.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ datetime โ€บ strftime
Python strftime() - datetime to string
from datetime import datetime timestamp = 1528797322 date_time = datetime.fromtimestamp(timestamp) d = date_time.strftime("%c") print("Output 1:", d) d = date_time.strftime("%x") print("Output 2:", d) d = date_time.strftime("%X") print("Output 3:", d) ... Format codes %c, %x and %X are used ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_datetime.asp
Python Dates
The method is called strftime(), and takes one parameter, format, to specify the format of the returned string: ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ python
Working with Timestamps in Python
July 3, 2023 - In this example, the strptime() function parses the timestamp_string into a datetime object based on the format string '%Y-%m-%d %H:%M:%S'. A Unix timestamp, also known as Unix time, POSIX time, or Epoch time, is a system for describing a point ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_date_time.htm
Python - Date and Time
fromtimestamp(timestamp, timezone.utc) โˆ’ On the POSIX compliant platforms, it is equivalent todatetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=timestamp) fromordinal(ordinal) โˆ’ Return the datetime corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. fromisoformat(date_string) โˆ’ Return a datetime corresponding to a date_string in any valid ISO 8601 format.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-strftime-function
Python strftime() function - GeeksforGeeks
July 12, 2025 - Python provides the strftime() function lets us convert a datetime object into a formatted string using special format codes. These format codes represent different parts of the date and time-like year, month, weekday, hour, etc. Itโ€™s especially useful when we need to display dates in a specific style or generate timestamps in logs and reports.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.to_datetime.html
pandas.to_datetime โ€” pandas 3.0.2 documentation
scalars can be int, float, str, datetime object (from stdlib datetime module or numpy). They are converted to Timestamp when possible, otherwise they are converted to datetime.datetime.
๐ŸŒ
Jiteshcodes
jiteshcodes.com โ€บ i built google bigtable in go: one file, zero dependencies
I Built Google Bigtable in Go: One File, Zero Dependencies | Jitesh's Blog
February 20, 2026 - The data model gives users enormous flexibility. Googleโ€™s original use cases included the web index (storing crawled HTML with timestamps), Google Analytics, Google Earth, and Gmail.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Timestamp.html
pandas.Timestamp โ€” pandas 3.0.2 documentation
class pandas.Timestamp(ts_input=<object object>, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None, *, nanosecond=None, tz=<object object>, unit=None, fold=None)[source]# Pandas replacement for python datetime.datetime object.
๐ŸŒ
CodeBeautify
codebeautify.org โ€บ jsonviewer
Best JSON Viewer and JSON Beautifier Online
Many websites offer APIs, which will return data in JSON format. Often the JSON provided has white space removed to reduce the size payload or data. This JSON API formatter gives you a quick and easy way to format the JSON so you can read it.
๐ŸŒ
Slack
docs.slack.dev โ€บ methods โ€บ chat.postmessage
chat.postMessage method | Slack Developer Docs
This field represents the timestamp of the draft's last update at the time this API is called.
๐ŸŒ
Twelve Data
twelvedata.com โ€บ docs
API Documentation - Twelve Data
The earliest_timestamp endpoint provides the earliest available date and time for a specified financial instrument at a given data interval.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-fix-python-time-formatting-489754
How to fix python time formatting | LabEx
This tutorial provides a comprehensive guide to mastering Python's time formatting techniques, helping programmers efficiently handle and manipulate time objects across various programming scenarios. Python provides robust tools for working with time and dates through various built-in modules. Understanding these basics is crucial for developers working on time-sensitive applications. Python offers several modules for time manipulation: import time import datetime ## Using time module current_timestamp = time.time() print("Current timestamp:", current_timestamp) ## Using datetime module current_datetime = datetime.datetime.now() print("Current datetime:", current_datetime)
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Date โ€บ getTime
Date.prototype.getTime() - JavaScript | MDN
Date objects are fundamentally represented by a timestamp, and this method allows you to retrieve the timestamp. You can use this method to help assign a date and time to another Date object.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ time.html
time โ€” Time access and conversions
The %f format directive only applies to strptime(), not to strftime(). However, see also datetime.datetime.strptime() and datetime.datetime.strftime() where the %f format directive applies to microseconds. When used with the strptime() function, the %p directive only affects the output hour field if the %I directive is used to parse the hour. The range really is 0 to 61; value 60 is valid in timestamps representing leap seconds and value 61 is supported for historical reasons.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ datetime
Python datetime Module: Working with Dates, Times, and Formatting
To work with dates and times in Python, you import the datetime object from the datetime module. The two most common operations are converting strings to datetime objects (strptime) and formatting datetime objects into strings (strftime).
๐ŸŒ
InfluxData
influxdata.com โ€บ home โ€บ what is the python time now string?
What Is the Python Time Now String? | InfluxData
November 6, 2023 - This will return a result in the format YYYY-MM-DD HH:MM:SS.ssssss. When you use the datetime.now() function, it returns a datetime object. To convert this to a string, you can use Pythonโ€™s string formatting: