Unix date tool

date is the Unix tool to check both the date and the time from the shell in Linux, Cygwin and other Unix-like systems. There is no separate tool to show the time. date is part of the GNU Coreutils package.

  • By default, the tool displays the local time in your local format. On my computer, the format was like this:
$ date
Mon, Jan 21, 2019  8:42:23 AM
  • To view the UTC time:
$ date -u
  • To view the modification time of a file:
$ date -r foobar.txt

How to change time style used by ls

The date and time displayed by ls -l can be changed to any format you want.

The default is:

$ ls -l
total 88
-rw-rw-r-- 1 joe joe     6 Jan 27 12:23 a.txt
-rw-rw-r-- 1 joe joe   522 Jan 27 12:22 haha.txt
-rw-rw-r-- 1 joe joe 80360 Jan 27 12:23 jump.pdf

To change the format of the date and time use the --time-style option. Values that can be passed to it are long-iso, full-iso and iso. You can also specify your own custom format as a string.

Here is how the different time styles look:

$ ls -l --time-style=long-iso
total 88
-rw-rw-r-- 1 joe joe     6 2015-01-27 12:23 a.txt
-rw-rw-r-- 1 joe joe   522 2015-01-27 12:22 haha.txt
-rw-rw-r-- 1 joe joe 80360 2015-01-27 12:23 jump.pdf

$ ls -l --time-style=full-iso
total 88
-rw-rw-r-- 1 joe joe     6 2015-01-27 12:23:08.309375574 +0800 a.txt
-rw-rw-r-- 1 joe joe   522 2015-01-27 12:22:59.773375414 +0800 haha.txt
-rw-rw-r-- 1 joe joe 80360 2015-01-27 12:23:32.693376032 +0800 jump.pdf

$ ls -l --time-style=iso
total 88
-rw-rw-r-- 1 joe joe     6 01-27 12:23 a.txt
-rw-rw-r-- 1 joe joe   522 01-27 12:22 haha.txt
-rw-rw-r-- 1 joe joe 80360 01-27 12:23 jump.pdf

Tried with: Ubuntu 14.04

How to get date and time in C++

In C++11 and later versions, date and time is supported in the standard library through the chrono header file. To print out the date and time, you may still have to fall back on the std::time_t and std::tm structures and std::ctime and std::localtime functions from the ctime header file and std::put_time from iomanip header file.

The code sample below shows these in action:

Tried with: Visual C++ 2013

C++: Print Current Date and Time

The easiest way to obtain the date and time string in C++ is to use time, localtime_s and asctime_s. The output string is of the formΒ Tue Aug 02 15:11:23 2011.

Here is the self-explanatory code:

#include <ctime>

time_t curTime;
struct tm locTime;
const int TimeStrLen = 26;
char timeStr[ TimeStrLen ];

if (    ( -1 != time( &curTime ) )                          // Seconds since 01-01-1970
    &&  ( 0 == localtime_s( &locTime, &curTime ) )          // Convert to local time
    &&  ( 0 == asctime_s( timeStr, TimeStrLen, &locTime ) ) // Convert to string
    cout << "Date-time is: " << timeStr;
    cerr << "Error calculating date-time!" << endl;
    exit( 1 );

How to change the date and time format of clock in panel of Ubuntu


The Clock on the Panel in Ubuntu displays the time. You can enable it to display the date and day of the week by changing its settings. It displays this information in this format:

Fri Apr 2 02:10 PM

I prefer the following format for date and time display:

2010-04-02 Fri 14:10

The date and time format can be changed to anything that you are comfortable with.

Ubuntu 12.04 and later

Changing date-time format in Ubuntu
Changing date-time format in Ubuntu

Here is how to change the date and time format of the panel clock to your liking:

  1. Install the package dconf-tools from the Ubuntu repositories. This installs the dconf-editor which is needed for changing the configuration.

  2. Open dconf editor from the Dash or dconf-editor from the shell. In the editor, navigate to com.canonical.indicator.datetime.

  3. Change the time-format field to custom.

  4. In the custom-time-format field, change the value to the desired format. The format can include both the date and time specifiers. This format uses the same specifiers as that of the C standard library function strftime().

To change to my desired format, I use:

%Y-%m-%d %a %H:%M
2010-04-02 Fri 14:10

The changes should take effect immediately and the date-time should change in the panel.

Tried with: Ubuntu 12.04 and Ubuntu 14.04

Ubuntu 10.04 and older

Use gconf-editor. Navigate to /apps/panel/applets/clock_screen0/prefs. Change format field to the string custom. Change the custom_format field as described above.