Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if date passed is in yyyymmdd format and not yyyyddmm?

I am passing an argument ‘bdate’ with date as an argument.How to create a function in shell script to check if date passed is in yyyymmdd format and not in ‘yyyyddmm’?

Any ideas would be appreciated?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To check if a date is in the yyyymmdd format and not the yyyyddmm format in a shell script, you can use the date command to convert the date string to the yyyymmdd format and compare it to the original date string. If the converted date string is the same as the original date string, then the date is in the correct format.

Here is an example:

#!/bin/bash

# function to check if a date is in the correct format (yyyymmdd)
is_date_correct_format() {
    # parse the date string and convert it to the yyyymmdd format
    local converted_date=$(date -d "$1" +%Y%m%d)
    
    # check if the converted date string is the same as the original date string
    if [ "$converted_date" = "$1" ]; then
        # the date is in the correct format
        return 0
    else 
        # the date is in the incorrect format
        return 1
    fi
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading