Linux date custom format

How do you specify a format for the input to date?

Given a date and time in a format that is not recognized by date , how can I get date to recognize the date and time? For example:

$ date -d "09SEP2012:23:58:46" date: invalid date `09SEP2012:23:58:46' $ date -d "09SEP2012:23:58:46" --magic-option "ddMMMYYY:hh:mm:ss" Sun Sep 9 23:58:46 MDT 2012 

Does —magic-option exist? If not, is there a more elegant way to solve this rather than using sed to transform the input into a well-formed date string?

3 Answers 3

Neither POSIX nor GNU date have —magic-option . FreeBSD calls it -f (with a syntax similar to date ‘s output format specifiers, not the one you propose).

Your date is very close to being recognized by GNU date: all it takes is replacing the colon that separates the date from the time by a space.

date -d "$(echo "09SEP2012:23:58:46" | sed 's/:/ /')" 

You can’t parse dates without knowing something about the format they’re in, the classical example being 01/02/03 .

I wrote a bunch of tools (dateutils) that deal with dates and times in a more script-friendly way. Your magic option there is —input-format|-i , e.g.:

dconv -i '%d%b%Y:%H:%M:%S' "09SEP2012:23:58:46" => 2012-09-09T23:58:46 

While dconv does not directly support date ‘s output format (it doesn’t confer TZ or anything in the environment), there’s a tool strptime in dateutils that does support the %Z format specifier.

If you’re using an environment like Ubuntu that has busybox installed, you can do busybox date -D «$input_format» -d «$input» , such as:

$ busybox date -D "%m/%d/%Y %H:%M:%S" -d "02/27/2013 23:48:00" Wed Feb 27 23:48:00 CST 2013 

If you want to format the output string to something non-standard you can specify an output format with busybox date -D «$input_format» -d «$input» +»$output_format» , such as

$ busybox date -D "%m/%d/%Y" -d "02/27/2013" +"%d.%-m.%y" 27.2.13 

but for ISO-8601-compliant dates, use -I with the level of precision needed, such as:

$ busybox date -D "%m/%d/%Y %H:%M:%S" -d "02/27/2013 23:48:00" -Iseconds 2013-02-27T23:48:00-0600 

Notes

The busybox man page. This might not work with %Z timezone format.

Источник

Formatting tricks for the Linux date command

The Linux date command is simple, yet powerful. This article shows you how to unleash the power of the date command.

Читайте также:  Ограничение скорости интернета линукс

Magic trick with Ace of Spades

The date command is simple. However, it has several useful options that enhance it. It’s also capable of giving you practical information about past or future dates. This article shows you some of the format controls to manipulate the date command’s output. At the end of the article, I offer some practical suggestions about how you can use this command in conjunction with common tasks.

Basic syntax for date

The most basic syntax for the date command is simply to type in the command with no options and no format controls. Here is an example of the command and its resulting output:

date command with output displaying day, month, day of the month, hour:minute:second, time zone, and year

One modification for the date command is the -u option. This option converts the output to Coordinated Universal Time (UTC). Here is an example:

date command output after using the -u option, displaying UTC time information.

Format controls

The real customization of the date command occurs when you start adding format controls. The controls can order output fields, manage abbreviations, set the format of the date on the screen or in a file, etc. At the end of the article, I show some examples of how you might use date , and you’ll see how controlling the format is useful.

Basic format control syntax

Use one or more format controls to display information. Here is the general syntax:

Let’s look at a few examples.

Label the output fields

If you want the output labeled, you can use this format:

Output of: date +

Change the order of the fields

You can alter the order in which the fields are displayed. This is one of the most useful customizations of date because it allows you to display the output in whatever format is most useful or familiar to you. Here is one way to do it:

Output of date +

Output from the date +

This example reverses the results:

Output of the date +

Use dashes, slashes, or spaces between the fields

Maybe you need to format the date output to meet particular standards, such as date information separated by dash, slash, or space characters. Here are a couple of different examples:

Output of date with fields separated by - characters and then in the second example by / characters

Linux security

Display information from outside my current locale or time zone

It’s embarrassing to say, but for whatever reason, time zones completely mess with my mind. They always have and they probably always will. That’s why I was excited to discover that the date command can save me from having to visualize the sun’s position over a particular geographical location to figure out the appropriate time zone.

First, you must know the name of the time zone you wish to check. You can use the timedatectl list-timezones command to display this information.

Next, combine the TZ value with the date command to display the time zone’s time and date information. To show the time on the east coast of the US, run this command:

Output of the TZ=

Run the following command to display the time in Tokyo:

Output of the TZ=

Display future time/date

What if you’re coordinating a meeting with someone on the east coast of the US, and you want to confirm or display a particular future time? You can use the date command to display that information.

First, you could display the date and time information for next Friday:

Output of date --date=

Here is an example that displays the local time for 10 AM next Friday on the east coast of the US:

Output of the date command displaying a future time and date in New York

Display past dates

You can also use date to display past information.

Читайте также:  Linux change path permanently

Output from the date --date=

This example shows time and date information from 15 days ago:

Output from the date --date=

Practical application

It’s one thing to know these tricks when using the date command, but it’s another to understand how to apply them. Here are a few straightforward scenarios to get you thinking about your own tasks where date might be useful.

Redirect

You can redirect the date command into a text file. You can use any of the format controls above to customize the output. For example, what if you are conducting a very simple server documentation project? You might use the following commands:

Output from a series of system inventory commands, including date.

Set a date alias

Do you prefer the date and time to be displayed in a specific format different from the default? Set an alias for date that shows the information the way you like it. You can do this in your ~/.bashrc file.

Alias of a formatted date command in ~/.bashrcOutput of the

Set the format as a variable

You can also set the date configuration as a variable on your system.

Displays content of a script declaring a variable

Create a template file, then copy/paste the content into scripts depending on the format desired

Perhaps you create or manage various scripts, and the date output is needed in several of them. However, the date format must be different in each script. You could create a master template file that stores the required formats and then copy/paste the appropriate template into your new scripts.

Template displaying 3 formatted date commands for use in scripts

Wrap up

Like many things in Linux, the date command is simple but also very flexible. You can control the format of the data that returned to you, ensuring you get the information you need as efficiently as possible. How have you used the date command to make your life easier?

Источник

12 Useful Linux date Command Examples

Linux Date Command Examples

The date command is a command-line utility for displaying or setting date and time in the Linux system. It uses the system default time zone to display the time.

In this article, I will show you 12 examples of how to best use the date command on Linux. To demonstrate the examples below I have used an Ubuntu 20.04 system. As the date command is pre-integrated in all Linux systems we don’t need to install it.

Date Command Examples

Displaying Date

By default, the date command will display the current system date and time in a default format.

Display current date

Current date of the system.

Displaying Universal Time

If your system time zone is based on your local time zone and you want to check the universal time, to do so we need to add the -u option to the command which refers to UTC.

Display UTC time and date

Custom Date Format

We can overwrite the default date format with our preferred date format. To achieve that we need to add a format control character led by + sign and format control begins with the % sign. Some of the most used date format control characters are:

  • %a – Locale’s abbreviated short weekday name (e.g., Wed)
  • %A – Locale’s abbreviated full weekday name (e.g., Wednesday)
  • %b – Locale’s abbreviated short month name (e.g., Jun)
  • %B – Locale’s abbreviated long month name (e.g., June)
  • %Y – Display Year (e.g., 2021)
  • %m – Display Month (01-12)
  • %d – Day of month (e.g., 02)
  • %D – Display date as mm/dd/yy
  • %H – Hour in 24 hr format (00-23)
  • %I – Hour in 12 hr format (01-12)
  • %M – Display Minute (00-59)
  • %S – Display Second (00-60)
  • %u – Day of the week (1-7)
Читайте также:  Remove unused kernel linux

Here, in the following example, we formatted the date in yyyy-MM-dd format.

Use a custom date format

Custm date format without time

Displaying Date From String

We can display the formatted date from the date string provided by the user using the -d or –date option to the command. It will not affect the system date, it only parses the requested date from the string. For example,

Pass a string containing a date to date command

Parsing string to date

Displaying Upcoming Date & Time With -d Option

Aside from parsing the date, we can also display the upcoming date using the -d option with the command. The date command is compatible with words that refer to time or date values such as next Sun, last Friday, tomorrow, yesterday, etc. For examples,

Displaying Next Monday Date

Display futire dates and date calculations

Displaying Past Date & Time With -d Option

Using the -d option to the command we can also know or view past date. For examples,

Displaying Last Friday Date

Parse Date From File

If you have a record of the static date strings in the file we can parse them in the preferred date format using the -f option with the date command. In this way, you can format multiple dates using the command. In the following example, I have created the file that contains the list of date strings and parsed it with the command.

Parse multiples dates from file

Setting Date & Time on Linux

We can not only view the date but also set the system date according to your preference. For this, you need a user with Sudo access and you can execute the command in the following way.

$ sudo date -s "Sun 30 May 2021 07:35:06 PM PDT"

Display File Last Modification Time

We can check the file’s last modification time using the date command, for this we need to add the -r option to the command. It helps in tracking files when it was last modified. For example,

Display file modification time

Override the System Timezone

The date command will display the date according to your configured system time zone. We need to set the TZ variable to the desired time zone to use various time zones in the environment. For example, to switch to New York time, execute:

Use TZ variable to override time zone

Date with prefer time zone

To see all available time zones, use the timedatectl list-timezones command.

Use Unix Epoch Time

Epoch time is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC. We can use %s format control to view the number of seconds from epoch time to current time.

Seconds since since January 1, 1970, at 00:00:00 UTC

Using Date in File Naming

We can create files with the current date which helps in keeping the track record of the file. In the following example, I have created a file including a current date in its name.

Use date in file names

Naming file with the date.

Conclusion

In this article, we learn how to use the date command and how to pare send format dates on Linux.

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials

Источник

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