Formatting text box to show time range

  • Hello:

    This is no doubt a stupid question, but I'm new to report design, so here it is:

    I have a text box that I want to display a start time, then a hyphen, then an end time. (Ex: 4:30 pm - 6:00 pm)

    I put this in the text box's Value section:

    =Fields!TimeFrom.Value & " - " & Fields!TimeTo.Value

    It's giving me error Operator '&' is not defined for type 'TimeSpan' and string " - ".

    Can anyone help me with this?

  • Try:

    =formatdatetime(Fields!TimeFrom.Value) & " - " & formatdatetime(Fields!TimeTo.Value)

    You may have to play with the optional 'type' parameter to get it just right....

  • Hi, Bob:

    Thanks for the quick reply! I tried your suggestion and received the error message "Conversion from type 'TimeSpan' to type 'Date' is not valid." I'm not really familiar with the syntax here, so I'm not sure what other type to try. Any suggestions would be welcome! 🙂

  • The issue you're facing is that subtracting dates returns a timespan, not an int or a date. As you have discovered, the format function doesn't work with timespans.

    You could investigate using the datediff function, as has already been suggested. Or, try out the tostring method of the timespan:

    http://msdn2.microsoft.com/en-us/library/system.timespan.tostring(vs.71).aspx

    For example:

    =DateTime.MinValue.AddSeconds((Fields!date1.Value-Fields!date2.Value).TotalSeconds).ToString("mms.ff")

    from:

    http://social.msdn.microsoft.com/Forums/en-US/sqlreportingservices/thread/adcf1532-b018-47e4-ad5e-de5c8e6bdddb/

  • Hi, Bob:

    It seems that you've discovered my problem! I don't want to subtract the times at all--I only want to display the start and end times with a dash to serve as a separator. I suppose that if it's not possible to make the dash be read as text rather than an arithmetic operator, I'll have to replace it with "to". I was under the impression that putting quotes around the dash would designate it as text, but apparently I was mistaken. 🙁

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply