0

How do you format the date time output so that 11:30 is printed as 11.5?

  • 3
    Hi, what have you tried so far ? – steve Mar 1 at 16:50
  • 3
    you want every possible value of minutes to be truncated to one decimal point? Rounded? – Jeff Schaller Mar 1 at 17:33
3

Here's one approach.

$ date
Fri Mar  1 19:30:36 STD 2019
$ date '+%H %M' | awk '{ printf "%d.%d\n",$1,$2/60*10 }'
19.5
$

Shorter version

$ date '+%H %M'|awk '{$0=$1"."int($2/6)}1'
19.5
$
1

A simple solution, using the 12-hour clock, but not including the requisite “AM” / “PM” indicator, is

date +'scale=1; %I + %M/60' | bc

Of course you can capture the output in a variable:

dec_time=$(date +'scale=1; %I + %M/60' | bc)

This simply executes the calculator (bc) with an expression that is hours (%I) plus a fraction of the hour (minutes divided by 60).  scale=1 specifies that you want one decimal digit.  For the 24-hour clock (hour ranges from 0 through 23), use %H instead of %I.

This will not give you any padding (leading zero or space) for hours < 10, and the division truncates down (11:35 will be reported as “11.5”, not “11.6”).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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