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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:54:17+00:00 2026-05-28T06:54:17+00:00

I need to compare whether date is less than 3 months old. I will

  • 0

I need to compare whether date is less than 3 months old.

I will get installation date:

DateTime installdate=DateTime.Parse("1/5/2012 8:12:14 PM");
if ((installdate<DateTime.Now.AddMonths(-3)))
{
  // do something
}

Is this the best way to compare the dates?

Thanks.

  • 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-28T06:54:18+00:00Added an answer on May 28, 2026 at 6:54 am

    A few things to think about:

    • “Is date x earlier than 3 months before today” isn’t the same as “today is more than 3 months later than date x”; you’ll need to make sure you have the exact semantics you want.
    • Consider what you want to do with the time component – are you interested in dates or dates and times? (Would you expect the condition evaluation to change based on the current time of day?)
    • Consider time zones: are you interested in “today in the system’s current time zone” or some fixed time zone?
    • Depending on the source of the text data, you should possibly use DateTime.TryParse and you should possibly use DateTime.ParseExact or DateTime.TryParseExact, passing in the expected format (and culture)

    Basically, there are various corner cases around date and time behaviour – you should explicitly think about all of these things (some of which are forced upon you if you use Noda Time instead of DateTime, btw 🙂

    Regarding the first point, if the idea is that you get a trial period of three months from the installation date (or something similar), that suggests you should be adding three months to that instead.

    I’d also change the variable name and get rid of the redundant parentheses, by the way:

    DateTime installationDate = DateTime.Parse("1/5/2012 8:12:14 PM");
    DateTime trialPeriodEnd = installationDate.AddMonths(3);
    if (trialPeriodEnd > DateTime.Now)
    {
       // do something
    }
    

    Assuming you’re storing the installation date yourself somewhere, I would try to store it in some form which is less ambiguous – possibly even storing just a “ticks” value instead of a string. But assuming you are storing it yourself, you shouldn’t need to use TryParse – it makes sense to go “bang” if you can’t parse the value. I’d use ParseExact, probably with a standard format specifier of “o” (round trip).

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

Sidebar

Related Questions

I need to compare two files based on datetime. I need to check whether
I need to compare two Date s (e.g. date1 and date2 ) and come
I have a profile page (less than 10k users) and I need to track
Possible Duplicate: How to compare datetime in Django? I need to compare 2 dates
I need to compare two XMLs, which I get in the form of string
I need to compare a timestamp to a date. I would just like to
In my Java application i need to compare two list's element whether it is
I need to compare strings in shell: var1=mtu eth0 if [ $var1 == mtu
I need to compare 2 strings as equal such as these: Lubeck == Lübeck
I need to compare the integer part of two doubles for inequality and I'm

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.