none
How to keep the date format and only change the month to MM, day to DD, hour to HH? RRS feed

  • Question

  • I converted the filetime to local date, but I need to change the month to "MM", day to "dd" and hour to "HH" because I will sort the items. I known to use the .Tostring(yyyy-MM-dd HH:mm:ss) format the date string, but I do not know the local time format on client.

    For example the local time format on client is "YYYY/M/D hh:mm:ss",  how to change my string to "yyyy/MM/dd HH:mm:ss", I cannot use the .Tostring("yyyy/MM/dd HH:mm:ss") because I do not know client's date format.


    • Edited by gaxjyxq Friday, February 21, 2020 10:57 AM
    Friday, February 21, 2020 10:56 AM

Answers

  • This is my code, it works well.

      Public Shared Function ConvertDateToSpecifiedFormat(DT As DateTime) As String
            Try
                Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
                Dim shortDatePattern As String = ci.DateTimeFormat.ShortDatePattern

                If Regex.Split(shortDatePattern, "yyyy").Length = 1 Then 
                    shortDatePattern = shortDatePattern.Replace("yy", "yyyy")
                End If
                If Regex.Split(shortDatePattern, "MM").Length = 1 Then '
                    shortDatePattern = shortDatePattern.Replace("M", "MM")
                End If
                If Regex.Split(shortDatePattern, "dd").Length = 1 Then 
                    shortDatePattern = shortDatePattern.Replace("d", "dd")
                End If
                Return DT.ToString(shortDatePattern & " HH:mm:ss")
            Catch ex As Exception
            End Try
            Return DT.ToString
        End Function

    • Marked as answer by gaxjyxq Monday, February 24, 2020 9:30 AM
    Monday, February 24, 2020 9:29 AM

All replies

  • Hi,
    parse the string with CurrentCulture to DateTime and then write new string.

          Dim d1 = Now
          Dim s1 = d1.ToString("yyyy/M/d hh:mm:ss")
    
          Console.WriteLine(s1)
    
          Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-us")
          Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-us")
    
          Dim d2 = Date.Parse(s1)
          Dim s2 = d2.ToString("yyyy/MM/dd HH:mm:ss")
    
          Console.WriteLine(s2)


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks



    Friday, February 21, 2020 11:19 AM
  • Keep your current conversion to local format, and use the date values directly in your sorting procedures, without conversion to strings.

    Friday, February 21, 2020 12:00 PM
  • If i do not convert the date, when i click the column header, the sort is not right.

    For example "2019/5/20">"2019/12/20", "6:15">"12:15", i used the AdvTree control, i feel need to custome the IComparer for the colmun but i do not how to create the IComparer to sort date for the column. 

    Friday, February 21, 2020 1:08 PM
  • I cannot use the d1.ToString because i need to keep the local date format, and i do not know the client's local date format.
    Friday, February 21, 2020 1:12 PM
  • See Microsoft docs for IComparer example

    https://docs.microsoft.com/en-us/dotnet/standard/collections/comparisons-and-sorts-within-collections


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, February 21, 2020 1:25 PM
    Moderator
  • Hi,
    you must parse input string from local use with local settings of application (system). This DataTime value you can write in target format.
          ' simulate local format, e.g. spanish
          Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("es-es")
          Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("es-es")
    
          ' user type date and time in spanish format
          Dim s1 As String = "21/2/2020 04:28:32"
          Console.WriteLine(s1)
    
          ' parse string to get DateTime from local date format
          Dim d2 = Date.Parse(s1)
    
          ' convert date to string to target format 
          Dim s2 = d2.ToString("yyyy/MM/dd HH:mm:ss")
          Console.WriteLine(s2)


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Friday, February 21, 2020 3:55 PM

  • For example the local time format on client is "YYYY/M/D hh:mm:ss",  how to change my string to "yyyy/MM/dd HH:mm:ss", I cannot use the .Tostring("yyyy/MM/dd HH:mm:ss") because I do not know client's date format.


    No it is not, the format on the client is just a Int64 value which has a starting point of 1/1/1 00:00

    What you show is a representation of that in characters. (ToString) and therefore you cannot easily sort it in that strange format. 

    However, you can add an extra tag in your format as a string and then sort it.  


    Success
    Cor

    Friday, February 21, 2020 9:41 PM
  • This is my code, it works well.

      Public Shared Function ConvertDateToSpecifiedFormat(DT As DateTime) As String
            Try
                Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
                Dim shortDatePattern As String = ci.DateTimeFormat.ShortDatePattern

                If Regex.Split(shortDatePattern, "yyyy").Length = 1 Then 
                    shortDatePattern = shortDatePattern.Replace("yy", "yyyy")
                End If
                If Regex.Split(shortDatePattern, "MM").Length = 1 Then '
                    shortDatePattern = shortDatePattern.Replace("M", "MM")
                End If
                If Regex.Split(shortDatePattern, "dd").Length = 1 Then 
                    shortDatePattern = shortDatePattern.Replace("d", "dd")
                End If
                Return DT.ToString(shortDatePattern & " HH:mm:ss")
            Catch ex As Exception
            End Try
            Return DT.ToString
        End Function

    • Marked as answer by gaxjyxq Monday, February 24, 2020 9:30 AM
    Monday, February 24, 2020 9:29 AM