LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to Find: last login information in terms of year-month-date (http://www.linuxquestions.org/questions/linux-general-1/how-to-find-last-login-information-in-terms-of-year-month-date-702784/)

avklinux 02-06-2009 12:51 PM

How to Find: last login information in terms of year-month-date
 
Thanks
AVklinux

jan61 02-06-2009 02:46 PM

Moin,

I'm not sure, if I understood your question. Do you mean this?
Code:

last | cut -c1-9,40-56 | while read u d; do date -d "$d" +"$u %Y-%m-%d"; done
Jan

avklinux 02-27-2009 06:51 PM

thanks for answer.

can you tell me script to find the diff between 2 dates.

like
d1=2009-23-03
d2=2008-22-78


thanks
avklinux

win32sux 02-27-2009 07:06 PM

Quote:

Originally Posted by avklinux (Post 3459969)
can you tell me script to find the diff between 2 dates.

Script it such that the date is presented in UNIX time format, then do subtraction.

Example of UNIX time output:
Code:

date -u +%s
BTW, I'm moving this to Linux - General. Good luck.

avklinux 03-02-2009 04:18 PM

can u explain in terms of d1 and d2.

means d1=1236430420 and d2=1236430460

then how do i script it ...???

Tinkster 03-03-2009 01:06 PM

Code:

d1=1236430420
d2=1236430460
echo "scale=5; ${d1}-${d2}"|bc


jan61 03-14-2009 03:17 PM

Moin,

Quote:

Originally Posted by avklinux (Post 3459969)
can you tell me script to find the diff between 2 dates.

like
d1=2009-23-03
d2=2008-22-78

d2 is a strange date, isn't it? ;-) I'll use the standard format yyyy-mm-dd for an example:

Code:

jan@jack:~/tmp> d1=2009-03-23
jan@jack:~/tmp> d2=2008-08-22
jan@jack:~/tmp> expr `date -d "$d1" +%s` - `date -d "$d2" +%s`
18406800 # seconds
jan@jack:~/tmp> expr \( `date -d "$d1" +%s` - `date -d "$d2" +%s` \) / 60
306780 # minutes
jan@jack:~/tmp> expr \( `date -d "$d1" +%s` - `date -d "$d2" +%s` \) / 3600
5113 # hours
jan@jack:~/tmp> expr \( `date -d "$d1" +%s` - `date -d "$d2" +%s` \) / 86400
213 # days

Jan

yancek 03-14-2009 06:43 PM

The Bash script below does basically what jan61's post above does. Input dates in the same format to get total days, hours, minutes between two dates.

Code:

#!/bin/bash
read -p "Date1: " d1
read -p "Date2: " d2
d3=$(date --date "$d1" +%s)
d4=$(date --date "$d2" +%s)
d5=$(($d4 - $d3))
printf "Days: "
echo "scale=0; (${d5} / 86400) " | bc
printf "Hours: "
echo "scale=0; ((${d5} / 86400) * 24) " | bc
printf "Minutes: "
echo "scale=0; ((${d5} /86400) * (24) * 60)" | bc

The bash script below here takes same input format to seconds and produces the difference between two dates not just total days, hours, minutes. Example enter date: 2009-03-03 02:30:15, and second date:
2009-03-05 10:45:30, get output:

Days: 2
Hours: 8
Minutes: 15
Seconds: 15


Code:

#!/bin/bash
read -p "Date1: " d1
read -p "Date2: " d2
d3=$(date --date "$d1" +%s)
d4=$(date --date "$d2" +%s)
d5=$(($d4 - $d3))
printf "Days: "
echo "scale=0; (${d5} / 86400) " | bc
printf "Hours: "
echo "scale=0; ((${d5} % 86400) / 3600) " | bc
printf "Minutes: "
echo "scale=0; ((${d5} % 86400) % 3600) / 60 " | bc
printf "Seconds: "
echo "scale=0; ((${d5} % 86400) % 3600) % 60 % 60" | bc

First actual Bash scripts I've done so I imagine there are easier ways to do it. They work! Been playing around trying to get what I wanted to work here for so long I forgot why I was doing it.


All times are GMT -5. The time now is 10:43 AM.