conversion of date into seconds in unix
Clash Royale CLAN TAG#URR8PPP
I have a requirement where I will be give the time in below format
2019-02-08T19:24:30.220Z
by this I need to output number of days between the given date and present date.
given date = 2019-02-08T19:24:30.220Z
present date = 2019-02-20T19:24:30.220Z
output = 12
shell-script osx date
add a comment |
I have a requirement where I will be give the time in below format
2019-02-08T19:24:30.220Z
by this I need to output number of days between the given date and present date.
given date = 2019-02-08T19:24:30.220Z
present date = 2019-02-20T19:24:30.220Z
output = 12
shell-script osx date
What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?
– Stéphane Chazelas
Feb 20 at 11:10
1
Your clock must be off as you posted your question at 2019-02-20T11:03:04Z
– Stéphane Chazelas
Feb 20 at 11:20
I am using Mac and shell is zsh
– Sugatur Deekshith S N
Feb 20 at 11:59
I've added a zsh solution to my answer.
– Stéphane Chazelas
Feb 20 at 12:28
add a comment |
I have a requirement where I will be give the time in below format
2019-02-08T19:24:30.220Z
by this I need to output number of days between the given date and present date.
given date = 2019-02-08T19:24:30.220Z
present date = 2019-02-20T19:24:30.220Z
output = 12
shell-script osx date
I have a requirement where I will be give the time in below format
2019-02-08T19:24:30.220Z
by this I need to output number of days between the given date and present date.
given date = 2019-02-08T19:24:30.220Z
present date = 2019-02-20T19:24:30.220Z
output = 12
shell-script osx date
shell-script osx date
edited Feb 20 at 12:36
roaima
45.6k757124
45.6k757124
asked Feb 20 at 11:03
Sugatur Deekshith S NSugatur Deekshith S N
416
416
What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?
– Stéphane Chazelas
Feb 20 at 11:10
1
Your clock must be off as you posted your question at 2019-02-20T11:03:04Z
– Stéphane Chazelas
Feb 20 at 11:20
I am using Mac and shell is zsh
– Sugatur Deekshith S N
Feb 20 at 11:59
I've added a zsh solution to my answer.
– Stéphane Chazelas
Feb 20 at 12:28
add a comment |
What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?
– Stéphane Chazelas
Feb 20 at 11:10
1
Your clock must be off as you posted your question at 2019-02-20T11:03:04Z
– Stéphane Chazelas
Feb 20 at 11:20
I am using Mac and shell is zsh
– Sugatur Deekshith S N
Feb 20 at 11:59
I've added a zsh solution to my answer.
– Stéphane Chazelas
Feb 20 at 12:28
What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?
– Stéphane Chazelas
Feb 20 at 11:10
What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?
– Stéphane Chazelas
Feb 20 at 11:10
1
1
Your clock must be off as you posted your question at 2019-02-20T11:03:04Z
– Stéphane Chazelas
Feb 20 at 11:20
Your clock must be off as you posted your question at 2019-02-20T11:03:04Z
– Stéphane Chazelas
Feb 20 at 11:20
I am using Mac and shell is zsh
– Sugatur Deekshith S N
Feb 20 at 11:59
I am using Mac and shell is zsh
– Sugatur Deekshith S N
Feb 20 at 11:59
I've added a zsh solution to my answer.
– Stéphane Chazelas
Feb 20 at 12:28
I've added a zsh solution to my answer.
– Stéphane Chazelas
Feb 20 at 12:28
add a comment |
1 Answer
1
active
oldest
votes
With ksh93
(often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh
of Solaris 11 and newer:
date=2019-02-08T19:24:30.220Z
export LC_ALL=C # to make sure the decimal radix is "."
then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
now_in_seconds=$(printf '%(%s.%N)Tn' now)
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
At 2019-02-20T11:17:30Z and a bit, that gave me:
Result: 11.6618110817684377
You can use $((f(difference_in_24h_periods)))
where f
is one of round
, floor
, ceil
, nearbyint
, trunc
, rint
, int
like in C if you want the difference as an integer, or use printf
format specifications to specify the number of significant digits.
With zsh
:
zmodload zsh/datetime
date=2019-02-08T19:24:30.220Z
TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
then_in_seconds+=.$$date##*.%Z
now_in_seconds=$EPOCHREALTIME
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f501818%2fconversion-of-date-into-seconds-in-unix%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
With ksh93
(often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh
of Solaris 11 and newer:
date=2019-02-08T19:24:30.220Z
export LC_ALL=C # to make sure the decimal radix is "."
then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
now_in_seconds=$(printf '%(%s.%N)Tn' now)
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
At 2019-02-20T11:17:30Z and a bit, that gave me:
Result: 11.6618110817684377
You can use $((f(difference_in_24h_periods)))
where f
is one of round
, floor
, ceil
, nearbyint
, trunc
, rint
, int
like in C if you want the difference as an integer, or use printf
format specifications to specify the number of significant digits.
With zsh
:
zmodload zsh/datetime
date=2019-02-08T19:24:30.220Z
TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
then_in_seconds+=.$$date##*.%Z
now_in_seconds=$EPOCHREALTIME
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
add a comment |
With ksh93
(often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh
of Solaris 11 and newer:
date=2019-02-08T19:24:30.220Z
export LC_ALL=C # to make sure the decimal radix is "."
then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
now_in_seconds=$(printf '%(%s.%N)Tn' now)
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
At 2019-02-20T11:17:30Z and a bit, that gave me:
Result: 11.6618110817684377
You can use $((f(difference_in_24h_periods)))
where f
is one of round
, floor
, ceil
, nearbyint
, trunc
, rint
, int
like in C if you want the difference as an integer, or use printf
format specifications to specify the number of significant digits.
With zsh
:
zmodload zsh/datetime
date=2019-02-08T19:24:30.220Z
TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
then_in_seconds+=.$$date##*.%Z
now_in_seconds=$EPOCHREALTIME
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
add a comment |
With ksh93
(often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh
of Solaris 11 and newer:
date=2019-02-08T19:24:30.220Z
export LC_ALL=C # to make sure the decimal radix is "."
then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
now_in_seconds=$(printf '%(%s.%N)Tn' now)
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
At 2019-02-20T11:17:30Z and a bit, that gave me:
Result: 11.6618110817684377
You can use $((f(difference_in_24h_periods)))
where f
is one of round
, floor
, ceil
, nearbyint
, trunc
, rint
, int
like in C if you want the difference as an integer, or use printf
format specifications to specify the number of significant digits.
With zsh
:
zmodload zsh/datetime
date=2019-02-08T19:24:30.220Z
TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
then_in_seconds+=.$$date##*.%Z
now_in_seconds=$EPOCHREALTIME
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
With ksh93
(often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh
of Solaris 11 and newer:
date=2019-02-08T19:24:30.220Z
export LC_ALL=C # to make sure the decimal radix is "."
then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
now_in_seconds=$(printf '%(%s.%N)Tn' now)
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
At 2019-02-20T11:17:30Z and a bit, that gave me:
Result: 11.6618110817684377
You can use $((f(difference_in_24h_periods)))
where f
is one of round
, floor
, ceil
, nearbyint
, trunc
, rint
, int
like in C if you want the difference as an integer, or use printf
format specifications to specify the number of significant digits.
With zsh
:
zmodload zsh/datetime
date=2019-02-08T19:24:30.220Z
TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
then_in_seconds+=.$$date##*.%Z
now_in_seconds=$EPOCHREALTIME
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"
edited Feb 20 at 15:08
answered Feb 20 at 11:14
Stéphane ChazelasStéphane Chazelas
310k57584945
310k57584945
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f501818%2fconversion-of-date-into-seconds-in-unix%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
What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?
– Stéphane Chazelas
Feb 20 at 11:10
1
Your clock must be off as you posted your question at 2019-02-20T11:03:04Z
– Stéphane Chazelas
Feb 20 at 11:20
I am using Mac and shell is zsh
– Sugatur Deekshith S N
Feb 20 at 11:59
I've added a zsh solution to my answer.
– Stéphane Chazelas
Feb 20 at 12:28