- 1 year ago
Any format date/time conversion to epoch
Comment
1 / 1
To convert a date/time to epoch format (Unix timestamp) in various programming languages, you can use the following approaches:
Python:
pythonimport datetime
import time
# Convert current date/time to epoch
current_time = datetime.datetime.now()
epoch_time = int(current_time.timestamp())
print(epoch_time)
# Convert a specific date/time to epoch
specific_time = datetime.datetime(2023, 1, 1, 0, 0, 0) # Example: January 1, 2023, 00:00:00
epoch_time = int(specific_time.timestamp())
print(epoch_time)
JavaScript:
javascript// Convert current date/time to epoch
var current_time = Math.floor(Date.now() / 1000);
console.log(current_time);
// Convert a specific date/time to epoch
var specific_time = Math.floor(new Date(2023, 0, 1).getTime() / 1000); // Example: January 1, 2023, 00:00:00
console.log(specific_time);
Bash:
bash# Convert current date/time to epoch
current_time=$(date +%s)
echo $current_time
# Convert a specific date/time to epoch
specific_time=$(date -d "2023-01-01 00:00:00" +%s) # Example: January 1, 2023, 00:00:00
echo $specific_time
These examples demonstrate how to convert the current date/time or a specific date/time to epoch format using Python, JavaScript, and Bash. The epoch time is the number of seconds elapsed since January 1, 1970 (Unix epoch).