Convert linux uptime to well format date

5,099

Solution 1

You may get it for free from the output of last reboot:

$ last reboot
reboot   system boot  4.14.81-i7       Sat Nov 17 23:25   still running
reboot   system boot  4.14.80-i7       Fri Nov 16 09:16 - 15:49  (06:33)

$ printf "On since: "; last reboot | grep "still running" | cut -c 40-56
On since: Sat Nov 17 23:25 

$ printf "On since: " ; last reboot --time-format iso | grep "still running" | cut -c 40-49
On since: 2018-11-17

Your uptime command might also have the -s option:

$ uptime -s
2018-11-17 23:25:23

Since this format is acceptable to date -d, you can reformat the time however you wish like this::

$ date -d "$(uptime -s)" "+On since: %d:%m:%y"
On since: 17:11:18

Solution 2

Single command.

$ date -r /proc/1 '+The computer is on since %d-%b-%y'
The computer is on since 09-Oct-18
$
Share:
5,099

Related videos on Youtube

ZPUFF19
Author by

ZPUFF19

Updated on September 18, 2022

Comments

  • ZPUFF19
    ZPUFF19 almost 2 years

    I want to convert uptime to date DD:MM:YY without the | and I want to put a string like "the computer is on since 23-feb-16"

    • Lewis M
      Lewis M over 5 years
      What is the output of your uptime command? What distro are you using? On Ubuntu, uptime outputs something like "16:25:06 up 47 days, 8:50, 2 users, load average: ..." The "16:25:06" is the time. The "up 47 days, 8:50" is up 47 days, 8 hours and 50 minutes" So, are you wanting to do a date/time calculation on the "47 days, 8:50" or is the output of your uptime different? Could you edit your question to show the output of the uptime command you are using?
    • Jeff Schaller
      Jeff Schaller over 5 years
      do you like uptime -p ?