Format datetime day with st, nd, rd, th

陌路散爱 提交于 2019-12-02 09:24:59

问题


I'm creating a report in SSRS and across the top I have a header with a placeholder for "Last Refreshed" which will show when the report last ran.

My function in the placeholder is simply this:

=Format(Now, "dddd dd MMMM yyyy hh:mm tt")

Which looks like this:

Monday 22 September 2015 09:46 AM

I want to format the day value with the English suffix of st, nd, rd and th appropriately.

I can't find a built in function for this and the guides I've looked at so far seem to describe doing it on the SQL side with stored procedures which I don't want. I'm looking for a report side solution.

I thought I could get away with an ugly nested IIF that did it but it errors out despite not giving me any syntax errors (whitespace is just for readability).

=Format(Now, "dddd " + 

IIF(DAY(Now) = "1", "1st", 
IIF(DAY(Now) = "21","21st",
IIF(DAY(Now) = "31","31st",
IIF(DAY(Now) = "2","2nd",
IIF(DAY(Now) = "22","22nd",
IIF(DAY(Now) = "3","3rd",
IIF(DAY(Now) = "23","23rd",
DAY(Now) + "th"))))))) 

+ " MMMM yyyy hh:mm tt")

In any other language I would have nailed this ages ago, but SSRS is new to me and so I'm not sure about how to do even simple string manipulation. Frustrating!

Thanks for any help or pointers you can give me.

Edit: I've read about inserting VB code into the report which would solve my problem, but I must be going nuts because I can't see where to add it. The guides say to go into the Properties > Code section but I can't see that.


回答1:


Go to layout view. Select Report Properties.Click on the "Code" tab and Enter this code

Public Function ConvertDate(ByVal mydate As DateTime) as string
  Dim myday as integer
  Dim strsuff As String
  Dim mynewdate As String
  'Default to th
  strsuff = "th" 
  myday = DatePart("d", mydate)
  If myday = 1 Or myday = 21 Or myday = 31 Then strsuff = "st"
  If myday = 2 Or myday = 22 Then strsuff = "nd"
  If myday = 3 Or myday = 23 Then strsuff = "rd"
  mynewdate = CStr(DatePart("d", mydate)) + strsuff + " " +      CStr(MonthName(DatePart("m", mydate))) + " " + CStr(DatePart("yyyy", mydate))
 return mynewdate
End function

Add the following expression in the required field. I've used a parameter, but you might be referencing a data field?

=code.ConvertDate(Parameters!Date.Value)



回答2:


Right Click on the Textbox, Go To Textbox Properties then, Click on Number tab, click on custom format option then click on fx button in black.

Write just one line of code will do your work in simpler way:

A form will open, copy the below text and paste there to need to change following text with your database date field.

Fields!FieldName.Value, "Dataset"

  1. Replace FieldName with your Date Field
  2. Replace Dataset with your Dateset Name

    ="d" + switch(int(Day((Fields!FieldName.Value, "Dataset"))) mod 
    10=1,"'st'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 = 
    2,"'nd'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 = 
    3,"'rd'",true,"'th'") + " MMMM, yyyy"
    


来源:https://stackoverflow.com/questions/32712572/format-datetime-day-with-st-nd-rd-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!