989

I have to check the time in a Linux terminal.

What is the command for getting date and time in a Linux terminal?

Is there a way in which we can set a custom function?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
harshal
  • 10,252
  • 3
  • 18
  • 23
  • 62
    `date` ???, see `man date` – Andrzej Jozwik Aug 27 '13 at 06:55
  • just type [root@msr ~]$date its shows current time. – madhu Dec 10 '14 at 13:44
  • 27
    This question seems on topic as it relates to a specific computing task on a specific operating system (which is just a software layer itself). As, @drstevens said, this is the first result in Google and it was informative for me needs. If it were off-topic, it would read something like "Do operating systems tell time?" or "What do you guys think about operating systems that tell time." Stack Overflow is a great resource for beginners and it seems like this was closed as for being too beginner of a question. It should instead just be listed as a _beginner_ question. – Andrew Nov 13 '15 at 00:41
  • 73
    StackExchange is the place for answers. This is a good question. Perhaps the question should be migrated, but it should not be closed, in my opinion. This question appeared first in my Google results when I searched for "linux check time". – Dan Nissenbaum Feb 08 '16 at 17:47
  • 1
    Stack Overflow is not the place for answers for questions about general computing hardware and software. – Jeffrey Bosboom Feb 16 '16 at 03:11
  • 2
    how do I know the time in a different **time zone**? Oh, `man date` shows this: `tzselect` (to find zones), and then: `TZ='Europe' date` – nilon May 30 '17 at 19:03
  • 11
    Can someone maybe simply move the question, instead of closing it and complaining? – Egor Hans Nov 11 '17 at 15:34
  • 4
    `Stack Overflow is not the place for answers for questions about general computing hardware and software.` Well, it is now, since every search on Google of this type leads to Stack Overflow! I really don't understand where this movement comes from, but trying to toss this huge knowledge base seems pretty crazy. It's like if Wikipedia now decided they don't want and started to massively delete them. – Natix Mar 26 '18 at 16:26
  • 61
    Hi. This is a developer from the future. I am from the year 2018, and I arrived here because I googled "unix get current time" because although I suspected it was "date", it is still early in the morning and it was easier to google than guessing or googling for Unix documentation and then trying to find it in there. Btw - this is now the top hit in google when you search for how to do this. I hope people on Stack Overflow nowadays realize the importance of keeping all questions around, no matter how "stupid" they might seem. – the-nick-wilson Jul 24 '18 at 13:32
  • 1
    date displays time. awesome, isn't it? – Alexey Sh. Jul 22 '20 at 14:16
  • 1
    and time displays 0 unless you run something with in it :) – Wilf Jul 23 '20 at 06:14
  • 1
    I believe at some point when you are getting older like 69 years old. you might google this thing up again. – MaXi32 Jul 30 '20 at 07:37

2 Answers2

1367

The command is date

To customise the output there are a myriad of options available, see date --help for a list.

For example, date '+%A %W %Y %X' gives Tuesday 34 2013 08:04:22 which is the name of the day of the week, the week number, the year and the time.

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
104

You can use date to get time and date of a day:

$ date
Tue Aug 27 15:01:27 CST 2013

Also hwclock would do:

$ hwclock
Tue 27 Aug 2013 03:01:29 PM CST  -0.516080 seconds

For customized output, you can either redirect the output of date to something like awk, or write your own program to do that.

Remember to put your own executable scripts/binary into your PATH (e.g. /usr/bin) to make it invokable anywhere.

Shahrad Elahi
  • 774
  • 12
  • 22
starrify
  • 14,307
  • 5
  • 33
  • 50
  • 3
    Another way: root@linux 17:32:02 /linux >cat /proc/driver/rtc rtc_time : 23:38:24 rtc_date : 2014-07-10 – Lunar Mushrooms Jul 11 '14 at 06:40
  • 15
    @pzkpfw because it's not a programming question. It's a linux question, which means it should be on something like SuperUser like some similar questions such as http://superuser.com/questions/309034/how-to-check-which-timezone-in-linux. If the question were about getting current date and time on Linux using Java, C++, etc., then it would be more appropriate for SO. – Jeutnarg Feb 24 '16 at 17:38