剛開始接觸RDLC報表,覺得RDLC報表提供的格式化工具太少,不像Crystal Report一樣那么多的API支持,用起來多少的靈活啊。
由於RDLC報表中有相關的日期格式字段,因此自然而然的就需要對日期字段進行格式化了,搜索了一些文章都是在介紹
FormatDateTime函數,其實用起來就發現FormatDateTime不是我要的料,這此先看看FormatDateTime的聲明吧:
Function
FormatDateTime(
ByVal Expression As DateTime,
Optional ByVal NamedFormat As DateFormat = DateFormat.GeneralDate
) As String
ByVal Expression As DateTime,
Optional ByVal NamedFormat As DateFormat = DateFormat.GeneralDate
) As String
DateFormat是一個枚舉,其值很少不太適合中國人制作報表的習慣,
NamedFormat 參數具有下列設置:
常量 | 說明 |
---|---|
DateFormat.GeneralDate | 顯示日期和/或時間。如果有日期部分,則用短日期格式顯示。如果有時間部分,則用長時間格式顯示。如果二者都有,則兩部分都顯示。 |
DateFormat.LongDate | 使用計算機的區域設置中指定的長日期格式來顯示日期。 |
DateFormat.ShortDate | 使用計算機的區域設置中指定的短日期格式來顯示日期。 |
DateFormat.LongTime | 使用計算機區域設置中指定的時間格式來顯示時間。 |
DateFormat.ShortTime | 使用 24 小時格式 (hh:mm) 顯示時間。 |
FormatDateTime(Fields!PlanStartDate.Value,DateFormat.ShortDate)
Command | Result |
FormatDateTime(Parameters!Date.Value,1) | Tuesday, April 10, 2007 |
FormatDateTime(Parameters!Date.Value,2) | 4/10/2007 |
FormatDateTime(Parameters!Date.Value,3) | 12:00:00 AM |
FormatDateTime(Parameters!Date.Value,4) | 00:00 |
如果我想通過FormatDateTime將日期格式顯示成“2012年4月”,那就很難了。
解決方法
The Format command and specify the exact format you require.
好Format函數現已隆重出場了,因此他確實可以解決我的問題,而且使用習慣與DateTime.ToString()類似,非常簡單:
Command | Result |
Format(Parameters!Date.Value,"dd-MM-yyyy") | 10-04-2007 |
Format(Parameters!Date.Value,"dd/MM/yyyy") | 10/04/2007 |
Format(Parameters!Date.Value,"MMM-dd-yyyy") | Apr-10-2007 |
Format(Parameters!Date.Value,"MMM-dd-yy") | Apr-10-07 |