Bash Script to Convert Date to Unix Timestamp
Use the Bash command
date -d "your_date" +%s to convert a date string to a Unix timestamp, for example: date -d "2024-06-01 12:00:00" +%s.Examples
Input2024-06-01 12:00:00
Output1717204800
Input1970-01-01 00:00:00
Output0
Input2024-12-31 23:59:59
Output1735689599
How to Think About It
To convert a date to a Unix timestamp in Bash, you use the
date command with the -d option to specify the date string, and the format +%s to output the time as seconds since January 1, 1970 (Unix epoch). This converts human-readable dates into a numeric timestamp.Algorithm
1
Get the date string input from the user or script argument.2
Use the <code>date</code> command with <code>-d</code> to parse the date string.3
Format the output with <code>+%s</code> to get the Unix timestamp.4
Print the resulting timestamp.Code
bash
#!/bin/bash # Read date string from argument input_date="$1" # Convert to Unix timestamp timestamp=$(date -d "$input_date" +%s) # Print the timestamp echo "$timestamp"
Output
1717204800
Dry Run
Let's trace the input '2024-06-01 12:00:00' through the script
1
Receive input date string
input_date = '2024-06-01 12:00:00'
2
Run date command to convert
date -d '2024-06-01 12:00:00' +%s
3
Get Unix timestamp output
1717204800
4
Print the timestamp
echo 1717204800
| Step | Action | Value |
|---|---|---|
| 1 | Input date string | 2024-06-01 12:00:00 |
| 2 | Convert with date command | 1717204800 |
| 3 | Output timestamp | 1717204800 |
Why This Works
Step 1: Using date command with -d
The -d option tells date to parse the given date string instead of the current date.
Step 2: Formatting output as Unix timestamp
The +%s format outputs the time as seconds since the Unix epoch (January 1, 1970).
Step 3: Printing the result
The script stores the timestamp in a variable and prints it, making it easy to use in other scripts or commands.
Alternative Approaches
Using date with UTC timezone
bash
#!/bin/bash input_date="$1" timestamp=$(date -u -d "$input_date" +%s) echo "$timestamp"
This method converts the date assuming UTC timezone, useful if you want consistent timestamps regardless of local timezone.
Using Python from Bash
bash
#!/bin/bash input_date="$1" timestamp=$(python3 -c "import time; import sys; print(int(time.mktime(time.strptime(sys.argv[1], '%Y-%m-%d %H:%M:%S'))))" "$input_date") echo "$timestamp"
This uses Python to convert the date, helpful if <code>date -d</code> is not available or for more complex date formats.
Complexity: O(1) time, O(1) space
Time Complexity
The conversion uses a single system call to date, which runs in constant time regardless of input size.
Space Complexity
Only a few variables are used to store input and output, so space usage is constant.
Which Approach is Fastest?
Using the built-in date command is fastest and simplest; calling Python adds overhead but offers more flexibility.
| Approach | Time | Space | Best For |
|---|---|---|---|
| date -d +%s | O(1) | O(1) | Simple date strings, fast conversion |
| date -u -d +%s | O(1) | O(1) | UTC timestamps, timezone consistency |
| Python time.mktime | O(1) | O(1) | Complex formats, portability when date command is limited |
Always quote your date string in
date -d to avoid errors with spaces or special characters.Forgetting to quote the date string or using an unsupported date format causes
date to fail or give wrong results.