【问题标题】:Adding a 0 in a Date variable在 Date 变量中添加 0
【发布时间】:2015-08-07 12:50:32
【问题描述】:

我正在通过 Reporting Services 进行报告。我的一个文本框有一个表达式,它接收一个参数,该参数基本上是一个包含日期的字符串,例如“20110410”,我需要将此字符串转换为日期,添加 / 并将日期放在首位,月,年。

这已经完成了。但问题是我需要在低于 10 的日期和月份之后添加一个 0,所以我想要 01/04/2011 而不是 2011 年 1 月 4 日。

我不知道如何使用标签代码,如果代码显示不正确,请提前抱歉。

=IIF(Parameters!Uperiodo.Value = "Día",
IIF(Day(FormatDateTime(
CDate(mid(Parameters!Desde.Value,5,2) & "/" & mid(Parameters!Desde.Value,1,4) & "/" & mid(Parameters!Desde.Value,7,2)),DateFormat.ShortDate))<10,
"0"+Day(FormatDateTime(
CDate(mid(Parameters!Desde.Value,5,2) & "/" & mid(Parameters!Desde.Value,1,4) & "/" & mid(Parameters!Desde.Value,7,2)),DateFormat.ShortDate)),
(Day(FormatDateTime(
CDate(mid(Parameters!Desde.Value,5,2) & "/" & mid(Parameters!Desde.Value,1,4) & "/" & mid(Parameters!Desde.Value,7,2)),DateFormat.ShortDate))))
& "/" & 
Month(FormatDateTime(
CDate(mid(Parameters!Desde.Value,5,2) & "/" & mid(Parameters!Desde.Value,1,4) & "/" & mid(Parameters!Desde.Value,7,2)),DateFormat.ShortDate)) 
& "/" & 
Year(FormatDateTime(
CDate(mid(Parameters!Desde.Value,5,2) & "/" & mid(Parameters!Desde.Value,1,4) & "/" & mid(Parameters!Desde.Value,7,2)),DateFormat.ShortDate))
,
Month(FormatDateTime(
CDate(mid(Parameters!Desde.Value,5,2) & "/" & mid(Parameters!Desde.Value,1,4) & "/" & mid(Parameters!Desde.Value,7,2)),DateFormat.ShortDate)) 
& "/" & 
Year(FormatDateTime(
CDate(mid(Parameters!Desde.Value,5,2) & "/" & mid(Parameters!Desde.Value,1,4) & "/" & mid(Parameters!Desde.Value,7,2)),DateFormat.ShortDate))
)

谢谢!

编辑:对不起,我没有解释我们为什么这样做。我们需要这样做,因为需要下载报告的服务器具有英文格式(mm/dd/yyyy)。

最好的问候,

【问题讨论】:

  • DateTime.Parse 方法 - msdn.microsoft.com/en-us/library/…
  • 这是 VB.NET 代码吗?
  • 将字符串解析为 DateTime 变量 (DateTime.Parse) 只需使用正确的格式字符串 myDt.ToString("hh/mm/yyyy") 不需要所有可怕的 Mid 和 CDate 代码...尽管为什么是 @ 987654326@函数没有大写?那是你真正的 VB 代码吗?
  • 使用 Reporting Services Format 函数,所以这一切都归结为一个简单的 =Format(Parameters!Desde.Value,"dd/MM/yy") 但请确保您的参数之前定义为 DateTime你这样做。

标签: asp.net reporting-services


【解决方案1】:

如果这是 vb.net,就像您的标签所说的那样,您应该真正阅读您正在使用的类上的 documentationParseExact 会将字符串转换为 DateTime,ToString 会将 DateTime 转换回您想要的格式的字符串。

    Dim d As DateTime = DateTime.ParseExact("20110410", "yyyyMMdd", Nothing)

    Console.WriteLine(d.ToString("dd/MM/yyyy")) ' 10/04/2011

另外,您应该使用 Substring 而不是 mid。
如果你有一个数字,你可以附加一个 0 。

     Console.WriteLine(4.ToString("00")) ' 04
     Console.WriteLine(10.ToString("00")) ' 10

【讨论】:

  • I want 01/04/2011 所以 OP 可能需要根据他们的文化使用ToString(hh/MM/yyyy")
  • 将在 Reporting Services 服务器上读取此报告。报告服务 dll 在解决方案中。所以放 VB.NET 标签是我的错。
  • @NicolasMendez 我看到您已更改为 asp.net,它可能是 C# 或 VB.NET。我认为在你的情况下它是 vb.net。
【解决方案2】:

我找到了解决办法。

我知道代码很难看,最近(1 分钟前 :) 我意识到我可以使用报表属性中的“个性化代码”来执行变量等操作。

所以解决方案是使用 & 而不是 +。

最好的问候和感谢您的帮助。-

重要编辑: 事实上,我不必使用 IIF,我只需要为第一个 9 日期添加“0”& 就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多