Skip to content

Instantly share code, notes, and snippets.

@medeirosinacio
Last active August 27, 2025 12:49
Show Gist options
  • Save medeirosinacio/2336b841e2f8278b998aa749fea55b71 to your computer and use it in GitHub Desktop.
Save medeirosinacio/2336b841e2f8278b998aa749fea55b71 to your computer and use it in GitHub Desktop.
Bash Function to_timestamp_ms – Convert Dates to Unix Timestamp in Milliseconds
#!/bin/bash
# File: to_timestamp_ms.sh
# Description: Convert a date in DD/MM/YYYY HH:MM:SS format to Unix timestamp in milliseconds
# Compatible with macOS (BSD date) and Linux (GNU date)
#
# Example usage:
# ./to_timestamp_ms.sh "25/08/2025 19:50:05"
# # Output: 1756162205000
#
# Or source the script and use the function directly in your shell:
# source to_timestamp_ms.sh
# to_timestamp_ms "25/08/2025 19:50:05"
to_timestamp_ms() {
local input="$*"
if command -v gdate >/dev/null 2>&1; then
gdate -d "$input" "+%s%3N"
elif date --version >/dev/null 2>&1; then
date -d "$input" "+%s%3N"
else
date -j -f "%d/%m/%Y %H:%M:%S" "$input" "+%s000"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment