How do i Minus a Year from the current year in ASP

# Thread: How do i Minus a Year from the current year in ASP

1. Senior Member
Join Date
Dec 1969
Posts
368

## How do i Minus a Year from the current year in ASP

I have the following<BR><BR>&#060;%Response.Write fncFmtDate(Now(), " %m/%d/%y") %&#062;<BR><BR>which represents the date which is generated by the snippet below.. ideally i would liek to minus one year from the current year is anyone aware how i do this??<BR><BR>regards<BR><BR><BR>Musa<BR><BR>&#060 ;%<BR>Function fncGetDayOrdinal( _<BR> byVal intDay _<BR> )<BR> &#039; Accepts a day of the month as an integer and returns the<BR> &#039; appropriate suffix<BR><BR> Dim strOrd<BR><BR> Select Case intDay<BR> Case 1, 21, 31<BR> strOrd = "st"<BR> Case 2, 22<BR> strOrd = "nd"<BR> Case 3, 23<BR> strOrd = "rd"<BR> Case Else<BR> strOrd = "th"<BR> End Select<BR><BR> fncGetDayOrdinal = strOrd<BR>End Function &#039; fncGetDayOrdinal<BR><BR><BR><BR>Function fncFmtDate( _<BR> byVal strDate, _<BR> byRef strFormat _<BR> )<BR> &#039; Accepts strDate as a valid date/time,<BR> &#039; strFormat as the output template.<BR> &#039; The function finds each item in the<BR> &#039; template and replaces it with the<BR> &#039; relevant information extracted from strDate<BR><BR> &#039; Template items (example)<BR> &#039; %m Month as a decimal (02)<BR> &#039; %B Full month name (February)<BR> &#039; %b Abbreviated month name (Feb )<BR> &#039; %d Day of the month (23)<BR> &#039; %O Ordinal of day of month (eg st or rd or nd)<BR> &#039; %j Day of the year (54)<BR> &#039; %Y Year with century (1998)<BR> &#039; %y Year without century (98)<BR> &#039; %w Weekday as integer (0 is Sunday)<BR> &#039; %a Abbreviated day name (Fri)<BR> &#039; %A Weekday Name (Friday)<BR> &#039; %H Hour in 24 hour format (24)<BR> &#039; %h Hour in 12 hour format (12)<BR> &#039; %N Minute as an integer (01)<BR> &#039; %n Minute as optional if minute &#060;&#062; 0<BR> &#039; %S Second as an integer (55)<BR> &#039; %P AM/PM Indicator (PM)<BR><BR> On Error Resume Next<BR><BR> Dim intPosItem<BR> Dim int12HourPart<BR> Dim str24HourPart<BR> Dim strMinutePart<BR> Dim strSecondPart<BR> Dim strAMPM<BR><BR> &#039; Insert Month Numbers<BR> strFormat = Replace(strFormat, "%m", _<BR> DatePart("m", strDate), 1, -1, vbBinaryCompare)<BR><BR> &#039; Insert non-Abbreviated Month Names<BR> strFormat = Replace(strFormat, "%B", _<BR> MonthName(DatePart("m", strDate), _<BR> False), 1, -1, vbBinaryCompare)<BR><BR> &#039; Insert Abbreviated Month Names<BR> strFormat = Replace(strFormat, "%b", _<BR> MonthName(DatePart("m", strDate), _<BR> True), 1, -1, vbBinaryCompare)<BR><BR> &#039; Insert Day Of Month<BR> strFormat = Replace(strFormat, "%d", _<BR> DatePart("d",strDate), 1, _<BR> -1, vbBinaryCompare)<BR><BR> &#039; Insert Day of Month Ordinal (eg st, th, or rd)<BR> strFormat = Replace(strFormat, "%O", _<BR> fncGetDayOrdinal(Day(strDate)), _<BR> 1, -1, vbBinaryCompare)<BR><BR> &#039; Insert Day of Year<BR> strFormat = Replace(strFormat, "%j", _<BR> DatePart("y",strDate), 1, _<BR> -1, vbBinaryCompare)<BR><BR> &#039; Insert Long Year (4 digit)<BR> strFormat = Replace(strFormat, "%Y", _<BR> DatePart("yyyy",strDate), 1, _<BR> -1, vbBinaryCompare)<BR><BR> &#039; Insert Short Year (2 digit)<BR> strFormat = Replace(strFormat, "%y", _<BR> Right(DatePart("yyyy",strDate),2), _<BR> 1, -1, vbBinaryCompare)<BR><BR> &#039; Insert Weekday as Integer (eg 0 = Sunday)<BR> strFormat = Replace(strFormat, "%w", _<BR> DatePart("w",strDate,1), 1, _<BR> -1, vbBinaryCompare)<BR><BR> &#039; Insert Abbreviated Weekday Name (eg Sun)<BR> strFormat = Replace(strFormat, "%a", _<BR> WeekDayName(DatePart("w",strDate,1),True), 1, _<BR> -1, vbBinaryCompare)<BR><BR> &#039; Insert non-Abbreviated Weekday Name<BR> strFormat = Replace(strFormat, "%A", _<BR> WeekDayName(DatePart("w",strDate,1),False), 1, _<BR> -1, vbBinaryCompare)<BR><BR> &#039; Insert Hour in 24hr format<BR> str24HourPart = DatePart("h",strDate)<BR> If Len(str24HourPart) &#060; 2 then str24HourPart = "0" & _<BR> str24HourPart<BR> strFormat = Replace(strFormat, "%H", str24HourPart, 1, _<BR> -1, vbBinaryCompare)<BR><BR> &#039; Insert Hour in 12hr format<BR> int12HourPart = DatePart("h",strDate) Mod 12<BR> If int12HourPart = 0 then int12HourPart = 12<BR> strFormat = Replace(strFormat, "%h", int12HourPart, 1, _<BR> -1, vbBinaryCompare)<BR><BR> &#039; Insert Minutes<BR> strMinutePart = DatePart("n",strDate)<BR> If Len(strMinutePart) &#060; 2 then _<BR> strMinutePart = "0" & strMinutePart<BR> strFormat = Replace(strFormat, "%N", strMinutePart, _<BR> 1, -1, vbBinaryCompare)<BR><BR> &#039; Insert Optional Minutes<BR> If CInt(strMinutePart) = 0 then<BR> strFormat = Replace(strFormat, "%n", "", 1, _<BR> -1, vbBinaryCompare)<BR> Else<BR> If CInt(strMinutePart) &#060; 10 then _<BR> strMinutePart = "0" & strMinutePart<BR> strMinutePart = ":" & strMinutePart<BR> strFormat = Replace(strFormat, "%n", strMinutePart, _<BR> 1, -1, vbBinaryCompare)<BR> End if<BR><BR> &#039; Insert Seconds<BR> strSecondPart = DatePart("s",strDate)<BR> If Len(strSecondPart) &#060; 2 then _<BR> strSecondPart = "0" & strSecondPart<BR> strFormat = Replace(strFormat, "%S", strSecondPart, 1, _<BR> -1, vbBinaryCompare)<BR><BR> &#039; Insert AM/PM indicator<BR> If DatePart("h",strDate) &#062;= 12 then<BR> strAMPM = "PM"<BR> Else<BR> strAMPM = "AM"<BR> End If<BR><BR> strFormat = Replace(strFormat, "%P", strAMPM, 1, _<BR> -1, vbBinaryCompare)<BR><BR> fncFmtDate = strFormat<BR><BR> &#039;If there is an error output its value<BR> If err.Number &#060;&#062; 0 then<BR> Response.Clear<BR> Response.Write "ERROR " & err.Number & _<BR> ": fmcFmtDate - " & err.Description<BR> Response.Flush<BR> Response.End<BR> End if<BR>End Function &#039; fncFmtDate<BR>%&#062;

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: How do i Minus a Year from the current year in

Holy **** Batman!<BR><BR>What are you trying to Do? seems a lotta code!<BR><BR>instead of Now() why not use Date?<BR><BR>dateadd("y", 1, Date()) &#039;adds one year to current date

3. Senior Member
Join Date
Dec 1969
Posts
368

## So far is is how far i go.. but still kinda stuck

hey guys i have managed to get so far...<BR><BR>&#060;%=DateAdd("yyyy", -1, date())%&#062; <BR><BR>which works BUT.. to take the shorter root.. i might just catpure the last 4 figures so i was looking at something like this...<BR><BR>&#060;%=right ???????? .Value,3)%&#062;<BR><BR><BR>but currently with the date thing i have above.. i get an error.<BR><BR>Any help ???<BR><BR>regards<BR>

4. Senior Member
Join Date
Dec 1969
Posts
368

## Dude..

I sort did that...<BR><BR>&#060;%=dateadd("y", -1, Date())%&#062; <BR><BR><BR>but it only subtracts the day of the month and not the year.. however if i do this..<BR><BR>&#060;%=DateAdd("yyyy", -1, date())%&#062; <BR><BR>i subtracts the year.. but shows the who date... i.e<BR>17/10/2002 <BR><BR>but i deally i just wanna get the year to display i.e<BR><BR>2002

5. Senior Member
Join Date
Dec 1969
Posts
11,247

## Duh

6. Senior Member
Join Date
Dec 1969
Posts
368

## ease it dude!!!

ta very muchly mate.. dnd thanks to you :o)<BR><BR>

7. Junior Member
Join Date
Dec 1969
Posts
5

## RE: So far is is how far i go.. but still kinda st

White Knight is right, if you try something like<BR>right(cstr(year(DateAdd("yyyy", -1, date())),4)<BR>it will go awry if your system date format is "yyyy/mm/dd" instead of "dd/mm/yy" (or "mm/dd/yy" if you&#039;re American).<BR><BR>Now, if you want *just* the last 2 digits of the year:<BR>right(cstr(year(DateAdd("yyyy", -1, date())),2)<BR>

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Okay, I *have* to butt in...

What&#039;s wrong with simply<BR> Year(Date())-1<BR>????<BR><BR>Why do we need DateAdd()???<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•