I'm trying to populate an expression (default value of a parameter) with an explicit time. How do I remove the time from the the "now" function?
-
1Check out Method 1: stackoverflow.com/a/19693553/2635532– Anup AgrawalFeb 5, 2016 at 17:17
-
2It is important to note that even if the SQL within <CommandText> returns a Date variable only, the rdl will still use DateTime. Therefore, it must be formatted by the rdl -- see various answers below.– JosephDoggieJan 15, 2020 at 14:08
14 Answers
Something like this:
=FormatDateTime(Now, DateFormat.ShortDate)
Where "Now" can be replaced by the name of the date/time field that you're trying to convert.)
For instance,
=FormatDateTime(Fields!StartDate.Value, DateFormat.ShortDate)
-
6As my date parameter was a datetime, I required an additional CDate() around the above expression.– Nick.McMar 13, 2014 at 1:51
-
1Won't work if the report is required in Excel format. FormatDateTIme is string function. Sure it strips off the time, but the result is a string date column which when sorting will be sorted as a string from left to right. The user would have to convert the column to a real Date using Text to Columns. Jun 21, 2016 at 0:17
-
1The point is, you cannot do what the OP asked in Date only format. You're stuck with string dates. Jun 21, 2016 at 0:20
-
This gave me exactly what I needed. I had a DOB column that SSRS assumed needed to add a time to. Microsoft really needs to update this software.– user12217470Jan 28, 2021 at 17:23
Since SSRS utilizes VB, you can do the following:
=Today() 'returns date only
If you were to use:
=Now() 'returns date and current timestamp
-
9
-
1To clarify,
Today()
returns the date only in terms of value, but the datatype returned isDateTime
. Nov 14, 2018 at 16:00
=CDate(Now).ToString("dd/MM/yyyy")
Although you are hardcoding the date formart to a locale.
-
2+1 for the concise answer that, as a matter of fact, was very helpful to me.– jasoncoJan 20, 2010 at 10:52
-
1As I mentioned above, this works but turns the column into a string. It's still not a true DATE only datatype. Jun 21, 2016 at 0:19
If you have to display the field on report header then try this... RightClick on Textbox > Properties > Category > date > select *Format (Note this will maintain the regional settings).
Since this question has been viewed many times, I'm posting it... Hope it helps.
-
1What if it gets ignored for whatever reason? E.g. date is still shown with hours and minutes. Any idea why this could be happening?– AlexMar 3, 2016 at 10:43
-
2If you have a function specified for the value of the TextBox/Placeholder then it will override the formatting options on this screen.– WayneMay 31, 2016 at 20:04
-
1Saved my day. Event though it is easily visible in the options many wont look here for formatting option. Thanks a lot.– RohithJan 30, 2018 at 9:03
If expected data format is MM-dd-yyyy
then try below,
=CDate(Now).ToString("MM-dd-yyyy")
Similarly you can try this one,
=Format(Today(),"MM-dd-yyyy")
Output: 02-04-2016
Note:
Now()
will show you current date and time stamp
Today()
will show you Date only not time part.
Also you can set any date format instead of MM-dd-yyyy
in my example.
In the format property of any textbox field you can use format strings:
e.g. D/M/Y, D, etc.
-
4The question is referring to the default value of a report parameter, so setting the format on a textbox won't work. Also, "D/M/Y" won't give the expected result, try "dd/MM/yyyy" instead. Jul 31, 2012 at 8:24
One thing that might help others is that you can place: =CDate(Now).ToString("dd/MM/yyyy")
in the Format String Property of SSRS which can be obtained by right clicking the column. That is the cleanest way to do it. Then your expression won't be too large and difficult to visually "parse" :)
FormatDateTime(Parameter.StartDate.Value)
-
1You are missing the second parameter of the expression FormatDateTime(DateField, DateFormat) Jan 23, 2014 at 13:31
I'm coming late in the game but I tried all of the solutions above! couldn't get it to drop the zero's in the parameter and give me a default (it ignored the formatting or appeared blank). I was using SSRS 2005 so was struggling with its clunky / buggy issues.
My workaround was to add a column to the custom [DimDate] table in my database that I was pulling dates from. I added a column that was a string representation in the desired format of the [date] column. I then created 2 new Datasets in SSRS that pulled in the following queries for 2 defaults for my 'To' & 'From' date defaults -
'from'
SELECT Datestring
FROM dbo.dimDate
WHERE [date] = ( SELECT MAX(date)
FROM dbo.dimdate
WHERE date < DATEADD(month, -3, GETDATE()
)
'to'
SELECT Datestring
FROM dbo.dimDate
WHERE [date] = ( SELECT MAX(date)
FROM dbo.dimdate
WHERE date <= GETDATE()
)
My solution for a Date/Time parameter:
=CDate(Today())
The trick is to convert back to a DateTime as recommend Perhentian.
Found the solution from here
This gets the last second of the previous day:
DateAdd("s",-1,DateAdd("d",1,Today())
This returns the last second of the previous week:
=dateadd("d", -Weekday(Now), (DateAdd("s",-1,DateAdd("d",1,Today()))))
-
7I stumbled here from google.. but.. this doesn't answer your question. (why do you care about the last second?) However, you did beat RSolberg (who correctly answered it). Maybe you should edit your accepted answer to reference his correct answer?– itchiAug 26, 2010 at 0:29
-
3DateAdd("s",-1,DateAdd("d",1,Today()) returns the last second of Today, not previous day - and there's a closing backet missing to make it work Jul 31, 2012 at 8:20
This should be done in the dataset. You could do this
Select CAST(CAST(YourDateTime as date) AS Varchar(11)) as DateColumnName
In SSRS
Layout, just do this =Fields!DateColumnName.Value
-
Won't strip off the time component is the DateColumnName is a DateTime datatype. The OP wants the date only. Jun 21, 2016 at 0:13
Just concatenate a string to the end of the value:
Fields!<your field>.Value & " " 'test'
and this should work!
-
1Hi Nathan, Would you please elaborate on your editing ?. sorry i am kind of new to stackoverflow. Regards!– amadJul 14, 2017 at 15:33