Comparing Two Dates - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Comparing Two Dates

Super26Super26 Posts: 14Member
I am trying to compare two dates, if the date that the user enters is before the current date, then I want to display a message box saying it is an invalid date. What I have currently is this:

If FormatDateTime(userDate, vbShortDate) < FormatDateTime(Date, vbShortDate) = True Then
MsgBox ("Please enter a valid date.")
txtDate.SetFocus

This code only works for the year and day, if you change the month, irregardless of the day and the year it doesn't display the message box. Is there any way to compare the two dates without having to convert everything into seconds?

Comments

  • Sand_Hawk9Sand_Hawk9 Posts: 233Member
    I always convert my dates to longs to compare them. I most of the time use this routine:

    [code]
    if clng(Dateserial(Year(EnteredDate), Month(EnteredDate), Day(EnteredDate)) < clng(Dateserial(Year(Date), Month(Date), Day(Date)) then
    end if
    [/code]

    this one will check if the entered date is smaller then the current date. Maybe there is some other way of doing it but I always use this.

    Hope it helped,

    Sand Hawk
    Member of the Stupid Coders
    http://www.stupidcoders.cjb.net

    - Earth is 98% full. Please delete anybody you can.

  • ultimatumultimatum Posts: 1Member
    : I am trying to compare two dates, if the date that the user enters is before the current date, then I want to display a message box saying it is an invalid date. What I have currently is this:
    :
    : If FormatDateTime(userDate, vbShortDate) < FormatDateTime(Date, vbShortDate) = True Then
    : MsgBox ("Please enter a valid date.")
    : txtDate.SetFocus
    :
    : This code only works for the year and day, if you change the month, irregardless of the day and the year it doesn't display the message box. Is there any way to compare the two dates without having to convert everything into seconds?
    :
    :

    Try this:

    If CDate(userDate) < Date Then
    MsgBox "Please enter a valid date.
    txtDate.SetFocus
  • Sand_Hawk9Sand_Hawk9 Posts: 233Member
    That's another possiblity. I use my way because I store my dates in a database as longs. I dunno why, but I think it's easier for me(Its more coding).

    Sand Hawk
    Member of the Stupid Coders
    http://www.stupidcoders.cjb.net

    - Earth is 98% full. Please delete anybody you can.

  • Super26Super26 Posts: 14Member
    : I am trying to compare two dates, if the date that the user enters is before the current date, then I want to display a message box saying it is an invalid date. What I have currently is this:
    :
    : If FormatDateTime(userDate, vbShortDate) < FormatDateTime(Date, vbShortDate) = True Then
    : MsgBox ("Please enter a valid date.")
    : txtDate.SetFocus
    :
    : This code only works for the year and day, if you change the month, irregardless of the day and the year it doesn't display the message box. Is there any way to compare the two dates without having to convert everything into seconds?
    :
    :

    Thanks so much guys it worked!!!
  • KDivad LeahcimKDivad Leahcim Posts: 3,948Member
    : I am trying to compare two dates, if the date that the user enters is before the current date, then I want to display a message box saying it is an invalid date. What I have currently is this:
    :
    : If FormatDateTime(userDate, vbShortDate) < FormatDateTime(Date, vbShortDate) = True Then
    : MsgBox ("Please enter a valid date.")
    : txtDate.SetFocus
    :
    : This code only works for the year and day, if you change the month, irregardless of the day and the year it doesn't display the message box. Is there any way to compare the two dates without having to convert everything into seconds?
    :
    :

    Nothign against anyone else's code, but I always use DateDiff.

    If DateDiff("s", Date1, Date2) < 0 Then
    'Date1 is later than Date2
    Else
    'Date2 is later than Date1
    End If

    I could have those backwards (Date1, Date2), but it's the idea...
  • Sand_Hawk9Sand_Hawk9 Posts: 233Member
    Thanks for the tip David! I never knew that function existed.

    Sand Hawk
    Member of the Stupid Coders
    http://www.stupidcoders.cjb.net

    - Earth is 98% full. Please delete anybody you can.

  • KDivad LeahcimKDivad Leahcim Posts: 3,948Member
    : Thanks for the tip David! I never knew that function existed.
    :
    : Sand Hawk
    : Member of the Stupid Coders
    : http://www.stupidcoders.cjb.net
    :
    : - Earth is 98% full. Please delete anybody you can.
    :
    :

    You're welcome! For those that don't have the help file or don't like to use it, replace the first parameter with one of the following to get the difference in a different amount:

    s seconds
    n minutes 'Yes, "n". m is for months and is only mins when next to hours
    h hours
    d days
    m months
    y years

    There are a couple of others, but these will help people the most.


    I'm one of those that looks up in the help file and uses See Also repeatedly until I get tired of learning new things...
Sign In or Register to comment.