LinuxQuestions.org
Help answer threads with 0 replies.
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 02-06-2009, 01:51 PM   #1
avklinux
Member
 
Registered: Nov 2008
Posts: 32

Rep: Reputation: 15
How to Find: last login information in terms of year-month-date


Thanks
AVklinux
 
Old 02-06-2009, 03:46 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
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
 
Old 02-27-2009, 07:51 PM   #3
avklinux
Member
 
Registered: Nov 2008
Posts: 32

Original Poster
Rep: Reputation: 15
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
 
Old 02-27-2009, 08:06 PM   #4
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
Quote:
Originally Posted by avklinux View Post
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.

Last edited by win32sux; 02-27-2009 at 08:21 PM.
 
Old 03-02-2009, 05:18 PM   #5
avklinux
Member
 
Registered: Nov 2008
Posts: 32

Original Poster
Rep: Reputation: 15
can u explain in terms of d1 and d2.

means d1=1236430420 and d2=1236430460

then how do i script it ...???
 
Old 03-03-2009, 02:06 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Code:
d1=1236430420
d2=1236430460
echo "scale=5; ${d1}-${d2}"|bc
 
Old 03-14-2009, 04:17 PM   #7
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

Quote:
Originally Posted by avklinux View Post
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
 
Old 03-14-2009, 07:43 PM   #8
yancek
Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 5,072

Rep: Reputation: 804Reputation: 804Reputation: 804Reputation: 804Reputation: 804Reputation: 804Reputation: 804
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.

Last edited by yancek; 03-15-2009 at 01:40 PM. Reason: correct entry for second
 
  


Reply


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
LQ-er of the day/week/month/year? alan_ri LQ Suggestions & Feedback 0 01-24-2009 06:11 PM
How to write function to list file (*.log) follow feature time(date, month, year) phanvinhgiap Programming 2 12-30-2008 09:30 PM
Date command - month of year, blank padded? menator Programming 3 06-27-2006 07:00 AM
Multicount in PHP , displays visits on current day, week, month, year and total visit xbaez Programming 1 04-24-2005 03:50 AM


All times are GMT -5. The time now is 10:35 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration