none
How to extract the date within a string and make a judgment with the day? RRS feed

  • Question

  • Hello,

    How to extract the date within a string and make a judgment with the day?

    Best Regards,

    CLG

    Wednesday, August 22, 2018 12:29 AM

Answers

  • Hi everyone

    I have solved my problem, thank you.

    Imports System.Text.RegularExpressions
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim source As String = "exec PROC_RPT_SAL003 2014-07-15,2015-07-01123,'12356"
            Dim reg As Regex = New Regex("[\d]{4}\-[\d]{2}\-[\d]{2}")
            Dim mc As MatchCollection = reg.Matches(source)
    
            For Each m As Match In mc
                MessageBox.Show(m.Value)
            Next
        End Sub
    End Class

    Saturday, August 25, 2018 12:53 PM

All replies

  • Hi

    Might help us to help you if you supply sufficient detail. Show an example of the String, and explain what a 'judgement with the day' is. You would also need to tell if the String is always of the same make-up. i.e. is it always the same number of leading/trailing characters; is the date part of the String always of a particular format? Is the string  delineated in any way i.e. is there any fixed character before/after the date portion of the string to indicate that this is the date portion?


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, August 22, 2018 1:05 AM
    Wednesday, August 22, 2018 1:03 AM
  • As Les pointed out you need to be specific, otherwise someone might come up with perhaps this which can be way off base or not.

    Convert a string to a date, get the day which from here you can make a judgement on the day via an If statement for instance.

    Dim someDateAsString As String = "08/20/2018"
    Dim d As Date
    If Date.TryParse(someDateAsString, d) Then
        Console.WriteLine(d.Day)
    End If
    So please provide specifics.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, August 22, 2018 1:40 AM
    Moderator
  • Hi Karen

    I read the question as a Date string embedded in a longer string or something like that. Perhaps something like a CSV string?


    Regards Les, Livingston, Scotland

    Wednesday, August 22, 2018 1:54 AM
  • Hi,

    You need to provide a string,like:

    "<div class='newsfunction'><span class='functiontuData'>2018/08/22</span>"

    If so, you can use regular expressions to solve the problem.

    Imports System.Text.RegularExpressions
    
    Module Module1
    
        Sub Main()
            Dim reg As Regex = New Regex("<span class='functiontuData'>(?<date>.*?)</span>", RegexOptions.CultureInvariant)
            For Each m As Match In reg.Matches("<div class='newsfunction'><span class='functiontuData'>2018/08/22</span>")
    
                Dim da As String = m.Groups("date").Value
    
                Dim dt As DateTime = System.DateTime.Now.AddDays(1)
    
                If DateTime.TryParse(da, dt) Then
    
                    If dt.ToShortDateString() = System.DateTime.Now.ToShortDateString() Then
                        Dim res As String = dt.ToString("yyyy/MM/dd")
                        Console.WriteLine("true")
                        Console.ReadLine()
                    Else
                        Console.WriteLine("false")
                        Console.ReadLine()
                    End If
                End If
            Next
        End Sub
    
    End Module
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 22, 2018 2:17 AM
  • Hi Karen

    I read the question as a Date string embedded in a longer string or something like that. Perhaps something like a CSV string?


    Regards Les, Livingston, Scotland

    Exactly, I wanted the OP to be more specific by showing them that the post can be interpreted many ways "as is". So I totally agree with you.

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, August 22, 2018 2:18 AM
    Moderator
  • Hi everyone

    I have solved my problem, thank you.

    Imports System.Text.RegularExpressions
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim source As String = "exec PROC_RPT_SAL003 2014-07-15,2015-07-01123,'12356"
            Dim reg As Regex = New Regex("[\d]{4}\-[\d]{2}\-[\d]{2}")
            Dim mc As MatchCollection = reg.Matches(source)
    
            For Each m As Match In mc
                MessageBox.Show(m.Value)
            Next
        End Sub
    End Class

    Saturday, August 25, 2018 12:53 PM
  • In a case like this, it is OK to mark your own post as an answer in order to close the thread.

    -EDIT-

    By the way, good job researching and figuring out how to solve your own problem! :)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Saturday, August 25, 2018 2:53 PM
    Moderator