简体   繁体   English

如何将“ dd / mm / yyyy”日期显示为“ yyyy-mm-dd”日期

[英]How can I display a “dd/mm/yyyy” date as a “yyyy-mm-dd” date

I keep seeing similar questions for Python and PHP, but I don't have access to those functions since I'm not using either of them. 我一直在Python和PHP上看到类似的问题,但是由于我没有使用它们,所以我无法访问这些函数。 The language I'm using is VB.NET. 我使用的语言是VB.NET。 So if I have: 所以,如果我有:

Dim date1 = "06/19/2015"

then I want to convert it to 然后我想将其转换为

"2015-06-19"

I can replace to "/" character using Replace() really easily, but I can't figure out how to order the day, month, and year around without it being too complex. 我可以很容易地使用Replace()替换为“ /”字符,但是我不知道如何在不复杂的情况下对日期,月份和年份进行排序。 Again, lots of similar questions on the web, but I'm having trouble finding one on that isn't using Python and PHP. 同样,网络上有很多类似的问题,但是我很难找到一个没有使用Python和PHP的问题。

Build up a list of possible formats and use DateTime.TryParseExact() . 建立可能的格式列表,并使用DateTime.TryParseExact() Then pass your desired output format to DateTime.ToString() . 然后将所需的输出格式传递给DateTime.ToString()

    Dim date1 As String = "06/19/2015"
    Dim allowedFormats() As String = {"M/d/yy", "M/dd/yy", "MM/d/yy", "MM/dd/yy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy", "MM/dd/yyyy"}
    Dim dt As DateTime
    If DateTime.TryParseExact(date1, allowedFormats, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dt) Then
        Dim date2 As String = dt.ToString("yyyy-MM-dd")
        Debug.Print(date1 & " --> " & date2)
    End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM