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 7583393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:40:01+00:00 2026-05-30T18:40:01+00:00

I want to calculate the date difference between form date and two date..Am using

  • 0

I want to calculate the date difference between form date and two date..Am using timespan to calculate the difference between two date if date difference is positive means it enter another process falls means it return error message.

My partial code is here..

  TimeSpan span = Convert.ToDateTime(txtenddate.Text).Subtract(Convert.ToDateTime(txtstartdate.Text));
        int formatted = span.Days;
        if (formatted < 1)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Invalid date difference ');</script>", false);
        } 

In the above code input is end date : 30-01-2004 start date : 01-02-2002

but it returns error message : String was not recognized as a valid DateTime.

please give me a solution to solve this with out changing date format…

  • 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-05-30T18:40:02+00:00Added an answer on May 30, 2026 at 6:40 pm

    You should be using ParseExact to get the relevant DateTime.

    TimeSpan ts = DateTime.ParseExact("30-01-2004", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture)
                      - DateTime.ParseExact("01-02-2002", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
    

    While using Convert.ToDateTime it invokes DateTime.Parse which will try the conversion corresponding to the your current culture setting which as in this case doesn’t support the format of DateTime you have, so you should rely on the ParseExact whereby you know the format in which the string is expected and achieve in fetching your result.

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

Sidebar

Related Questions

I want to calculate the difference between two date/time in java using Date and
I want to calculate the difference between two times, one of which is the
Possible Duplicate: Calculating the Difference Between Two Java Date Instances In Java, I want
I want to calculate the difference of days between two dates. My code works
I want to calculate the difference between two dates in php. I am trying
I want to get difference between dates skipping time means if one date is
I want to calculate the angle between two vectors a and b. Lets assume
I have two objects of Zend_Date class and I want to calculate the difference
I want to calculate the difference in hours from two dates. This is my
Possible Duplicates: Difference between dates How to calculate the date difference between 2 dates

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.