Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-29-2011, 12:24 AM
|
#1
|
Member
Registered: Apr 2009
Posts: 36
Rep:
|
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);
|
|
|
03-29-2011, 01:32 AM
|
#2
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
How about :
Code:
date --date="-1 days" +"%Y-%m-%d"
Evo2.
|
|
|
03-29-2011, 01:43 AM
|
#3
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
The OP asked about yesterday's date:
Code:
date -d "yesterday" +"%Y-%m-%d"
lol
jlinkels
|
|
1 members found this post helpful.
|
03-29-2011, 03:12 AM
|
#4
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Quote:
Originally Posted by jlinkels
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.
|
|
|
03-29-2011, 03:40 AM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
|
|
|
03-29-2011, 05:06 AM
|
#6
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Quote:
Originally Posted by colucix
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.
|
|
|
03-30-2011, 12:57 AM
|
#7
|
Member
Registered: Apr 2009
Posts: 36
Original Poster
Rep:
|
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 .
|
|
|
03-31-2011, 12:09 PM
|
#8
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
Quote:
Originally Posted by jlinkels
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
|
|
|
03-31-2011, 01:23 PM
|
#9
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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
|
|
|
03-31-2011, 03:07 PM
|
#10
|
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,228
|
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.
|
03-31-2011, 06:33 PM
|
#11
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,331
|
Quote:
Originally Posted by smallpond
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 06:34 PM.
|
|
|
All times are GMT -5. The time now is 04:37 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|