LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 12: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, 02:46 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
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, 06: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, 07:06 PM   #4
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
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 07:21 PM.
 
Old 03-02-2009, 04: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, 01:06 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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

Rep: Reputation: 47
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, 06:43 PM   #8
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,539

Rep: Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496
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 12:40 PM. Reason: correct entry for second
 
  


Reply



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 05:11 PM
How to write function to list file (*.log) follow feature time(date, month, year) phanvinhgiap Programming 2 12-30-2008 08:30 PM
Date command - month of year, blank padded? menator Programming 3 06-27-2006 06:00 AM
Multicount in PHP , displays visits on current day, week, month, year and total visit xbaez Programming 1 04-24-2005 02:50 AM

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

All times are GMT -5. The time now is 02:21 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