Parsing a date from string RRS feed

  • Question

  • Hello guys,

    I have the following two date string {22 Nov, 2017 @ 11:3pm | 6 Jan @ 5:59am}, and i want to make one function that parse both of them to Date value, any idea about how that can be done?
    Thanks in advance

    With kind regards,
    I. Sabri

    Friday, November 9, 2018 1:41 PM

All replies

  • Hi

    Does the second part actually have NO year?

    Regards Les, Livingston, Scotland

    Friday, November 9, 2018 2:16 PM
  • Hey,

    No it doen't

    Friday, November 9, 2018 2:26 PM
  • Hey,

    No it doen't


    Can I ask why?

    Do you only ever have pairs of Date\times in that very specific format?

    Any solution would only work for that specific format.

    Regards Les, Livingston, Scotland

    Friday, November 9, 2018 2:36 PM
  • Hey

    I am trying to get a date of an activity from Steam website and the date in the page is in that format, i have two issues parsing that date, the first one is that when the activity happend this year then no year will be giving, and the second issue is that days, minutes and hours doesn't have leading zeroes.

    Friday, November 9, 2018 2:42 PM
  • Hello,

    Hope below code helps you. i have added current year in date where year is absent and removed '@' and ',' from string before conversion

                Dim input_1 As String = "22 Nov, 2017 @ 11:3pm"
                Dim input_2 As String = "6 Jan @ 5:59am"
                Dim result_1 As DateTime = Convert.ToDateTime(input_1.Replace(",", String.Empty).Replace("@", String.Empty))
                Dim result_2 As DateTime = Convert.ToDateTime(input_2.Replace(",", String.Empty).Replace("@", DateTime.Now.Year))
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try

    Friday, November 9, 2018 2:54 PM
  • Hi

    Here is my version. This example needs Form1 with TextBox1, Label1 and Label2.



    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = "22 Nov, 2017 @ 11:3pm | 6 Jan @ 5:59am"
        Dim cd As New ConvertedDates
        cd = GetDateTime(TextBox1.Text)
        Label1.Text = cd.D1.ToString
        Label2.Text = cd.D2.ToString
        TextBox1.SelectionStart = 0
        TextBox1.SelectionLength = 0
      End Sub
      Function GetDateTime(s As String) As ConvertedDates
        Dim cd As New ConvertedDates
        s = s.Replace("@"c, "")
        Dim firstsplit() As String = Split(s, "|"c)
        If firstsplit(0).Length < 16 Then
          firstsplit(0) = firstsplit(0).Insert(7, "," & Now.Date.Year.ToString)
        End If
        If firstsplit(1).Length < 16 Then
          firstsplit(1) = firstsplit(1).Insert(7, "," & Now.Date.Year.ToString)
        End If
        cd.D1 = CDate(firstsplit(0))
        cd.D2 = CDate(firstsplit(1))
        Return cd
      End Function
      Class ConvertedDates
        Property D1 As DateTime
        Property D2 As DateTime
      End Class
    End Class

    Regards Les, Livingston, Scotland

    Friday, November 9, 2018 3:17 PM
  • Check this example too:

    Dim text = "22 Nov, 2017 @ 11:3pm | 6 Jan @ 5:59am"
    Dim a = text.Trim("{"c).Trim("}"c).Split("|"c)
    Dim text1 = a(0).Trim()
    Dim text2 = a(1).Trim()
    Dim c = CultureInfo.GetCultureInfo("en-US")
    Dim date1 = DateTime.ParseExact(text1, "d MMM, yyyy '@' h:m%tt", c)
    Dim date2 = DateTime.ParseExact(text2, {"d MMM, yyyy '@' h:m%tt", "d MMM '@' h:m%tt"}, c, DateTimeStyles.None)

    Saturday, November 10, 2018 9:19 AM