Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date doubt

Status
Not open for further replies.

shanmugham

Programmer
Jun 19, 2001
71
0
0
IN
Dear Friends

my date input is
Purchase date is : "01-jan-2004"

automatically calculate exp date, that is exactly one year
that is 31-dec-2004

example : "29-feb-2004" next year as "28-feb-2004"

how can i do, i developed code give some problem in a particluar date

pls

thanks in advance

shan
 
dtdate = dateadd("yyyy",1,dtdate)
dtdate = dateadd("d",-1,dtdate)

mmilan
 
excellent !!!

thanks


but i am using 29-feb-2004 , it displays 27-feb-2004
01-mar-2005 converts 02-jan-2006

i think small correction

shan

 
Hey shan, can you maybe just post the section of code that you use to calculate the dates with, or have the problem with. That way we might be able to spot the problem.
 
You appear to be using NON U.S. date formats. Many (most? all?) MS date functions require the U.S. (date) Format.





MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Hi Shan,

Try the following code and adjust as necessary.
You need a text box (text1) a command button (command1) and a label (label1) for the demo code, put the code in Command1_Click()

***Code***
Dim StartDate As Date, EndDate As Date

StartDate = CDate(Text1.Text)
If ((Year(StartDate)) Mod 4) = 0 Then
EndDate = DateAdd("d", 365, StartDate) ' Leap year so add 365
Else
EndDate = DateAdd("d", 364, StartDate) ' Non leap year so add 364
End If
Label1.Caption = "Expires on " & EndDate
***End Code****

Enter a date in your text box hit the command button and you should get your answer in the label

HTH

Regards,

Dave
 
Lets not read too much into the date format.
If you use '01-jan-2004' it will always convert it correctly as it reads the month and works it out from there.
You normally only fall foul of date formats if you use
'01-04-2004' - is it 4th Of January, or 1st of May?


I am pretty sure all you need to do is

dtdate = dateadd("yyyy",1,dtdate)

DateAdd does not return invalid dates.
If you use the above to add 1 year to 29th Feb 2004, it should return 28th of Feb 2005, just as you wanted.



 
>'01-04-2004' - is it 4th Of January, or 1st of May?

Hope its not the 1st of May. If so what happened to April?
 
I agree with JeffTullin (except for May of course;> ). The beauty of using dateadd("yyyy",1,dtdate) is that it handles your leap-year and millennium-leap for you.

Don't know if you know about the millennium-leap (if that is what it is actually called), Digsy, but your code doesn't make provision for it. Generally speaking every millennium will be a leap-year except for every 4th millennium, which is not. We can say our code is not gonna last that long but that's what they said before - and we ended up with the Y2K problem...
 
Good point except that you got it backwards, every 4th millenium IS a leap year.

i.e.
2000 was
2100 wont be
2200 wont be (doubt it'll bother me tho)
2300 wont be
2400 will be

Basically a year has to be divisible by 4 and not by 100, however if it is divisible by 4, 100 and 400 then it is a leap year...
 
Thanks, yes. Just testing your knowledge;) Won't do it again:)
 
Digsy,
Just a couple technicalities: normal years have 365 days, leap years have 366 (your code has 364 and 365) and the years 2100, 2200, 2300, and 2400 are not millenia (millenia being a thousand years, ie 2000, 3000, 4000, etc).
 
Dear Digsy

thanks... simple code

its working fine

thanks lot

shan
 
dear digsy

some problem, i.e. starting date is jan or feb no problem
from mar to dec its problem ... disply day before date..

thnks

shan
 

the following code is working fine ...

some slight modification .. it works fine..

thanks all


Dim startdate As Date
Dim enddate As Date

startdate = CDate(crrifromdt)
If ((Year(startdate)) Mod 4) = 0 Then
enddate = DateAdd("d", 365, startdate) ' Leap year so add 365
Else
enddate = DateAdd("d", 364, startdate) ' Non leap year so add 364
End If

If ((Year(enddate)) Mod 4) = 0 Then
If Not Year(startdate) = Year(enddate) Then
enddate = DateAdd("d", -1, enddate)
End If
End If

If (Year(startdate) Mod 4 > 0) And (Year(enddate) Mod 4 = 0) Then
enddate = DateAdd("d", 2, enddate) '
End If

If (Year(startdate) Mod 4 = 0) And (Year(enddate) Mod 4 > 0) Then
enddate = DateAdd("d", -1, enddate) '
End If

If (Year(startdate) Mod 4 = 0) And (Year(enddate) Mod 4 > 0) And Month(startdate) < 3 Then
enddate = DateAdd(&quot;d&quot;, 1, enddate) '
End If

If (Year(startdate) Mod 4 > 0) And (Year(enddate) Mod 4 = 0) And Month(startdate) < 3 Then
enddate = DateAdd(&quot;d&quot;, -1, enddate) '
End If

Crritodt = Format(enddate, &quot;dd-mmm-yyyy&quot;)

once again thanks

shan

 
The easy way to check for leap year (subject to your international settings) is:

If Isdate(&quot;29 feb &quot; & year(now)) Then
' It's a leap year

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Below is a function which computes the expiry date in a simple and trouble-free way.
___
[tt]
Option Explicit

Function ExpriyDate(PurchaseDate As Date) As Date
ExpriyDate = DateSerial(Year(PurchaseDate) + 1, Month(PurchaseDate), Day(PurchaseDate) - 1)
End Function

Private Sub Form_Load()
MsgBox ExpriyDate(&quot;01-jan-2004&quot;) '12/31/2004
MsgBox ExpriyDate(&quot;29-feb-2004&quot;) '2/28/2005
MsgBox ExpriyDate(&quot;01-mar-2005&quot;) '2/28/2006
MsgBox ExpriyDate(Date) '1/20/2005
Unload Me
End Sub[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top