How to change date in linux

How can I change the date and time on Ubuntu?

When I go to change the date and time it doesn’t work. I try to unlock but it just won’t unlock. If there is any way to make it work please help. I know it isn’t very descriptive but I really would appreciate some answers.

7 Answers 7

-s, --set=STRING set time described by STRING 

To change date, use the command in terminal,

For example, to change date to 25 Sep 2013 15:00, the command would be,

sudo date --set "25 Sep 2013 15:00:00" 

On 16.04 LTS, in Time & Date (System Settings), set the time manually. If not, the command is executed, but the date and the time are not changed.

not working. set date by sudo date —set=STRING result printed that date has changed but actually date is not changed.

sudo dpkg-reconfigure tzdata 

This both lets you configure your timezone (you may just pick the same), and automagically synchronizes your time.

If there’s no tzdata package, install it:

sudo apt-get update sudo apt-get install tzdata 

In 12.04 I don’t have to Unlock anything. Just click on the clock on the top bar, and choose Time & Date Settings, once the Time & Date window opens, choose Manually, so you can change the time and date manually; otherwise choose your time zone from the map, and choose Automatic.

enter image description here

enter image description here

I am not able to change my system’s time this way. I can change the time from the settings but it is not reflecting on my top bar.

You’ll have to change the focus of the Date spinner control to have the date changed immediately. Very odd 🙂 (Ubuntu 14.10)

sometimes to reflect the settings changes you need to kill unity panel service and restart it. Click clock on the top bar, and choose Time & Date Settings once the Time & Date window opens choose Manually so you can change the time and date manually; otherwise choose your time zone from the map, and choose Automatically. Now to reflect changes instantly open terminal and type killall unity-panel-service

Источник

How to Change the Linux Date and Time: Simple Commands

Telling the time on Linux is more complicated than it might seem at first glance. To start with, the time command on Linux doesn’t tell the time:

Prague clock$ time 
real 0m0.000s
user 0m0.000s
sys 0m0.000s

Because time is a timer for measuring how long a process runs. For example, how long does it take to recursively list all the files in a directory?

$ time ls -Rl dir/* 
[. ]
real 0m22.156s
user 0m1.652s
sys 0m4.772s

Date for Time

It may sound odd, but you must use the date command to see the time on Linux:

$ date 
Thu Oct 11 11:47:25 PDT 2012

The date command is fundamental to understanding time options on Linux. For example, the panel clock in Xfce4 supports using the standard date options to customize the date and time display. Figure 1 shows what mine looks like.

Читайте также:  Консультант плюс запуск на линукс

This comes from these FORMAT options for the date command: %r %n%a %b %d, %Y , which you can easily test for yourself:

$ date +"%r %n%a %b %d, %Y" 
12:05:00 PM
Thu Oct 11, 2012

man date details all the formatting options. Note how you can use ordinary spacing and punctuation to control the appearance. You customize date and time displays to suit your own whims, and in consistent, script-friendly ways.

man date lists a good set of options, but it does not tell you everything. To get the complete story of date you need the GNU coreutils manual. And that is where we learn about the magic strings that let us ask for dates next week, last year, day of week, and many more. Like the date three Tuesdays from now, five months from now, eight years ago:

$ date -d "third tuesday" 
Tue Oct 30 00:00:00 PDT 2012
$ date -d "fifth month"
Mon Mar 11 14:02:54 PDT 2013
$ date -d "8 years ago"
Mon Oct 11 14:03:32 PDT 2004
$ date -d "23 years ago 2 months 19 days 17 hours 59 minutes"
Sun Dec 31 06:48:14 PST 1989

You can quickly check the time in a different time zone:

UTC (Coordinated Universal Time) is the universal standard for time worldwide. When you know your UTC offset you always know what time it is, because date will tell you:

$ date -R 
Thu, 11 Oct 2012 13:56:17 -0700

If you live in one of those demented regions that uses Daylight Savings Time, date -R will always tell you the correct offset no matter what time of year it is.

Cal For Dates

When you just want to see some dates, think of our good old friend cal :

$ cal 
October 2012
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

cal -3 displays three months: last month, this month, and next month. cal YYYY displays a specific year, like cal 1962 .

ncal is included on most Linux systems, and it is an oldtimer designed to fit nicely on a 25×80 terminal:

$ ncal 
October 2012
Su 7 14 21 28
Mo 1 8 15 22 29
Tu 2 9 16 23 30
We 3 10 17 24 31
Th 4 11 18 25
Fr 5 12 19 26
Sa 6 13 20 27

ncal -b switches to our customary horizontal display. ncal will show an arbitrary number of months in the past or future. For example ncal -bB 6 displays the current month plus six months previous, and ncal -bA 6 display the current month plus the next six months.

You can see any month in any year, for example March 1950, with ncal -bm March 1950 . This works for future months, too.

Those Wacky ls Timestamps

The way the ls outputs the date and time is a continual source of vexation because it varies on the different Linux distributions. This is how it looks on my Linux Mint system:

$ ls -l 
-rw-r----- 1 carla carla 11537 Oct 1 17:16 hp-check.log
-rw-r--r-- 1 carla carla 705 Aug 12 2011 ledproject.txt

Files dated within the last six months display the time instead of the year, and older files display the year and not the time. The Mint time style is called iso , and this is the the GNU default. long-iso is my preference, and it looks like this:

$ ls -l --time-style=long-iso 
-rw-r----- 1 carla carla 11537 2012-10-01 17:16 hp-check.log
-rw-r--r-- 1 carla carla 705 2011-08-12 12:15 ledproject.txt

I like long-iso because it’s easy to sort– year, month, day, single-digit months and days are padded to two digits, and it uses a 24-hour clock. This is all defined in ISO 8601. On Linux it’s controlled by the TIME_STYLE environment variable, so you can override the default system-wide in /etc/profile , or in your personal .profile or .bashrc by adding a line like export TIME_STYLE=long-iso , then logging out and back in.

Читайте также:  What is gedit in linux

You might want to create a custom timestamp. This uses the same options as the date command, and you can test this on the command line before making it permanent in a configuration file:

$ export TIME_STYLE="+%Y-%m-%d %H:%M:%S %z" 
$ ls -l -rw-r----- 1 carla carla 11537 2012-10-01 17:16:45 -0700 hp-check.log
-rw-r--r-- 1 carla carla 705 2011-08-12 12:15:02 -0700 ledproject.txt

The GNU manual spells all this out in plain English. You can experiment to your heart’s content, and then log out and log back in to reset to your system default.

And that is just the beginning of telling time on Linux. My dream is a lifestyle that doesn’t need clocks at all, but I haven’t figured out how to do that in Linux.

Источник

Date Command in Linux: How to Set, Change, Format and Display Date

Linux date command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.

Read on to learn how to use the date command in Linux.

How to use Linux date command

  • A system running Linux
  • A user account with root privileges
  • Access to a terminal window/command line

Linux date Command Syntax

The syntax for the date command is:

How to Use date Command in Linux

To show the current system time and date, type in the date command:

Date command in linux

The output displays the day of the week, day of the month, month, year, current time, and time zone. By default, the date command is set to the time zone of the operating system.

The -d option allows users to operate on a specific date. For example, we can type in the following command:

See an older date

You can use the —date command to display the given date string in the format of a date. This command does not affect the system’s actual date and time values, and it only prints the requested date. For example:

Pull a date from a string

Note: Learn how can you create a script using the printf command to display the current date.

Linux date Command Format Options

To format the date command’s output, you can use control characters preceded by a + sign. Format controls begin with the % symbol and are substituted by their current values.

Here, the %Y character is replaced with the current year, %m with month, and %d with the day of the month:

date +"Year: %Y, Month: %m, Day: %d"

Format the current date

Here are another two formatting examples:

Another date format example

date +"Week number: %V Year: %y"

Another formatting example

These are the most common formatting characters for the date command:

    • %D – Display date as mm/dd/yy
    • %Y – Year (e.g., 2020)
    • %m – Month (01-12)
    • %B – Long month name (e.g., November)
    • %b – Short month name (e.g., Nov)
    • %d – Day of month (e.g., 01)
    • %j – Day of year (001-366)
    • %u – Day of week (1-7)
    • %A – Full weekday name (e.g., Friday)
    • %a – Short weekday name (e.g., Fri)
    • %H – Hour (00-23)
    • %I – Hour (01-12)
    • %M – Minute (00-59)
    • %S – Second (00-60)

    To see all formatting options, run date —help or the man command man date in your terminal.

    Set or Change Date in Linux

    To change the system clock manually, use the set command. For example, to set the date and time to 5:30 PM, May 13, 2010, type:

    Manual time set in Linux

    Most Linux distributions have the system clock synchronized using the ntp or the systemd-timesyncd services, so be careful when the setting the clock manually.

    Display Past Dates

    Use the —date option to display past dates in Linux. The date command accepts values such as «tomorrow» , «Friday» , «last Friday» , «next Friday» , «next week» , and similar. So, use the following strings to print past dates::

    Format to

    Yesterday

    Time ten seconds ago format in linux

    Display Future Dates

    The —date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:

    Date next monday format in linux

    Date four days from now format in linux

    Tomorrow

    Display the Date String at Line of File

    The —file option prints the date string present at each line of the file. Unlike the —date option, —file can present multiple date strings at each line.

    This is the syntax for the —file command:

    Here we use the cat command to add dates to a file and then print them with the date command:

    Display dates at each file line in linux

    Display Last Modified Timestamp of a Date File

    When you use the -r option, the date command prints the last modification time of a file. For example, the following command prints the last time the hosts file was changed:

    The last date the file was modified in linux

    Override a Time Zone

    By default, the date command uses the time zone defined in /etc/localtime . To use a different time zone in the environment, set the TZ variable to the desired time zone.

    For example, to switch to New York time, enter:

    Switch to New York time zone in Linux

    Type in the date command to return the system to its default time zone. To see all available time zones, use the timedatectl list-timezones command.

    The date command can also show the local time for a different time zone. For example, to display the local time for 4:30 PM next Monday on the Australian east coast, type:

    date -d 'TZ="Australia/Sydney" 04:30 next Monday'

    Switch to South Australian time in Linux

    Use date with Other Commands

    You can use the date command to create file names that contain the current time and date. The input below creates a backup MySQL file in the format of the current date:

    mysqldump database_name > database_name-$(date +%Y%m%d).sql

    Another common use of the date command is in shell scripts. Below we assign the output of date to the date_now variable:

    Echo the date now variable

    Use Unix Epoch Time (Epoch Converter)

    You can use the date command as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.

    To show the number of seconds from the epoch to the current day, use the %s format control:

    Epoch seconds in linux

    To see how many seconds passed from epoch to a specific date, enter:

    Epoch seconds from a specific date in Linux

    You now have a good understanding of how to use the date command in Linux. If you are interested in more date/time configuration options for Linux, read How to Set or Change Timezone/Date/Time on Ubuntu.

    Источник

Оцените статью
Adblock
detector