473,382 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

shell: how to get the date before the current date ?

43
Hi guys,

is there a function in shell( bash ) to get the date of the day before the current date ?

I made it with a long script, because it becomes complicated with the beginning and the end of month and year, so I wonder is there a function in shell to get the date before the current date?
who knows, please help !

thanks a million.
Tanya
Aug 9 '07 #1
5 19281
prn
254 Expert 100+
No. Bash has no built-in function to do that, but you can make your own.

For example:
Expand|Select|Wrap|Line Numbers
  1. yesterday () {
  2.     i=`date --utc +%s`
  3.     j=$(($i-86400))
  4.     date --utc --date "1970-01-01 $j sec" "+%Y-%m-%d %T"
  5. }
will return something like:
2007-08-08 16:53:29

If you want a different format, just change the format string (the last bit of the last line). See man date for how format strings work.

You can put the function, however you might want to edit it, into your .bashrc if you want to use it on a regular basis. Or, of course, if you wanted it in a bash script, you can put it into that script.

HTH,
Paul
Aug 9 '07 #2
prn
254 Expert 100+
I noticed that my last function was doing something odd with the time. First off, it was trying to give back the time in UTC, which may (or may not) be appropriate for your needs. Furthermore, I was not even getting the right time in UTC, which I still don't understand, even after some attempt at debugging.

However, with a little digging, I found that the whole thing is far more complicated than required as you can do this:
Expand|Select|Wrap|Line Numbers
  1. date -d "-1 day"
which is far simpler and clearer, and saves a lot of silly debugging. :-)

Enjoy,
Paul
Aug 9 '07 #3
tanyali
43
I noticed that my last function was doing something odd with the time. First off, it was trying to give back the time in UTC, which may (or may not) be appropriate for your needs. Furthermore, I was not even getting the right time in UTC, which I still don't understand, even after some attempt at debugging.

However, with a little digging, I found that the whole thing is far more complicated than required as you can do this:
Expand|Select|Wrap|Line Numbers
  1. date -d "-1 day"
which is far simpler and clearer, and saves a lot of silly debugging. :-)

Enjoy,
Paul
thanks Paul, you saved me long long long script !
I used it as : date +"%Y%m%d" -d "-1 day"
and get 20070809.

also in PHP, use the function mktime() to get any date, I used :

$yesterday=mktime(0, 0, 0, date("m") , date("d")-1, date("Y"));
echo "yesterday is ".date("Ymd",$yesterday);

thanks Paul .~_~.
Tanya.
Aug 10 '07 #4
Allmots Unix. The most simple way:
TZ=GMT+24 date +"%Y/%M/%d"
Dec 21 '07 #5
Instead of writing "-1 day", you can also use "1 day ago", which I find more intuitively. It's only different syntax and does absolutely the same.

date -d "1 day ago"
Oct 5 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Michael Banjumi | last post by:
Where n is the parm reads from the file. How can I handle this without giving any syntax error and produce the desired result? TIA
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
2
by: Andy B. | last post by:
I've got a python utility that I want to change my shell's current directory based on criteria it finds. I've scoured google and the python cookbook and can't seem to figure out if this is even...
6
by: vijayk | last post by:
Hi all, I have a field which has data as YYYYMMDD, and I have to find the age of the person by substracting it from current date. can you please please advice... thanks
0
by: Shawger Lager | last post by:
Hi, I am new to DB2 (and SQL) and I am having some problems with the current date. I am trying to make sure the date in the table review is not past the current day using a constriant. I have tried...
3
by: seanw89 | last post by:
I have a bit trouble in converting CURRENT DATE into a integer type as YYYYMMDD. The following script is trying to find a day matching a date column with integer type with yesterday (CURRENT DATE...
5
by: bruce24444 | last post by:
I have a database which assigns warranty claims to people with a main screen showing number of files assigned to each person. The number assigned shows day, week, month and year numbers so they can...
3
by: lyndac | last post by:
Hello! Last night a job to create a view using DATE(CURRENT DATE - 1 DAY) received the current days date instead the previous days date. Any one know why/how this can happen? The...
2
by: NitinSawant | last post by:
Hello Experts, I'm newbie to php, I want to get date 30 day back from current date using php. i'm able to get current date, $currentDate= date("d-M-y",time());
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.