【问题标题】:How do I determine the users local system date format如何确定用户本地系统日期格式
【发布时间】:2017-05-28 21:53:33
【问题描述】:

我正在尝试从 vb.net 确定系统日期格式,该格式必须与 cmd 提示符中显示的格式相匹配。

例如,如果您在 cmd 提示符下键入 date(您可能需要以管理员身份运行) 它将输出日期更改生效所需的格式,类似于:

The current date is: Sun 05/28/2017 Enter the new date: (mm-dd-yy)

我的任务栏中显示的当前日期也是05/28/2017

从 vb.net,我需要找出请求的格式,在本例中是 mm-dd-yy

我试过System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern(),它输出的dd/MM/yyyy与格式不匹配。

【问题讨论】:

  • 我不确定这一点,但鉴于请求输入的格式与输出格式不匹配,我想说输入格式是恒定的,与文化无关。跨度>

标签: vb.net datetime format


【解决方案1】:

按照 Window 的“日期”命令的官方文档,它只是硬编码输出,并且可以接受任何分隔符,只要它们是“/”、“.”或“-”。请参阅此处的文档: https://technet.microsoft.com/en-us/library/cc732776(v=ws.11).aspx

但是,System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern() 命令应该输出用户当前本地系统日期格式;那或 MSDN 文档是错误的。 https://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.shortdatepattern(v=vs.110).aspx

【讨论】:

    【解决方案2】:

    使用这个就可以了:

    Private Function CheckDateFormat1()
        Dim Format As String = DateTimeFormatInfo.CurrentInfo.ShortDatePattern
    
        If Format.Contains("MMM") Then
            Format = Format.Replace("MMM", "MM")
        ElseIf Format.Contains("MMMM") Then
            Format = Format.Replace("MMMM", "MM")
        End If
    
        If Format.Contains("dddd") Then
            Format = Format.Replace("dddd", "dd")
        End If
    
        Return Format
    End Function
    

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多