LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-28-2011, 11:24 PM   #1
priyophan
Member
 
Registered: Apr 2009
Posts: 36

Rep: Reputation: 0
Yesterday's date in unix


#!/usr/bin/perl
$presentdate=`date +"%m-%d-%Y"`;
@datearray=split('-',$presentdate);
$mm=$datearray[0];
$dd=$datearray[1];
$yy=$datearray[2];
if ( $dd > 2 )
{
$dd-=1;
}
elsif ( $mm == ( 5 || 7 || 8 || 10 || 12 ))
{
$dd = 30;
$mm = $mm-1;
}
elsif ( $mm == 1 )
{
$dd=31;
$mm=12;
$yy=$yy-1;
}
elsif ( $mm == 3 )
{
$rem=$yy%4;
$mm=$mm-1;
$dd=29 if $rem==0;
$dd=28 if $rem!=0;
}
elsif ( $mm == ( 2 || 4 || 6 || 9 || 11 ))
{
$dd=31;
$mm=$mm-1;
}
$date = ($mm . "-$dd" ."-$yy");
printf ("%s \n", $date);
 
Old 03-29-2011, 12:32 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
How about:
Code:
date --date="-1 days" +"%Y-%m-%d"
Evo2.
 
Old 03-29-2011, 12:43 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
The OP asked about yesterday's date:
Code:
date -d "yesterday"  +"%Y-%m-%d"
lol

jlinkels
 
1 members found this post helpful.
Old 03-29-2011, 02:12 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by jlinkels View Post
The OP asked about yesterday's date:
Code:
date -d "yesterday"  +"%Y-%m-%d"
Wow. Seeing that rings a bell, I must a have read somewhere that date will take natural language. Seems that things like "last week" work too.

Cheers,

Evo2.
 
Old 03-29-2011, 02:40 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Actually the OP did not ask anything. The perl solution works and I guess the script is meant to be used on Unix machines where the date command lacks the -d, --date option. I used a C program in the good ol' days of IRIX or SUN SPARC for the same reason.
 
Old 03-29-2011, 04:06 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by colucix View Post
Actually the OP did not ask anything. The perl solution works
The OPs solution does not treat leap years correctly.
See for example: http://en.wikipedia.org/wiki/Leap_year#Algorithm

Cheers,

Evo2.
 
Old 03-29-2011, 11:57 PM   #7
priyophan
Member
 
Registered: Apr 2009
Posts: 36

Original Poster
Rep: Reputation: 0
Thnx everyone for the response , I was looking for something that works universally on hpux ,solaris and linux ,the options provided above work on linux mainly and over a leap year I was not sure so I thought it would be better to use something universally valid .
 
Old 03-31-2011, 11:09 AM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by jlinkels View Post
The OP asked about yesterday's date:
Code:
date -d "yesterday"  +"%Y-%m-%d"
lol

jlinkels
If you want some real fun:
Code:
for D in {yester,to,sun,mon,tues,wednes,thurs,fri,satur}day; 
do printf "%10s:  %s  %s  %s  %s\n" $D \
 `date -d "last $D" +"%F"` \
 `date -d "$D" +"%F"`      \
 `date -d "next $D" +"%F"` \
 `date -d "$D week" +"%F"`; 
done
 
Old 03-31-2011, 12:23 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Yeah, I know. Most people overlook the power of date. I have written fairly complicated Bash scripts using date and time and date is invariably your friend.

However, a few weeks ago someone asked to find the last Sunday in March and the last Sunday in October. That was something date could not do.

jlinkels
 
Old 03-31-2011, 02:07 PM   #10
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,143

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
A better Perl function is:

#!/usr/bin/perl
use Date::Calc qw(Today Add_Delta_Days);

my ($y,$m,$d) = Add_Delta_Days(Today(),-1);
print "Yesterday was $d-$m-$y\n"'
 
1 members found this post helpful.
Old 03-31-2011, 05:33 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,646

Rep: Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969
Quote:
Originally Posted by smallpond View Post
A better Perl function is:

#!/usr/bin/perl
use Date::Calc qw(Today Add_Delta_Days);

my ($y,$m,$d) = Add_Delta_Days(Today(),-1);
print "Yesterday was $d-$m-$y\n"'
+1 for smallpond. I agree...the Date::Calc function in Perl is awesome. It even works across years, and calculates leap years automatically. So if you ask it what the date was 5 days ago on January 3, it'll tell you Dec 29th, last year.

Last edited by TB0ne; 03-31-2011 at 05:34 PM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert windows date and time to unix date and time jitupatil_2007 General 8 03-31-2008 05:58 AM
Shell Script for Unix Date ??? ajeetraina Linux - Newbie 11 02-08-2008 12:58 AM
How to get date from unix timestamp ruj.sabya Linux - General 9 03-22-2007 05:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:09 PM.

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