πŸ› οΈToolsShed

Epoch Batch Converter

Convert multiple Unix timestamps at once β€” auto-detects seconds vs milliseconds.

Enter timestamps above and click Convert.

Frequently Asked Questions

Code Implementation

from datetime import datetime, timezone

def timestamps_to_iso(timestamps: list[int | float], unit: str = "s") -> list[str]:
    """Convert a list of Unix timestamps to ISO 8601 strings.
    unit: 's' for seconds, 'ms' for milliseconds
    """
    results = []
    for ts in timestamps:
        if unit == "ms":
            ts = ts / 1000
        dt = datetime.fromtimestamp(ts, tz=timezone.utc)
        results.append(dt.isoformat())
    return results

def iso_to_timestamps(iso_strings: list[str], unit: str = "s") -> list[int]:
    """Convert a list of ISO 8601 strings to Unix timestamps."""
    results = []
    for s in iso_strings:
        dt = datetime.fromisoformat(s.replace("Z", "+00:00"))
        ts = dt.timestamp()
        if unit == "ms":
            ts = int(ts * 1000)
        else:
            ts = int(ts)
        results.append(ts)
    return results

# Example
timestamps = [0, 1000000000, 1700000000, 2000000000]
print("Timestamps to ISO:")
for ts, iso in zip(timestamps, timestamps_to_iso(timestamps)):
    print(f"  {ts} -> {iso}")

print("ISO to timestamps:")
isos = ["1970-01-01T00:00:00+00:00", "2023-11-14T22:13:20+00:00"]
for iso, ts in zip(isos, iso_to_timestamps(isos)):
    print(f"  {iso} -> {ts}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.