subtract 1 hour from date in unix shell script
I have the following in a shell script. How can I subtract one hour while retaining the formatting?
DATE=`date "+%m/%d/%Y -%H:%M:%S"`
shell datetime unix
add a comment |
I have the following in a shell script. How can I subtract one hour while retaining the formatting?
DATE=`date "+%m/%d/%Y -%H:%M:%S"`
shell datetime unix
add a comment |
I have the following in a shell script. How can I subtract one hour while retaining the formatting?
DATE=`date "+%m/%d/%Y -%H:%M:%S"`
shell datetime unix
I have the following in a shell script. How can I subtract one hour while retaining the formatting?
DATE=`date "+%m/%d/%Y -%H:%M:%S"`
shell datetime unix
shell datetime unix
edited May 9 '11 at 20:57
Karl Bielefeldt
32.9k44880
32.9k44880
asked May 9 '11 at 8:26
shamir chaikinshamir chaikin
161139
161139
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
The following command works on recent versions of GNU date
:
date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
1
i recieve -date: illegal option -- d usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
– shamir chaikin
May 9 '11 at 8:40
@shamir this means your version of date does not support this option.
– dogbane
May 9 '11 at 8:41
i figure it out - i post it because may be you know some other way to do it on solaris machine?
– shamir chaikin
May 9 '11 at 9:16
@shamir, it may be easiest to just install GNU date. You can use a different prefix or path so it doesn't conflict with the "official" one. When I used to do a lot of work on Solaris, I installed a number of GNU utilities in my /home/karl/bin because they have these handy extra options.
– Karl Bielefeldt
May 9 '11 at 23:48
add a comment |
date -v-60M "+%m/%d/%Y -%H:%M:%S"
DATE=`date -v-60M "+%m/%d/%Y -%H:%M:%S"`
If you have bash version 4.4+
you can use bash's internal date printing and arithmetics:
printf "current date: %(%m/%d/%Y -%H:%M:%S)Tn"
printf "date - 60min: %(%m/%d/%Y -%H:%M:%S)Tn" $(( $(printf "%(%s)T") - 60 * 60 ))
The $(printf "%(%s)T")
prints the epoch seconds, the $(( epoch - 60*60 ))
is bash-aritmetics - subtracting 1hour in seconds. Prints:
current date: 04/20/2017 -18:14:31
date - 60min: 04/20/2017 -17:14:31
yes my OS not support it, thanks any way
– shamir chaikin
May 11 '11 at 5:30
This doesn't work indate
shipped on Ubuntu.
– Flimm
Jun 15 '16 at 16:10
@Flimm This works on OS X and I expect BSD as well
– Jason S
Oct 14 '16 at 1:04
add a comment |
if you need substract with timestamp :
timestamp=$(date +%s -d '1 hour ago');
add a comment |
Convert to timestamp (a long integer), subtract the right number of milliseconds, reformat to the format you need.
Hard to give more details since you don't specify a programming language...
ahh.. i am using shell script, my formula go like this -DATE=date "+%m/%d/%Y -%H:%M:%S"
and from DATE i want to subtract 1 hour
– shamir chaikin
May 9 '11 at 8:32
1
Are you required to stay in shell? An inline awk or perl script could easily subtract an hour and output in your required format.
– CoreyStup
May 9 '11 at 20:38
add a comment |
This work on my Ubuntu 16.04 date
:
date --date="@$(($(date +%s) - 3600))" "+%m/%d/%Y -%H:%M:%S"
And the date
version is date (GNU coreutils) 8.25
add a comment |
If you need change timezone before subtraction with new format too:
$(TZ=US/Eastern date -d '1 hour ago' '+%Y-%m-%d %H:%M')
add a comment |
Here another way to subtract 1 hour.
yesterdayDate=`date -d '2018-11-24 00:09 -1 hour' +'%Y-%m-%d %H:%M'`
echo $yesterdayDate
Output:
2018-11-23 23:09
I hope that It can help someone.
Best Regards!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5934394%2fsubtract-1-hour-from-date-in-unix-shell-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following command works on recent versions of GNU date
:
date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
1
i recieve -date: illegal option -- d usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
– shamir chaikin
May 9 '11 at 8:40
@shamir this means your version of date does not support this option.
– dogbane
May 9 '11 at 8:41
i figure it out - i post it because may be you know some other way to do it on solaris machine?
– shamir chaikin
May 9 '11 at 9:16
@shamir, it may be easiest to just install GNU date. You can use a different prefix or path so it doesn't conflict with the "official" one. When I used to do a lot of work on Solaris, I installed a number of GNU utilities in my /home/karl/bin because they have these handy extra options.
– Karl Bielefeldt
May 9 '11 at 23:48
add a comment |
The following command works on recent versions of GNU date
:
date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
1
i recieve -date: illegal option -- d usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
– shamir chaikin
May 9 '11 at 8:40
@shamir this means your version of date does not support this option.
– dogbane
May 9 '11 at 8:41
i figure it out - i post it because may be you know some other way to do it on solaris machine?
– shamir chaikin
May 9 '11 at 9:16
@shamir, it may be easiest to just install GNU date. You can use a different prefix or path so it doesn't conflict with the "official" one. When I used to do a lot of work on Solaris, I installed a number of GNU utilities in my /home/karl/bin because they have these handy extra options.
– Karl Bielefeldt
May 9 '11 at 23:48
add a comment |
The following command works on recent versions of GNU date
:
date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
The following command works on recent versions of GNU date
:
date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
answered May 9 '11 at 8:35
dogbanedogbane
192k65320374
192k65320374
1
i recieve -date: illegal option -- d usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
– shamir chaikin
May 9 '11 at 8:40
@shamir this means your version of date does not support this option.
– dogbane
May 9 '11 at 8:41
i figure it out - i post it because may be you know some other way to do it on solaris machine?
– shamir chaikin
May 9 '11 at 9:16
@shamir, it may be easiest to just install GNU date. You can use a different prefix or path so it doesn't conflict with the "official" one. When I used to do a lot of work on Solaris, I installed a number of GNU utilities in my /home/karl/bin because they have these handy extra options.
– Karl Bielefeldt
May 9 '11 at 23:48
add a comment |
1
i recieve -date: illegal option -- d usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
– shamir chaikin
May 9 '11 at 8:40
@shamir this means your version of date does not support this option.
– dogbane
May 9 '11 at 8:41
i figure it out - i post it because may be you know some other way to do it on solaris machine?
– shamir chaikin
May 9 '11 at 9:16
@shamir, it may be easiest to just install GNU date. You can use a different prefix or path so it doesn't conflict with the "official" one. When I used to do a lot of work on Solaris, I installed a number of GNU utilities in my /home/karl/bin because they have these handy extra options.
– Karl Bielefeldt
May 9 '11 at 23:48
1
1
i recieve -date: illegal option -- d usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
– shamir chaikin
May 9 '11 at 8:40
i recieve -date: illegal option -- d usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff]
– shamir chaikin
May 9 '11 at 8:40
@shamir this means your version of date does not support this option.
– dogbane
May 9 '11 at 8:41
@shamir this means your version of date does not support this option.
– dogbane
May 9 '11 at 8:41
i figure it out - i post it because may be you know some other way to do it on solaris machine?
– shamir chaikin
May 9 '11 at 9:16
i figure it out - i post it because may be you know some other way to do it on solaris machine?
– shamir chaikin
May 9 '11 at 9:16
@shamir, it may be easiest to just install GNU date. You can use a different prefix or path so it doesn't conflict with the "official" one. When I used to do a lot of work on Solaris, I installed a number of GNU utilities in my /home/karl/bin because they have these handy extra options.
– Karl Bielefeldt
May 9 '11 at 23:48
@shamir, it may be easiest to just install GNU date. You can use a different prefix or path so it doesn't conflict with the "official" one. When I used to do a lot of work on Solaris, I installed a number of GNU utilities in my /home/karl/bin because they have these handy extra options.
– Karl Bielefeldt
May 9 '11 at 23:48
add a comment |
date -v-60M "+%m/%d/%Y -%H:%M:%S"
DATE=`date -v-60M "+%m/%d/%Y -%H:%M:%S"`
If you have bash version 4.4+
you can use bash's internal date printing and arithmetics:
printf "current date: %(%m/%d/%Y -%H:%M:%S)Tn"
printf "date - 60min: %(%m/%d/%Y -%H:%M:%S)Tn" $(( $(printf "%(%s)T") - 60 * 60 ))
The $(printf "%(%s)T")
prints the epoch seconds, the $(( epoch - 60*60 ))
is bash-aritmetics - subtracting 1hour in seconds. Prints:
current date: 04/20/2017 -18:14:31
date - 60min: 04/20/2017 -17:14:31
yes my OS not support it, thanks any way
– shamir chaikin
May 11 '11 at 5:30
This doesn't work indate
shipped on Ubuntu.
– Flimm
Jun 15 '16 at 16:10
@Flimm This works on OS X and I expect BSD as well
– Jason S
Oct 14 '16 at 1:04
add a comment |
date -v-60M "+%m/%d/%Y -%H:%M:%S"
DATE=`date -v-60M "+%m/%d/%Y -%H:%M:%S"`
If you have bash version 4.4+
you can use bash's internal date printing and arithmetics:
printf "current date: %(%m/%d/%Y -%H:%M:%S)Tn"
printf "date - 60min: %(%m/%d/%Y -%H:%M:%S)Tn" $(( $(printf "%(%s)T") - 60 * 60 ))
The $(printf "%(%s)T")
prints the epoch seconds, the $(( epoch - 60*60 ))
is bash-aritmetics - subtracting 1hour in seconds. Prints:
current date: 04/20/2017 -18:14:31
date - 60min: 04/20/2017 -17:14:31
yes my OS not support it, thanks any way
– shamir chaikin
May 11 '11 at 5:30
This doesn't work indate
shipped on Ubuntu.
– Flimm
Jun 15 '16 at 16:10
@Flimm This works on OS X and I expect BSD as well
– Jason S
Oct 14 '16 at 1:04
add a comment |
date -v-60M "+%m/%d/%Y -%H:%M:%S"
DATE=`date -v-60M "+%m/%d/%Y -%H:%M:%S"`
If you have bash version 4.4+
you can use bash's internal date printing and arithmetics:
printf "current date: %(%m/%d/%Y -%H:%M:%S)Tn"
printf "date - 60min: %(%m/%d/%Y -%H:%M:%S)Tn" $(( $(printf "%(%s)T") - 60 * 60 ))
The $(printf "%(%s)T")
prints the epoch seconds, the $(( epoch - 60*60 ))
is bash-aritmetics - subtracting 1hour in seconds. Prints:
current date: 04/20/2017 -18:14:31
date - 60min: 04/20/2017 -17:14:31
date -v-60M "+%m/%d/%Y -%H:%M:%S"
DATE=`date -v-60M "+%m/%d/%Y -%H:%M:%S"`
If you have bash version 4.4+
you can use bash's internal date printing and arithmetics:
printf "current date: %(%m/%d/%Y -%H:%M:%S)Tn"
printf "date - 60min: %(%m/%d/%Y -%H:%M:%S)Tn" $(( $(printf "%(%s)T") - 60 * 60 ))
The $(printf "%(%s)T")
prints the epoch seconds, the $(( epoch - 60*60 ))
is bash-aritmetics - subtracting 1hour in seconds. Prints:
current date: 04/20/2017 -18:14:31
date - 60min: 04/20/2017 -17:14:31
edited Apr 20 '17 at 16:18
answered May 9 '11 at 21:11
jm666jm666
43.6k1175144
43.6k1175144
yes my OS not support it, thanks any way
– shamir chaikin
May 11 '11 at 5:30
This doesn't work indate
shipped on Ubuntu.
– Flimm
Jun 15 '16 at 16:10
@Flimm This works on OS X and I expect BSD as well
– Jason S
Oct 14 '16 at 1:04
add a comment |
yes my OS not support it, thanks any way
– shamir chaikin
May 11 '11 at 5:30
This doesn't work indate
shipped on Ubuntu.
– Flimm
Jun 15 '16 at 16:10
@Flimm This works on OS X and I expect BSD as well
– Jason S
Oct 14 '16 at 1:04
yes my OS not support it, thanks any way
– shamir chaikin
May 11 '11 at 5:30
yes my OS not support it, thanks any way
– shamir chaikin
May 11 '11 at 5:30
This doesn't work in
date
shipped on Ubuntu.– Flimm
Jun 15 '16 at 16:10
This doesn't work in
date
shipped on Ubuntu.– Flimm
Jun 15 '16 at 16:10
@Flimm This works on OS X and I expect BSD as well
– Jason S
Oct 14 '16 at 1:04
@Flimm This works on OS X and I expect BSD as well
– Jason S
Oct 14 '16 at 1:04
add a comment |
if you need substract with timestamp :
timestamp=$(date +%s -d '1 hour ago');
add a comment |
if you need substract with timestamp :
timestamp=$(date +%s -d '1 hour ago');
add a comment |
if you need substract with timestamp :
timestamp=$(date +%s -d '1 hour ago');
if you need substract with timestamp :
timestamp=$(date +%s -d '1 hour ago');
answered Dec 4 '15 at 15:15
leGabianleGabian
412
412
add a comment |
add a comment |
Convert to timestamp (a long integer), subtract the right number of milliseconds, reformat to the format you need.
Hard to give more details since you don't specify a programming language...
ahh.. i am using shell script, my formula go like this -DATE=date "+%m/%d/%Y -%H:%M:%S"
and from DATE i want to subtract 1 hour
– shamir chaikin
May 9 '11 at 8:32
1
Are you required to stay in shell? An inline awk or perl script could easily subtract an hour and output in your required format.
– CoreyStup
May 9 '11 at 20:38
add a comment |
Convert to timestamp (a long integer), subtract the right number of milliseconds, reformat to the format you need.
Hard to give more details since you don't specify a programming language...
ahh.. i am using shell script, my formula go like this -DATE=date "+%m/%d/%Y -%H:%M:%S"
and from DATE i want to subtract 1 hour
– shamir chaikin
May 9 '11 at 8:32
1
Are you required to stay in shell? An inline awk or perl script could easily subtract an hour and output in your required format.
– CoreyStup
May 9 '11 at 20:38
add a comment |
Convert to timestamp (a long integer), subtract the right number of milliseconds, reformat to the format you need.
Hard to give more details since you don't specify a programming language...
Convert to timestamp (a long integer), subtract the right number of milliseconds, reformat to the format you need.
Hard to give more details since you don't specify a programming language...
answered May 9 '11 at 8:29
PhiLhoPhiLho
35.1k379122
35.1k379122
ahh.. i am using shell script, my formula go like this -DATE=date "+%m/%d/%Y -%H:%M:%S"
and from DATE i want to subtract 1 hour
– shamir chaikin
May 9 '11 at 8:32
1
Are you required to stay in shell? An inline awk or perl script could easily subtract an hour and output in your required format.
– CoreyStup
May 9 '11 at 20:38
add a comment |
ahh.. i am using shell script, my formula go like this -DATE=date "+%m/%d/%Y -%H:%M:%S"
and from DATE i want to subtract 1 hour
– shamir chaikin
May 9 '11 at 8:32
1
Are you required to stay in shell? An inline awk or perl script could easily subtract an hour and output in your required format.
– CoreyStup
May 9 '11 at 20:38
ahh.. i am using shell script, my formula go like this -DATE=
date "+%m/%d/%Y -%H:%M:%S"
and from DATE i want to subtract 1 hour– shamir chaikin
May 9 '11 at 8:32
ahh.. i am using shell script, my formula go like this -DATE=
date "+%m/%d/%Y -%H:%M:%S"
and from DATE i want to subtract 1 hour– shamir chaikin
May 9 '11 at 8:32
1
1
Are you required to stay in shell? An inline awk or perl script could easily subtract an hour and output in your required format.
– CoreyStup
May 9 '11 at 20:38
Are you required to stay in shell? An inline awk or perl script could easily subtract an hour and output in your required format.
– CoreyStup
May 9 '11 at 20:38
add a comment |
This work on my Ubuntu 16.04 date
:
date --date="@$(($(date +%s) - 3600))" "+%m/%d/%Y -%H:%M:%S"
And the date
version is date (GNU coreutils) 8.25
add a comment |
This work on my Ubuntu 16.04 date
:
date --date="@$(($(date +%s) - 3600))" "+%m/%d/%Y -%H:%M:%S"
And the date
version is date (GNU coreutils) 8.25
add a comment |
This work on my Ubuntu 16.04 date
:
date --date="@$(($(date +%s) - 3600))" "+%m/%d/%Y -%H:%M:%S"
And the date
version is date (GNU coreutils) 8.25
This work on my Ubuntu 16.04 date
:
date --date="@$(($(date +%s) - 3600))" "+%m/%d/%Y -%H:%M:%S"
And the date
version is date (GNU coreutils) 8.25
answered Mar 21 '17 at 12:14
ray_gray_g
265
265
add a comment |
add a comment |
If you need change timezone before subtraction with new format too:
$(TZ=US/Eastern date -d '1 hour ago' '+%Y-%m-%d %H:%M')
add a comment |
If you need change timezone before subtraction with new format too:
$(TZ=US/Eastern date -d '1 hour ago' '+%Y-%m-%d %H:%M')
add a comment |
If you need change timezone before subtraction with new format too:
$(TZ=US/Eastern date -d '1 hour ago' '+%Y-%m-%d %H:%M')
If you need change timezone before subtraction with new format too:
$(TZ=US/Eastern date -d '1 hour ago' '+%Y-%m-%d %H:%M')
answered May 24 '18 at 10:37
Kenan DumanKenan Duman
544
544
add a comment |
add a comment |
Here another way to subtract 1 hour.
yesterdayDate=`date -d '2018-11-24 00:09 -1 hour' +'%Y-%m-%d %H:%M'`
echo $yesterdayDate
Output:
2018-11-23 23:09
I hope that It can help someone.
Best Regards!
add a comment |
Here another way to subtract 1 hour.
yesterdayDate=`date -d '2018-11-24 00:09 -1 hour' +'%Y-%m-%d %H:%M'`
echo $yesterdayDate
Output:
2018-11-23 23:09
I hope that It can help someone.
Best Regards!
add a comment |
Here another way to subtract 1 hour.
yesterdayDate=`date -d '2018-11-24 00:09 -1 hour' +'%Y-%m-%d %H:%M'`
echo $yesterdayDate
Output:
2018-11-23 23:09
I hope that It can help someone.
Best Regards!
Here another way to subtract 1 hour.
yesterdayDate=`date -d '2018-11-24 00:09 -1 hour' +'%Y-%m-%d %H:%M'`
echo $yesterdayDate
Output:
2018-11-23 23:09
I hope that It can help someone.
Best Regards!
answered Nov 24 '18 at 21:29
Javier MuñozJavier Muñoz
3311518
3311518
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5934394%2fsubtract-1-hour-from-date-in-unix-shell-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown