【问题标题】:How to store date value to a string如何将日期值存储到字符串
【发布时间】:2014-10-30 11:16:23
【问题描述】:

我尝试了以下代码并得到了这样的结果 [3],[4]

j = DateDiff(DateInterval.Month, dtpFrom.Value, dtpTo.Value)

    Dim str2 As String = String.Empty
    Dim str3 As String = String.Empty

    Dim dat2 As Date = dtpFrom.Value
    For i As Integer = 1 To j + 1
        If str2.Trim().Length > 0 Then
            str2 = str2 & ","
        End If
        str2 = str2 & "[" & dat2.ToString("MM") & "]"

        dat2 = dat2.AddMonths(1)
    Next

我需要这样的 str -> [3] 三月,[4] 四月
有可能吗?

【问题讨论】:

  • 这是纯 vb 代码。为什么把sql server放在标签列表里?
  • MonthName函数如何使用?

标签: .net vb.net string date date-format


【解决方案1】:

您有几个选项,但在这个日期最容易阅读的是String.Format

应该这样做:

   Dim d As Date = DateTime.Now
   Dim s As String = String.Format("[{0:MM}] {0:MMM}", d)

   Debug.WriteLine(s) 'Outputs [10] Oct

更多信息请参见Custom Date and Time Format Strings

【讨论】:

  • 这个答案很有用(并且回答了这个问题(假设 OP 明白他必须适应他的要求))
【解决方案2】:

要获取月份名称,您必须使用dat2.ToString("MMM")

您可以找到here更多日期选项

【讨论】:

  • 这个答案很有用(但并不能真正回答问题)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多