adamn11
adamn11

Reputation: 311

Subtracting Days from Current Date

I have an expression that shows the current day, month, and year:

=Mid(Today(),1,9)

How would I display the week before the current date? So if today is 6/26/2014, it would show 6/19/2014 in the box next to it. I tried using DateAdd and adding -7 but that gave me an error.

Upvotes: 4

Views: 18031

Answers (3)

Ash
Ash

Reputation: 33

I think replacing K D's answer DateInterval.Day by "d" and Today() by FORMAT(Cdate(today), "MM/dd/yyyy") would help

Upvotes: 0

rchacko
rchacko

Reputation: 2119

Just adding a bit more information regarding DateAdd function:

To get the date of week before, just reduce 7 days (add number -7) in the function like:

=DateAdd(DateInterval.Day, -7,Today())

You can add/reduce year, quarter month etc. in a similar way as shown below. Just change the number to the required length

=DateAdd(DateInterval.Year,-1,Today())

=DateAdd(DateInterval.Quarter,-1,Today())

=DateAdd(DateInterval.Month,-1,Today())

=DateAdd(DateInterval.DayOfYear,-1,Today())

=DateAdd(DateInterval.WeekOfYear,-1,Today())

=DateAdd(DateInterval.WeekDay,-1,Today())

=DateAdd(DateInterval.Hour,-1,Today())

=DateAdd(DateInterval.Minute,-1,Today())

=DateAdd(DateInterval.Second,-1,Today())

Upvotes: 0

K D
K D

Reputation: 5989

I tried the following expression in ReportBuilder and it worked fine...

=DateAdd(DateInterval.Day, -7,Today())

In case you still get an error then I would suggest to create a function using custom code in report and pass the date value and return expected value from that function. Wrap it with Try Catch block and return the exception as string in case it fails. Then you can check what is an error exactly. Take a look at the function here...

' Call following function in expression like this =Code.SubstractDate(YourDateValue)

Function SubstractDate(InputDate As DateTime) As String
Try
   Return =DateAdd(DateInterval.Day, -7,InputDate).ToString() ' Use your own format as you like
Catch ex as Exception
 Return ex.Message
End Function

Upvotes: 10

Related Questions