Skip to content

Instantly share code, notes, and snippets.

@mcwalrus
Last active November 21, 2025 04:56
Show Gist options
  • Select an option

  • Save mcwalrus/2b42dfc392dc581a874849d592ab5ba4 to your computer and use it in GitHub Desktop.

Select an option

Save mcwalrus/2b42dfc392dc581a874849d592ab5ba4 to your computer and use it in GitHub Desktop.
Unix datetime converters
#!/bin/bash
unix_to_datetime() {
if command -v gdate >/dev/null 2>&1; then
gdate -u -d "@$timestamp" +"%Y-%m-%d %H:%M:%S"
elif [[ "$OSTYPE" == "darwin"* ]]; then
date -u -r "$timestamp" +"%Y-%m-%d %H:%M:%S"
else
date -u -d "@$timestamp" +"%Y-%m-%d %H:%M:%S"
fi
}
datetime_to_unix() {
local datetime="$1"
if [[ "$OSTYPE" == "darwin"* ]]; then
date -j -u -f "%Y-%m-%d %H:%M:%S" "$datetime" +"%s"
else
date -d -u "$datetime" +"%s"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment