1

anyone has any idea how do i convert this command available at How do I get the difference between two dates under bash from a bash to a zsh command

Marlon Richert
  • 5,250
  • 1
  • 18
  • 27
  • The bash command posted is invalid, as you can see if you just copy and paste what you have posted into a bash shell. Are you seriously asking how to convert an incorrect bash command into a incorrect zsh command? – user1934428 Jun 10 '21 at 06:45
  • https://stackoverflow.com/questions/9008824/how-do-i-get-the-difference-between-two-dates-under-bash/9008871#9008871 – aayush jangid Jun 10 '21 at 07:00
  • This is the same buggy command as before. It gives me a _bash: let: DIFF=(date +%s -d 20120203-date +%s -d 20120115)/86400: syntax error: operand expected (error token is "%s -d 20120203-date +%s -d 20120115)/86400")_ . Please upload a screenshot so that we can see what this command does in your bash. – user1934428 Jun 10 '21 at 07:01
  • The command you linked to is different from the one you posted..... – user1934428 Jun 10 '21 at 07:02
  • thats because of those symbols which enclose the dates – aayush jangid Jun 10 '21 at 07:03
  • In your question, don't use backquotes, but just type 4 leading spaces in front of the command. This is described [here](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks). – user1934428 Jun 10 '21 at 07:07
  • In a comment, type a baskslash in front of a backquote which is supposed to show up as code. – user1934428 Jun 10 '21 at 07:09
  • i will add it ,but do you know how can i convert it into zsh command – aayush jangid Jun 10 '21 at 07:11
  • See my answer. No conversion needed. – user1934428 Jun 10 '21 at 07:13

2 Answers2

1

Presuming you need to do this more than once, I would make a reusable function for this.

  1. Put this in a file called duration-in-days:
    autoload -Uz calendar_scandate
    local start end
    calendar_scandate $1        
    start=$REPLY        
    calendar_scandate $2        
    end=$REPLY        
    print $(( ( end - start ) / ( 24 * 60 * 60 ) ))
    
  2. In your .zshrc file, autoload your function. For example, if you keep your function files in a dir ~/functions:
    # Autoload all functions in your ~/functions dir.
    autoload -Uz ~/functions/*~*.zwc
    # Exclude .zwc files (generated when you compile functions).
    
  3. Restart your terminal or shell.

Now you can use the function above like this:

% duration-in-days 2012-01-15 2012-02-03
19
%

If you want to use this function in an executable script, then remember to autoload it in your script.

Marlon Richert
  • 5,250
  • 1
  • 18
  • 27
0

While bash and zsh are fairly different, they are similar enough that in your case, that the same command works for both:

((DIFF=($(date +%s -d 20120203)-$(date +%s -d 20120115))/86400))
user1934428
  • 19,864
  • 7
  • 42
  • 87