Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Shell

How can I write a shell script, using only shell features (and the date command), to output the number of minutes that have elapsed in the current day?

share|improve this question

Since date treats non-specifiers as literals you could use the format string to construct a simple arithmetic expression for the current number of minutes, and then evaluate the result using the shell's built-in arithmetic. For example, in bash

printf "$(( $(date '+%H * 60 + %M') ))\n"

If your shell does not support arithmetic, you could use an external calculator such as bc

date '+%H * 60 + %M' | bc

or dc

date '+%H 60 * %M + p' | dc
share|improve this answer

GNU date and bash:

daystart_sec="$(date --date="today 00:00:00" +%s)"
now_sec="$(date +%s)"
echo $(((now_sec-daystart_sec)/60))
share|improve this answer

how do I subtract the number of minutes passed in the day from the number of minutes left in the day (i.e., 1440)? So basically I want to subtract the output of the above from 1440.

share|improve this answer
    
This does not provide an answer to the question. Please, read the FAQ to learn how to ask questions properly. – zuazo Dec 22 '16 at 2:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.