---
title: "Effective Management Linux Timezones (Step by Step Tutorial)"
description: "Best practices for managing Linux timezones, dates, and time with practical examples for the IT administrator!"
canonical: "https://adamtheautomator.com/linux-timezones/"
---

# Effective Management Linux Timezones (Step by Step Tutorial)

> Best practices for managing Linux timezones, dates, and time with practical examples for the IT administrator!

Source: https://adamtheautomator.com/linux-timezones/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Effective Management Linux Timezones (Step by Step Tutorial)](https://adamtheautomator.com/wp-content/uploads/2022/06/Behind-the-Scenes-of-Linux-Timezones-Dates-and-Time.jpg)

# Effective Management Linux Timezones (Step by Step Tutorial)

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)26 July 20228 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Linux](/tag/linux/)[Linux Shell Scripting](/tag/linux-shell-scripting/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Printing the Current Date and Time with the date Command](#printing-the-current-date-and-time-with-the-date-command)
*   [Printing the Current Date in Different Formats](#printing-the-current-date-in-different-formats)
*   [Calculating and Printing Future and Past Dates](#calculating-and-printing-future-and-past-dates)
*   [Printing Incrementing Dates with a Bash Script](#printing-incrementing-dates-with-a-bash-script)
*   [Printing Current Date and Time with the GNU Awk CLI Tool](#printing-current-date-and-time-with-the-gnu-awk-cli-tool)
*   [Converting Date Format in a File](#converting-date-format-in-a-file)
*   [Checking the Time Zone Using the grep Command](#checking-the-time-zone-using-the-grep-command)
*   [Conclusion](#conclusion)

Timekeeping on computers is essential for many reasons, and spelling out each may take a day. But kidding aside, if you’re looking for effective management of Linux timezones, you’ve come to the right place.

In this tutorial, you’ll explore and learn how you can work with dates, times, and timestamps in Linux. Some are printing the current date and time, calculating future and past dates, working with timezones, and more.

Read on and slay timekeeping like a pro!

## Prerequisites

This tutorial comprises hands-on demonstrations. To follow along, you’ll need the following:

*   A Linux machine – This tutorial uses [Ubuntu](https://adamtheautomator.com/install-ubuntu/) 20.04, but any modern Linux distribution will work.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   A text editor – If you don’t have a preferred text editor, you can use nano, which is installed by default on most Linux distributions.

Related:[How to Edit Files with a Real PowerShell Text Editor](https://adamtheautomator.com/powershell-text-editor/)

## Printing the Current Date and Time with the `date` Command

The list of reasons for timekeeping goes on, depending on your needs for your project. But the most common reasons are below:

*   System logs use timestamps to catalog when events occurred.
*   Software releases are often tied to specific dates.
*   Network security tools use time-based analysis to detect anomalies.
*   Users rely on accurate time to schedule appointments and meetings.

For any of these reasons, the first thing you may want to perform is print the current date and time. And on Linux, you can accomplish this task with the date and awk commands. But for a start, you’ll run the date command first.

Run the `date` command below without parameters to print the current date and time.

```bash
date
```

As you can see below, the command’s output, by default, includes the following:

*   The day of the week (**Friday**)
*   The numeric day of the month (**10**)
*   The month name in long format (**Friday**), and the year.
*   The date is followed by the current full-time in 24-hour format and the time zone.

![Printing the Current Date and Time](https://adamtheautomator.com/wp-content/uploads/2022/06/image-839.png)

Printing the Current Date and Time

Now, run the following command to view all available options you can use along with the `date` command.

```bash
date --help
```

![Viewing all available options for the date command](https://adamtheautomator.com/wp-content/uploads/2022/06/image-840.png)

Viewing all available options for the date command

## Printing the Current Date in Different Formats

You’ve seen that the date command alone prints the current date and time in full format. But typically, you’ll have to change the format depending on your needs.

The good news is that the date command lets you print the system date and time in different formats — this feature enables you to get better visuals of the current date and time.

1\. Run the below command to print the current date only in numeric format (%D).

```bash
date "+DATE: %D”
```

![Printing the current date only in numeric format](https://adamtheautomator.com/wp-content/uploads/2022/06/image-841.png)

Printing the current date only in numeric format

> _Perhaps you prefer to see the time zone along with the date. In that case, append the %Z option, like in the command below: date “+DATE: %D %Z”_

![Printing the current date with the time zone](https://adamtheautomator.com/wp-content/uploads/2022/06/image-842.png)

Printing the current date with the time zone

2\. Next, run the following command to print the current date in the format of MMM YYYY. The %b option prints the abbreviated month (Nov, Dec, Jan…), while the %Y option prints the current year.

```bash
date "+DATE: %b %Y"
```

![Printing abbreviated month and year](https://adamtheautomator.com/wp-content/uploads/2022/06/image-843.png)

Printing abbreviated month and year

3\. Now, run the below command to print the date as the long Month (November, December…) (%B), DD (%d), YYYY (%Y).

You can also chain together multiple options to format the output exactly how you want it.

```bash
date "+DATE: %B %d,%Y"
```

![Printing the current date in long format](https://adamtheautomator.com/wp-content/uploads/2022/06/image-844.png)

Printing the current date in long format

4\. Lastly, run the same command in step three. But this time, add the %I:%M %p options to print the current time.

```bash
date "+DATE: %B %d,%Y %I:%M %p"
```

![Printing the current date and time](https://adamtheautomator.com/wp-content/uploads/2022/06/image-845.png)

Printing the current date and time

## Calculating and Printing Future and Past Dates

In real life, you might want to calculate a date that is in the future. Why? Perhaps you’re writing a script to automatically perform tasks on a specific date. If so, calculating future dates using the `--date` option will come in handy.

1\. Run the below command to calculate the date 40 days from the current date. You can change days to weeks, months, years, etc., as you like.

```bash
date --date='40 days'
```

Below, you can see the calculated full date and time 40 days from the current date.

![Calculating the future date in default full format](https://adamtheautomator.com/wp-content/uploads/2022/06/image-846.png)

Calculating the future date in default full format

2\. Next, run the same command as you did in step one. But this time, add options (“+%B %d,%Y %I:%M %p”) to change the output format.

```bash
date --date='40 days' "+%B %d,%Y %I:%M %p"
```

![Calculating the future date in a custom format](https://adamtheautomator.com/wp-content/uploads/2022/06/image-847.png)

Calculating the future date in a custom format

3\. Now, run the below command, specifying a negative number to calculate dates in the past. The below command calculates the date one year ago from the current date.

```bash
date --date='-1 year’
```

The output below shows the date a year in the past in full default format. But if you like to change the format, you can apply the same format you did in step two.

![View the date one year ago from the current date](https://adamtheautomator.com/wp-content/uploads/2022/06/image-848.png)

View the date one year ago from the current date

4\. Now, run the command below, using a combination of units (weeks and days) with the –date option. The below command calculates the date two weeks and four days ago from the current date.

```bash
date --date='-2 weeks -4 days’
```

![Calculating the date two weeks and four days ago](https://adamtheautomator.com/wp-content/uploads/2022/06/image-849.png)

Calculating the date two weeks and four days ago

5\. Lastly, run the below command to calculate the date next Tuesday.

You can also use other date formats besides the numeric format for the `--date` option. For example, you can use full month names, like `Tuesday`, or relative time formats like `tomorrow`, `next`, `last`., etc.

```bash
date --date='next Tuesday'
```

![Calculating the date next Tuesday from the current date](https://adamtheautomator.com/wp-content/uploads/2022/06/image-850.png)

Calculating the date next Tuesday from the current date

## Printing Incrementing Dates with a Bash Script

Printing current, past, and future dates work fine with the date command. But did you know you can also write a Bash script to work with timezones, dates, and times? For instance, incrementing the date by day, week, or month.

Related:[Your One and Only Linux Shell Scripting Tutorial](https://adamtheautomator.com/linux-shell-scripting-tutorial/)

Incrementing dates is helpful when working with cron jobs that need to run daily, weekly, or monthly.

Related:[How To Execute and List Cron Jobs for a Linux System via PHP](https://adamtheautomator.com/list-cron-jobs/)

1\. Create a Bash script named _date2022.sh_ with your preferred editor.

2\. Add the following code to the file _date2022.sh, s_ave the file, and close the editor.

The code below prints the date for each day of the week, starting with today’s date and ending with the following Monday’s date.

```bash
DATE=2022-05-30

for i in {0..6}
do
   NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day")
   echo "$NEXT_DATE"
done
```

3\. Finally, run the below bash command to execute the date2022.sh script

```bash
bash date2022.sh
```

![Viewing dates for the next seven days](https://adamtheautomator.com/wp-content/uploads/2022/06/image-851.png)

Viewing dates for the next seven days

## Printing Current Date and Time with the GNU Awk CLI Tool

So far, you’ve seen how to print dates and times in different formats with the `date` command, which is good enough. But having more ways to manipulate date and time, be it on the actual system or in a file, is always better.

What’s the other way? The GNU Awk CLI tool. Awk is a powerful text processing tool that can be used for a wide range of tasks. These tasks can be but are not limited to extracting data from log files and manipulating and analyzing text data.

But before you can use the GNU Awk CLI tool, you’ll first have to install it on your machine:

1\. Run the [apt update](https://linuxize.com/post/how-to-use-apt-command/#updating-package-index-apt-update) command below to update your system package index.

```bash
apt update -y
```

![Updating system package index](https://adamtheautomator.com/wp-content/uploads/2022/06/image-852.png)

Updating system package index

2\. Next, run the following [apt install](https://linuxize.com/post/how-to-use-apt-command/#installing-packages-apt-install) command to install the GNU Awk CLI tool.

```bash
apt install gawk -y
```

![Installing the GNU Awk CLI tool](https://adamtheautomator.com/wp-content/uploads/2022/06/image-853.png)

Installing the GNU Awk CLI tool

3\. Once installed, run the `gawk` command to `print` the day of the week (`%A`), month (`%B`), numeric current date (`%d`), and year (`%Y`) where the following functions:

*   The `strftime` function formats the date and time.
    
*   The systime function returns the current system time in seconds since the Epoch.
    

```bash
gawk 'BEGIN { print strftime("Today is %A, %B %d, %Y.", systime()) }'
```

Notice how awk allows you to format the output in a human-readable way.

![Printing current date and time using the GNU awk CLI tool](https://adamtheautomator.com/wp-content/uploads/2022/06/image-854.png)

Printing current date and time using the GNU awk CLI tool

4\. Lastly, run the below command to calculate and print the date a month ago. The command below uses the date command with a negative number (-1) to get the first day of the previous month.

```bash
awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{print previous_month}'
```

![Printing the date a month ago](https://adamtheautomator.com/wp-content/uploads/2022/06/image-855.png)

Printing the date a month ago

## Converting Date Format in a File

Perhaps you have a file containing dates in a format that is incompatible with your current locale. Or the dates in the file are not clear enough to be read by humans. If so, consider using the GNU Awk CLI tool.

Create a file with your preferred text editor and input the following lines to the file where the date format is unclear. Provide the file a name you like, but the CSV file is called _test.csv_ in this example.

```powershell
1,ABC,SSS,20-OCT-16,4,1,0,5,0,0,0,0
2,DEF,AAA,20-JUL-16,4,1,0,5,0,0,0,0
```

Now, run the awk command below to extract and convert the date format to a more human-readable format of YY-MM-DD. The -F option sets the field separator as a comma or dash, and the command prints (printf) the date in your desired format.

```bash
awk -F'[,-]' '{ printf "%s|20%02d-%02d-%02d,%s\\n", $3, $6, (match("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC",$5)+2)/3, $4, $10 }' test.csv
```

As you can see below, the date format is now much clearer.

![Extracting and printing a readable date](https://adamtheautomator.com/wp-content/uploads/2022/06/image-856.png)

Extracting and printing a readable date

> _You’ve seen that awk is a powerful tool that can perform a wide range of tasks, like comparing the date, adding a date field to a file, sorting the dates, etc. But that’s beyond the scope of this article._

## Checking the Time Zone Using the `grep` Command

Checking the current time zone shouldn’t be a complex tax, and the grep command ensures this statement stays true. The grep command is another powerful text processing tool you can use for a wide range of tasks.

Some tasks that the grep command supports are extracting data from log files and manipulating and analyzing text data. With these features, you can also check your current time zone with the grep command.

Run the following [timedatectl](https://man7.org/linux/man-pages/man1/timedatectl.1.html) command alone to print the current date, time, time zone, and NTP service status on your machine.

```bash
timedatectl
```

As you can see below, the output shows a handful of information about the current date, time, and time zone. This information lets you verify the correct time zone is set on your machine, which also helps ensure you don’t have issues with time synchronization on your network.

![Checking Date, Time, and Time Zone](https://adamtheautomator.com/wp-content/uploads/2022/06/image-857.png)

Checking Date, Time, and Time Zone

> _Perhaps you like to check the list of time zones before exploring more about the grep command. If so, run the following command: timedatectl list-timezones_

Now, run the below command piped to the grep command if you plan to check your machine’s current time zone to perform the following:

*   Fetch the date, time, and time zone information from the Linux kernel file (`timedatectl`).
*   Search and print lines of text that match the `Time zone` text pattern.

```bash
timedatectl | grep “Time zone”
```

As you can see below, the output only shows the current time zone set on your machine.

![Using Grep to Check the Time Zone](https://adamtheautomator.com/wp-content/uploads/2022/06/image-858.png)

Using Grep to Check the Time Zone

## Conclusion

This tutorial walked you through many practical examples of querying date, time, and Linux timezones. You’ve learned to manipulate date and time format, calculate past and future dates with the `date` command. You also ran `timedatectl` and `grep` commands to check your system’s time zone.

Now, why not write more scripts to automate tasks like calculating past and future dates or converting date formats? Or perhaps [create cron jobs](https://fedingo.com/how-to-run-shell-script-as-cron-job/) to schedule the execution of your scripts?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Flinux-timezones%2F&text=Effective%20Management%20Linux%20Timezones%20\(Step%20by%20Step%20Tutorial\))[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Flinux-timezones%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Flinux-timezones%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2024/03/pipe-command-in-linux-1.jpg)

### [Unleashing the Power of the Pipe Command in Linux](/pipe-command-in-linux/)

Unlock the prowess of the pipe command in Linux to streamline tasks, boost productivity, and simplify complex operations effortlessly!

![](https://adamtheautomator.com/wp-content/uploads/2022/07/How-to-Update-Ubuntu-IP-and-Hostname-via-Bash.jpg)

### [How to Update Ubuntu IP and Hostname via Bash](/change-linux-ip-hostname-bash-script/)

Discover and learn how to change a Linux IP and Hostname via a Bash script in this new tutorial by ATA Learning!

![](https://adamtheautomator.com/wp-content/uploads/2021/12/Manage-Directory-and-File-Permissions-with-chmod-Recursive.jpg)

### [Manage Directory and File Permissions with chmod Recursive](/chmod-recursive/)

Learn how to leverage the powerful chmod recursive command to ensure correct permissions across all of your files and folders!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
