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

  • SEARCH
  • Home
  • 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 9007923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:49:07+00:00 2026-06-16T01:49:07+00:00

I have a piece of code which compares and validate dates. Problem: This code

  • 0

I have a piece of code which compares and validate dates.

Problem:

This code is taking too much time during execution. When i comment this code the execution takes much less time. From this i can understand this code has some overhead in it. Please make suggestions to improve efficiency of this code.

DateTime regDate;
DateTime dob;

extractDate(buffer[5], buffer[6], out regDate, out dob);

private void extractDate(string date, string rDate, out DateTime regDate, out DateTime dob)
{
    if (date == "")     // Date of birth is not given;
    {
        regDate = getStandardDate(rDate);   // MM/DD/YYYY
        dob = regDate.AddYears(-18);
    }
    else
    {
        dob = getStandardDate(date);
    }

    if (rDate == "")
    {
        dob = getStandardDate(date);
        regDate = dob.AddYears(18);
    }
    else
    {
        regDate = getStandardDate(rDate);
    }
} 

private DateTime getStandardDate(string date)
{
    bool checkDate = isValidDate(date);
    if (checkDate)
    {
        IFormatProvider culture = new System.Globalization.CultureInfo("en-gb", true);
        DateTime dt;
        if (karachiDateFormat)
        {
            dt = DateTime.Parse(date);
        }
        else
        {
            dt = DateTime.Parse(date, culture);
        }


        return dt.Date;
    }
    else
    {
        return DateTime.Now.Date;
    }
}

private bool isValidDate(string date)
{
    IFormatProvider culture = new System.Globalization.CultureInfo("en-gb", true);
    DateTime Ttime;
    if (!karachiDateFormat)
    {
        Ttime = DateTime.Parse(date, culture);
    }
    else
    {
        Ttime = DateTime.Parse(date);
    }

    date = string.Empty;
    date = Ttime.Date.ToShortDateString();
    string[] splitDate = date.Split('/');

    int day = Int32.Parse(splitDate[1]);
    int month, year;
    month = Int32.Parse(splitDate[0]);

    year = Int32.Parse(splitDate[2]);
    DateTime time = DateTime.Now;
    if ((day > 0 && day < 32) && (month > 0 && month <= 12) && (year > 1950 && year < time.Year))
    {
        return true;
    }
    else
    {
        return false;
    }
}
  • 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-16T01:49:08+00:00Added an answer on June 16, 2026 at 1:49 am

    I believe you can speed up your code by changing the isValidDate method.
    From what I understand you validate the text if it matches the date pattern. I would try to completely remove this method and replace DateTime.Parse in your getStandardTime with DateTime.TryParse This will take care of both validation and parsing. See here DateTime.TryParse Method

    If you need some validation of your date, than do this with DateTime structure, not with string repesentation.

    Also you are parsing two dates (date and rDate). maybe using Parallel.Invoke could help, but I am not sure about this.

        private void extractDate(string date, string rDate, out DateTime regDate, out DateTime dob)
        {
            Parallel.Invoke(
                () =>
                {
                    if (date == "")     // Date of birth is not given;
                    {
                        regDate = getStandardDate(rDate);   // MM/DD/YYYY
                        dob = regDate.AddYears(-18);
                    }
                    else
                    {
                        dob = getStandardDate(date);
                    }
                },
            () =>
            {
    
                if (rDate == "")
                {
                    dob = getStandardDate(date);
                    regDate = dob.AddYears(18);
                }
                else
                {
                    regDate = getStandardDate(rDate);
                }
            });
        }
    
        private DateTime getStandardDate(string date)
        {
            IFormatProvider culture = new System.Globalization.CultureInfo("en-gb", true);
            DateTime dt;
            if (karachiDateFormat)
            {
                if (!DateTime.TryParse(date, out dt))
                    dt = DateTime.Now.Date;
            }
            else
            {
                if (!DateTime.TryParse(date, culture, out dt))
                    dt = DateTime.Now.Date;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code which works 90% of the time: $user_details=$fb->api_client->users_getInfo($fb_user, array('last_name','first_name','proxied_email'));
I have this piece of sh code which takes one argument at a time:
I have this piece of code which check the last customer_id from the db.
I have this piece of code which I'm hoping will be able to tell
Possible Duplicate: php loop through associative arrays i have this piece of code which
I am somewhat new to R, and i have this piece of code which
Ok i have this piece of code from which i took from W3schools :-
I have this piece of code which gives to SAXParseError i.e., it is not
I have this piece of code which i am using on another area of
I have this piece of code which works, however I would like to change

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.