InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Formatting Dates for Display in bash
Bash

Formatting Dates for Display in bash

InfinityCoder February 20, 2017

You need to format dates or time for output.

Use the date command with a strftime format specification.

See “Date and Time String Formatting with strftime” in Appendix A or the strftime manpage for the list of format specifications supported.

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
# Setting environment variables can be helpful in scripts:
$ STRICT_ISO_8601='%Y-%m-%dT%H:%M:%S%z'  # http://greenwichmeantime.com/info/iso.htm
$ ISO_8601='%Y-%m-%d %H:%M:%S %Z'       # Almost ISO-8601, but more human-readable
$ ISO_8601_1='%Y-%m-%d %T %Z'          # %T is the same as %H:%M:%S
$ DATEFILE='%Y%m%d%H%M%S'              # Suitable for use in a file name
 
$ date "+$ISO_8601"
2006-05-08 14:36:51 CDT
 
gawk "BEGIN {print strftime(\"$ISO_8601\")}"
2006-12-07 04:38:54 EST
 
# Same as previous $ISO_8601
$ date '+%Y-%m-%d %H:%M:%S %Z'
2006-05-08 14:36:51 CDT
 
$ date -d '2005-11-06' "+$ISO_8601"
2005-11-06 00:00:00 CST
 
$ date "+Program starting at: $ISO_8601"
Program starting at: 2006-05-08 14:36:51 CDT
 
$ printf "%b" "Program starting at: $(date '+$ISO_8601')\n"
Program starting at: $ISO_8601
 
$ echo "I can rename a file like this: mv file.log file_$(date +$DATEFILE).log"
I can rename a file like this: mv file.log file_20060508143724.log

You may be tempted to place the + in the environment variable to simplify the later command.

On some systems the date command is more picky about the existence and placement of the + than on others.

Our advice is to explicitly add it to the date command itself.
Many more formatting options are available, see the date manpage or the C strftime( ) function (man 3 strftime) on your system for a full list.

Unless otherwise specified, the time zone is assumed to be local time as defined by your system.

The %z format is a nonstandard extension used by the GNU date command; it may not work on your system.
ISO 8601 is the recommended standard for displaying dates and times and should be used if at all possible.

It offers a number of advantages over other display formats:

• It is a recognized standard
• It is unambiguous
• It is easy to read while still being easy to parse programmatically (e.g., using awk or cut)
• It sorts as expected when used in columnar data or in filenames

Try to avoid MM/DD/YY or DD/MM/YY or even worse M/D/YY or D/M/YY formats.
They do not sort well and they are ambiguous, since either the day or the month may come first depending on geographical location, which also makes them hard to parse.

Likewise, use 24-hour time when possible to avoid even more ambiguity and parsing problems.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Clobbering a File on Purpose in bash
You like to have noclobber set, but every once in …

Clobbering a File on Purpose in bash

Keeping Files Safe from Accidental Overwriting in bash
You don’t want to delete the contents of a file …

Keeping Files Safe from Accidental Overwriting in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap