LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-29-2021, 11:12 AM   #1
JASlinux
Member
 
Registered: Oct 2020
Posts: 95

Rep: Reputation: Disabled
Question 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
 
Old 04-29-2021, 11:18 AM   #2
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,520

Rep: Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095
Code:
a="Tue Sep 11 08:46:40 2001"

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

echo "$b"
1000216000
 
1 members found this post helpful.
Old 04-29-2021, 11:42 AM   #3
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,520

Rep: Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095
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
 
Old 04-29-2021, 11:54 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,827

Rep: Reputation: 6983Reputation: 6983Reputation: 6983Reputation: 6983Reputation: 6983Reputation: 6983Reputation: 6983Reputation: 6983Reputation: 6983Reputation: 6983Reputation: 6983
Quote:
Originally Posted by JASlinux View Post
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??
 
  


Reply

Tags
conversion, date, datetime


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to read date and convert it into yesterday's date ? sysmicuser Linux - Newbie 5 05-03-2018 11:28 PM
[SOLVED] Convert Julian date to Calendar date keif Programming 3 03-21-2014 09:16 AM
how to convert windows date and time to unix date and time jitupatil_2007 General 8 03-31-2008 05:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration