2

I am trying to convert some future date to epoch number on Ubuntu Mate 20.04.

So I decide to write this code:

daysAmount="10"   
endDate=$(date +"%b %_d %Y" -d "+$daysAmount days")   
epochNum=$(date -d "$endDate" +%s)  

The first two lines are working well, but I'm not getting anything on epochNum=.

Is it OK to write another variable inside $(date) like I have? If not, how can I make this thing work?

I also tried endDate=$(date +"%b %_d %Y" -d "+$daysAmount days" +%s) but that did not work either.

8
  • 2
    What operating system are you using? I am guessing Linux based on your date format, but please edit your question and clarify that. Anyway, the second line should give an error. Are you saying you don't get date: extra operand ‘+%s’?
    – terdon
    Nov 1, 2021 at 11:47
  • Your second line gives me: date: illegal time format - on OS X Nov 1, 2021 at 11:55
  • @Greenonline this can't be OSX or macOS because that uses BSD date which doesn't not support -d. Which of course just highlights why it is important to always mention your OS.
    – terdon
    Nov 1, 2021 at 12:00
  • @terdon i edited the code and the question. I forgot to remove that +%s from my second line. Now the code is how i run it
    – user499362
    Nov 1, 2021 at 12:02
  • That code works exactly as expected. What is it that doesn't work for you? What happens if you copy/paste the exact code block from your question into a terminal and then run echo $epochNum? Is this maybe part of a larger script or a loop and you are running the echo $epochNum outside it?
    – terdon
    Nov 1, 2021 at 12:06

2 Answers 2

1

You can run commands with a temporary locale. This should solve your issue:

daysAmount="10"   
endDate=$(LC_ALL=C date +"%b %_d %Y" -d "+$daysAmount days")   
epochNum=$(LC_ALL=C date -d "$endDate" +%s)  
1
  • That work as a charm! Thanx allot!
    – user499362
    Nov 1, 2021 at 13:05
0

Manage to make it work.

Check your date language before converting to epoch number. If your name of the mount is different from the us alphabet like the example of "date" command here:

1.11.2021 (пн) 14:47:59 EET

The conversion will not work.

In different machine where "date" shows like this (us alphabet):

Mon Nov  1 14:47:45 EET 2021

the code was working perfectly fine


For a working example on machines with different languages see: https://unix.stackexchange.com/a/675661/499362

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .