Date minus 1 day


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date minus 1 day
# 1  
Old 10-22-2014
Date minus 1 day

Hi the below code is failing i am trying to generate do the following:

2014-10-22 11:26:00 (Substract 24 hrs)
should produce
2014-10-21 11:26:00

I need the same formatting below because that gets inputted into code for an applcication report and it must match the applicaiton accepted format.

Code:
hourt="$d $h:$m:00"
hour2="$d $(printf '%02d:%02d:00' $((${h#0} - 12)) $m)"


2014-10-22 11:38:00
2014-10-22 -13:38:00


How do i subtract 1 day? without the negative being produced.



Thanks,

Sam
# 2  
Old 10-22-2014
What is your system? Do you have GNU date? Date math isn't trivial unfortunately, with leap years and varying lengths of months.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-22-2014
If GNU date, consider:

Code:
date  -d '2014-10-22 11:26:00 CDT - 1 day'

If old Unix date, you can manipulate UTC/GMT 0 withing 24 hours.... so you can do things like:

Code:
TZ=UTC24 date

The latter might not help... all depends on what you can or cannot do on your system.
This User Gave Thanks to cjcox For This Post:
# 4  
Old 10-22-2014
rhel5 yes ive got gnu date.

im messing with the below but cant get right format...
how do I add hrs at the into it???
Code:
date --date "-1 days" +'%Y-%m-%d'

Code:
2014-10-21 11:38:00

like above

---------- Post updated at 12:29 PM ---------- Previous update was at 12:26 PM ----------

Code:
date --date "-1 days" +'%Y-%m-%d %H:%M:%S'


Got it Smilie

---------- Post updated at 12:32 PM ---------- Previous update was at 12:29 PM ----------

thanks for reminding me corona! I always forget about GNU date :S
# 5  
Old 10-22-2014
Quote:
Originally Posted by Samuel12
Code:
date --date "-1 days" +'%Y-%m-%d %H:%M:%S'

Got it Smilie
That format combination is common enough that there's even a shortcut for it:
Code:
 date --date "-1 days" +'%F %T'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. Shell Programming and Scripting

Need to minus one day from a date given

Hello Folks, I have a variable output holding date as below - output = "20141220" I need to extract a day out of it and store it in another variable i.e. something similar to below - output1=20141219" and if the month is changing i.e. date in on 31st or 1st it should be taken care of "date... (5 Replies)
Discussion started by: ektubbe
5 Replies

3. Homework & Coursework Questions

How to minus 2 month from current date?

I am running a script in ksh to get the 2 months back date from system date.The below code is giving correct date output from putty command prompt.But while running the script is .ksh file it is giving the error below.Please suggest. ; d=a; y=a m-=num while(m < 1) {m+=12; y--}... (1 Reply)
Discussion started by: hini
1 Replies

4. HP-UX

Actual date minus six months (HP UX)

Hi there, is it possible to get the actual date minux six months with just a simple command? It's easy with Linux but on HP Unix (for me) impossible ;) Best wishes (3 Replies)
Discussion started by: System
3 Replies

5. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

6. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

7. UNIX and Linux Applications

sqlite: calculating with dates - compare current date minus 6 months with stored record

Hi I have a table with name, date in format DD.MM.YYYY. I need to something like this (I try to explain in pseudo code) if SYSDATE (current date) minus 6 months > $expiry date print OK else print NOK with $name and $expiry date I know this is possible with Oracle. How to do this... (0 Replies)
Discussion started by: slashdotweenie
0 Replies

8. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

9. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

10. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies
Login or Register to Ask a Question