LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Can linux *convert* date information on the command line? (https://www.linuxquestions.org/questions/linux-general-1/can-linux-%2Aconvert%2A-date-information-on-the-command-line-4175694467/)

JASlinux 04-29-2021 11:12 AM

Can linux *convert* date information on the command line?
 
I have the date as text or console input.

Can I convert it to Unix seconds in terminal?

example
IN: Tue Sep 11 08:46:40 2001 ---> OUT: 1000169200

teckk 04-29-2021 11:18 AM

Code:

a="Tue Sep 11 08:46:40 2001"

b=$(date -d "$a" +"%s")

echo "$b"
1000216000


teckk 04-29-2021 11:42 AM

Code:

#!/usr/bin/bash

col1=$(tput setaf 2)
col2=$(tput setaf 3)
clr=$(tput sgr0)

message="Enter time-date with format:
12:34 | apr 10 12:34 | apr 10 12:34 2021 | Sat apr 10 12:34 2021

Enter date/time:> "

while :; do
    read -p "${message}" clock_time

    Time_sec=$(date -d "$clock_time" +"%s")

    echo -e ""$col1"\nEpoch time is: "$col2""$Time_sec"\n"$clr""
done


TB0ne 04-29-2021 11:54 AM

Quote:

Originally Posted by JASlinux (Post 6246259)
I have the date as text or console input.

Can I convert it to Unix seconds in terminal?

example
IN: Tue Sep 11 08:46:40 2001 ---> OUT: 1000169200

Did you try reading the man page on the date command?? Possibly paying attention to the %s flag for formatting??


All times are GMT -5. The time now is 11:59 AM.