• Javascript
  • Python
  • Go
Tags: linux bash date sed

Converting YYYYMMDDHHMMSS to a date readable by 'date'

command Converting YYYYMMDDHHMMSS to a date readable by 'date' command When working with dates and times in the digital world, it is common ...

command

Converting YYYYMMDDHHMMSS to a date readable by 'date' command

When working with dates and times in the digital world, it is common to come across a format known as YYYYMMDDHHMMSS. This format, while easily readable by machines, can be confusing to read and understand for humans. However, fear not, as there is a simple solution to convert this format into a date that is easily readable by the 'date' command.

Before we dive into the conversion process, let's first understand what YYYYMMDDHHMMSS means. This format follows the ISO 8601 standard and is used to represent a specific date and time in a compact and unambiguous way. The first four digits (YYYY) represent the year, followed by two digits for the month (MM), two digits for the day (DD), two digits for the hour (HH), two digits for the minutes (MM), and finally two digits for the seconds (SS). This format is commonly used in databases, file names, and other digital systems.

Now, let's say we have a file named "20210824153000.txt" and we want to know the date and time it was created. We can easily do this by using the 'date' command in our terminal. However, if we simply type in the command "date 20210824153000", we will receive an error message as the 'date' command does not recognize this format. This is where the conversion process comes in.

To convert YYYYMMDDHHMMSS to a date readable by the 'date' command, we will need to use the 'date' command with the -d flag. The -d flag is used to specify the date and time in a specific format. In this case, we will use the format "+%Y%m%d%H%M%S" which is the reverse of the original format. This means that we are specifying the year (Y), month (m), day (d), hour (H), minutes (M), and seconds (S) in that order. So, our command will look like this:

date -d "20210824153000" "+%Y%m%d%H%M%S"

When we run this command, we will get the output "Tue Aug 24 15:30:00 IST 2021". Voila! We have successfully converted the YYYYMMDDHHMMSS format into a date that is easily readable by the 'date' command.

But what if we want to convert multiple dates at once? Say, we have a list of files with different creation dates and we want to know when each file was created. Instead of manually typing in the conversion command for each file, we can use a simple shell script to automate the process. Here's an example:

#!/bin/bash

for file in *.txt; do

date -d "${file::-4}" "+%Y%m%d%H%M%S"

done

In this script, we are using a "for" loop to go through each file with the .txt extension in the current directory. Then, we use the "date" command with the -d flag to convert the file name (without the .txt extension) into a readable date format. The output will be displayed in the terminal for each file.

In conclusion, converting YYYYMMDDHHMMSS to a date readable by the 'date' command may seem daunting at first, but with the use of the -d flag and a little bit of knowledge about the ISO 8601 standard, it can be easily done. Now, you can confidently work with dates and times in this format without any confusion. Happy coding!

Related Articles

No Include Path Found for stdio.h

When it comes to programming, one of the most frustrating errors that can occur is the infamous "No Include Path Found" error for stdio.h. T...

Redirecting stderr in bash

Bash is one of the most commonly used command-line interpreters in the world of Linux and Unix operating systems. It is a powerful tool that...