Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9079889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:56:32+00:00 2026-06-16T19:56:32+00:00

My date format is yyyy-mm-dd-hh:mm:ss How do I check my input? It should be

  • 0

My date format is yyyy-mm-dd-hh:mm:ss
How do I check my input?

It should be something like this:

#!/bin/bash

read -p "Date (format yy-mm-dd-HH-MM-SS): " input

check=$(date +"%Y-%m-%d-%H:%M:%S")

if [ $input -eq $check ]; do

     echo "Right!"

else
     echo "False!"

fi

But that doesn’t check the date It compares my input with the real date.

Best Regards
Vince

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:56:33+00:00Added an answer on June 16, 2026 at 7:56 pm

    Edited apr 2016!

    See further (stronger method)

    Original post

    Try:

    #!/bin/bash
    
    read -p "Date (format yyyy-mm-dd): " input
    check=$(date +%F)
    
    if [ "$input" == "$check" ]; then
        echo "Right!"
    else
        echo "False!"
    fi
    

    or

    #!/bin/bash
    
    read -p "Date (format YYYY-MN-DD-HH24:MM:SS): " input
    check=$(date +%F-%T)
    
    if [ "$input" == "$check" ]; then
        echo "Right!"
    else
        echo "False!"
    fi
    

    Well tested:

    cat >hesdate.sh     # Copy 1st sample and paste to terminal
    chmod +x hesdate.sh
    date +%F ; ./hesdate.sh
    2013-01-04
    Date (format yyyy-mm-dd): 2013-01-04
    Right!
    
    cat >hesdate.sh     # Copy 2nd sample and paste to terminal
    date -d now\ +10\ sec +%F-%T ; ./hesdate.sh 
    2013-01-04-10:17:06                                       # copy this line
    Date (format YYYY-MN-DD-HH24:MM:SS): 2013-01-04-10:17:06  # past exactly 10 secs after
    Right!
    

    Edit add

    For testing a date, you could:

    [[ $input =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$ ]]
    
    if [[ $input =~ ^2012-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$ ]];then
    

    and/or

    inputSecs=$(date -d "${input%-*} ${input##*-}" +%s)
    

    Using boot method let you confirm format and reliability of input

    Stronger method

    If you want to check input, there is a finer method:

    unset adate
    declare -A adate
    date=2013-12-04-10:17:06
    for field in s:0-59 m:0-59 h-0-23 D-1-31 M-1-12 Y#2000-2100 ;do
       sep=${field:1:1} min=${field:2} field=${field:0:1} max=${min#*-} min=${min%-*}
       crt=${date##*${sep:-#}}
       ((min<=10#$crt&&10#$crt<=max)) && adate[$field]=$crt ||
           echo Error: $crt not between $min and $max in $field field.
       date=${date%$sep*}
     done
    declare -p adate
    

    This will dump adate array variable:

    declare -A adate='([D]="04" [M]="12" [Y]="2013" [h]="10" [m]="17" [s]="06" )'
    

    From there, you could re-validate day number:

    max=$(date -d "${adate[Y]}-${adate[M]}-1 +1 month -1 day" +%d)
    ((10#${adate[D]}>max)) && echo "Error Day number too high: (${adate[D]}>$max)."
    

    The only thing not tested there is field length if

    date=2012-02-29-10:17:06
    

    will work, then

    date=2012-2-29-10:17:06
    

    will work too (there is only one digit in day field).

    If needed, you could change the line:

    for field in s:0-59 m:0-59 h-0-23 D-1-31 M-1-12 Y#2000-2100 ;do
    sep=${field:1:1} min=${field:2} field=${field:0:1} max=${min#*-} min=${min%-*}
    crt=${date##*${sep:-#}}
    

    for

    for field in s:20-59 m:20-59 h-20-23 D-21-31 M-21-12 Y#42000-2100 ;do
    sep=${field:1:1} len=${field:2:1} min=${field:3} field=${field:0:1} max=${min#*-} min=${min%-*}
    crt=${date##*${sep:-#}}
    [ ${#crt} -eq $len ] || echo "Error: Field $field is no $len len: ${#crt}."
    

    Nota: Year field is arbitrarily limited between 2000 and 2100, but this is easy to understand/change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

User should enter date in the next format yyyy-MM-dd . So I check it
I would like to check if the date format is (dd/mm/yyyy) before the post
I would like to check for a valid date in 'mm/dd/yyyy' format. When I
I have the need to display a date in this format: dd/mm/yyyy. This is
I have a date in String format, mm/dd/yyyy I want to convert this to
How can I best check if the following date (format dd/mm/yyyy) is in the
I have a date format like MMMM dd, yyyy and i am using <fmt:formatDate
How do I check if a date string is in the MM/DD/YYYY format in
I am trying to check if a date of format mm.dd.yyyy is greater than
I have a textbox for entering date.Textbox should allow only dd/MM/yyyy format.How can i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.