- How to change ubuntu’s server date and time via command line?
- 7 Answers 7
- Set Date in Linux from the Command Line
- Prerequisites
- Date Syntax
- Date
- User Manual Date Command
- Get Dates in Different Formats
- Get Day, Month, and Year
- The Month of the Year
- Get Dates of Past and Future
- Particular Day Ago
- Last Month
- Next Year
- Difference Between Dates
- Display Date with –d or –date Format
- Display Week Number in the Current Year
- Day Number in the Current Year
- Set Date According to Timezone
- Get Date and Time by Setting the Hardware Clock
- Conclusion
- About the author
- Aqsa Yasin
- Date Command in Linux: How to Set, Change, Format and Display Date
- Linux date Command Syntax
- How to Use date Command in Linux
- Linux date Command Format Options
- Set or Change Date in Linux
- Display Past Dates
- Display Future Dates
- Display the Date String at Line of File
- Display Last Modified Timestamp of a Date File
- Override a Time Zone
- Use date with Other Commands
- Use Unix Epoch Time (Epoch Converter)
How to change ubuntu’s server date and time via command line?
The Ubuntu server’s current date and time is different from the time zone date and time. I have tried using:
sudo date "30 Sep 2015 4:43:42"
to change it but it did not change the date and time, just printed on terminal the date and time I changed, but when I executed:
The date and time is still the old one. What is the correct way to change date and time of Ubuntu Server?
7 Answers 7
You can set the system date with this command:
sudo date --set="2015-09-30 10:05:59.990"
Then when using date , it should be showed correctly.
Now you should also the set hardware clock in the BIOS of the system, that the setting persists over a reboot (dureing the startup the system time is set to the value of the hardware clock). Do that with hwclock :
This gets the system clocks (sys) value and sets the hardware clock (hc). Check it with the hwclock command. Both hwclock and date should now show the same date and time.
To set your timezone, you can use this command:
sudo dpkg-reconfigure tzdata
BTW: If you use a this machine as a server, I strongly recommend using an NTP-Client to sync the time over network. So you can guarantee that all your servers have the exactly same time set. This will sync the time while the machine runs. If you have applications which are dependent of synced time over server, I recommend the NTP-Daemon. The longer it runs in the background, the more precise is the time.
Set Date in Linux from the Command Line
The date command depicts the date and time of the system. It permits the user to get both date and time in different formats. This command also helps in calculate current, past, and future dates with the help of different operators and format specifiers. For functionalities to be performed, the system clock and clock of the server should be precisely on time. Here are some examples that will increase your knowledge about the date command in Linux.
Prerequisites
To learn the functionality of date commands and their usage, Linux(Ubuntu) should be installed on your system. After installation, you have to configure Linux by providing user details, including name and password, to access the applications. The image below shows the interface of Ubuntu. Here you need to provide a password to proceed further.
Date Syntax
Date
A basic command used to display date is to type “Date”. It brings the current date and time which is automatically received from the system. The output shows the day of the month, date, year, and time. Date command gets the date of the operating system.
User Manual Date Command
To know more about the date-related commands, a manual guide is available for the user. The output shows the following description.
Get Dates in Different Formats
Dates can be displayed in more than one alternative method. Key “D” will display the date in the “/” form having a date, month, and year. Whereas “F” will show the date with the help of ”-“ and the format will have year, month, and day, respectively. To make it precise and easy for the user, we have used a string that shows the proper meaning of the format specifier, i.e., f and d. “+%” operator is used to bind the format specifier with the date.
Get Day, Month, and Year
There are some format specifiers used in the command with a date to enhance functionality. These are also shown in the user manual we have seen in the example above. For instance, we are going to explain some examples to make them easier for users to recognize. The syntax for format specifiers is:
As the syntax understands it, we use date and specified format to get a particular output.
Some specifiers are as follow:
% d = Presents the day of the month
% B = Displays full month name
% m = Displays the month of the year
% H = Depicts the hour in time
% M = Display the minute in time
% S = Presents the seconds in time
The Month of the Year
To find the month of the year. We use the ”B” format as described above in examples.
Year To display the current year, we use the ”Y” format. If we only want the last two digits of the year, then a small “y” is used.
Day of the weekWe will use ”A” to get the full name of the day in the week. While “a” is used to get the abbreviation of a day.
Get Dates of Past and Future
Last dayBy using this appended command, we will get yesterday’s date.
Particular Day Ago
We can get dates by providing a relevant date to go back to that particular date. For example, the user wants to get the date of 45 days ago, so this command is used.
Last Month
The last month command will help the user to get the previous month of the current year.
Next Year
Next year’s command shows the year after the current year.
Difference Between Dates
A definite day is obtained by providing two dates. One date is subtracted from the other so that the number of days is obtained between the two.
$ Echo $ ( ( ( $ ( date –d “ 2021 — 3 — 24 ” “+ % s” ) — $ ( date –d “ 2021 — 3 — 18 ” “+ % s” ) ) / 86400 ) )
Display Date with –d or –date Format
Date command knows to handle the –d or –date input. It takes an input date as a string. To obtain the date on the next day of the coming week, we input the below example.
Next, the date command can calculate the seconds of the date that have passed till now. We provide the relative date, whose seconds are to be known.
Irrespective of the format we provided in the command, the date is always displayed in a particular pattern set as default.
Display Week Number in the Current Year
As shown in the command below, we can get the week number of the current year using “V”.
Day Number in the Current Year
Day number till the current date is displayed by using %j%.
Set Date According to Timezone
To check the current time zone of the system, we use the command below.
This will display the output of the date and local time of a specific time zone. Here the time zone is Asia/Karachi.
To change the given time zone to another, you need to know the name of the timezone. The format followed to display timezone is Region/City. Here are the available options of timezone:
The same query is used to change the time zone besides the specific time zone you want to replace.
In the above-mentioned output, you can see that the time zone is updated with Europe/Istanbul.
Get Date and Time by Setting the Hardware Clock
Hardware clocks work even if there is no power supply. It runs inside the hardware of the system.
The output will be demonstrated as shown in the image:
Conclusion
Now, we can set a date or change it through the command line in Linux. It’s quite easy and understandable. The queries mentioned above are discussed in detail for the users. Format specifiers help in setting the date of a particular day.
About the author
Aqsa Yasin
I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.
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.
- 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:
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:
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:
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"
Here are another two formatting examples:
date +"Week number: %V Year: %y"
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:
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::
Display Future Dates
The —date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:
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 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:
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:
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'
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:
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:
To see how many seconds passed from epoch to a specific date, enter:
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.