Format datetime day with st, nd, rd, th

前端 未结 3 541
谎友^
谎友^ 2021-01-24 02:10

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

相关标签:
3条回答
  • 2021-01-24 02:45

    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)
    
    0 讨论(0)
  • 2021-01-24 02:54

    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"
      
    0 讨论(0)
  • 2021-01-24 02:59

    I found an easy way to do it. Please see example below;

    = DAY(Globals!ExecutionTime) & 
        SWITCH(
        DAY(Globals!ExecutionTime)= 1 OR DAY(Globals!ExecutionTime) = 21 OR DAY(Globals!ExecutionTime)=31, "st",
        DAY(Globals!ExecutionTime)= 2 OR DAY(Globals!ExecutionTime) = 22 , "nd",
        DAY(Globals!ExecutionTime)= 3 OR DAY(Globals!ExecutionTime) = 23 , "rd",
        true, "th"
        )
    
    0 讨论(0)
提交回复
热议问题