Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in Shell (Bash)

How to convert Datetime to Unix timestamp in Shell (Bash)

🎈 🎈 🎈
+1

    #!/bin/bash

    # Human-readable date (2018-07-15 12:30:45)
    date_str="2018-07-15 12:30:45"
    
    # Convert Date to Unix timestamp
    unix_timestamp=$(date -d "$date_str" +%s)
    
    echo "Unix Timestamp: $unix_timestamp"            
        

Output:
Unix Timestamp: 1692835200
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Bash example, the date_str variable holds the human-readable date '2018-07-15 12:30:45'. The date -d "$date_str" +%s command converts this date to a Unix timestamp using the +%s option, which outputs the timestamp in seconds.
The result is printed as a Unix timestamp.



Other useful Bash date functions

Option Description Example
-d Parses the provided string as a date. date -d "2018-07-15 12:30:45" returns Sun Jul 15 12:30:45 UTC 2018
+%s Outputs the Unix timestamp (seconds since 1970-01-01). date -d "2018-07-15 12:30:45" +%s returns 1531656645
-u Uses UTC (Coordinated Universal Time) for the output. date -u returns the current UTC time.
+%Y-%m-%d Formats the date as year-month-day. date +%Y-%m-%d returns 2024-08-28
+%H:%M:%S Formats the time as hours:minutes:seconds. date +%H:%M:%S returns 12:34:56
+%F Formats the date as YYYY-MM-DD (ISO format). date +%F returns 2024-08-28
+%T Formats the time as HH:MM:SS. date +%T returns 12:34:56
@timestamp Interprets the given timestamp as a Unix timestamp. date -d @1514764800 returns Mon Jan 1 00:00:00 UTC 2018
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder