I need help formatting a string and doing a Datedifference after formatting

Posted on 2011-05-01
Last Modified: 2012-05-11
Hi Expert,
In my VB.Net application I have a string that is formatted as follows:
(7/1/2010 - 9/30/2010)
I need to remove the parenthesis, and the dash, then do a date diffence between the two dates,
and then format the dates so that the year is a two digit year.  Finally I need to put the string back together so that is ends up looking like this:
(7/1/10 - 9/30/10)

How can I do this?

Thanks in advance,
Question by:mainrotor
    LVL 39

    Accepted Solution

        Dim str As String
            Dim arr() As String
            Dim newstr As String
            Dim dt1, dt2 As Date
            Dim day As Integer
            str = "(7/1/2010 - 9/30/2010)"
            str = str.Replace("(", "")
            str = str.Replace(")", "")
            arr = str.Split("-")
            dt1 = Convert.ToDateTime(arr(0).ToString().Trim())
            dt2 = Convert.ToDateTime(arr(1).ToString().Trim())

            day = DateDiff("d", dt1, dt2)
            newstr = "(" & dt1.ToString("MM/dd/yy") & " - " & dt2.ToString("MM/dd/yy") & ")"

    LVL 32

    Expert Comment

    by:Robberbaron (robr)
    are you going to be using this more than once ?

    If so , rolling it into a class or a ByRef procedure will make life easier.
    LVL 32

    Expert Comment

    by:Robberbaron (robr)
    an example class attached.  I'm old school so using apratima's code wrapped in a Sub Proceedure proably makes more sense.
    dim Test1 as InterogateDates
    Test1.InputString = "(7/1/2010 - 9/30/2010)"
    Response.write (Test1.OutputString)
    Response.write (Test1.DaysDiff)
    Class InterogateDates
    	Private tmpS As String
    	Private Outputtmp As String = ""
    	Private m_daysdiff As TimeSpan
    	Public WriteOnly Property InputString() As String
    			tmpS = value
    			tmpS = tmpS.Replace("(", "")
    			tmpS = tmpS.Replace(")", "")
    			Dim arr As String() = tmpS.Split("-"C)
    			Dim dt1 As System.DateTime = Nothing
    			Dim dt2 As System.DateTime = Nothing
    			dt1 = Convert.ToDateTime(arr(0).ToString().Trim())
    			dt2 = Convert.ToDateTime(arr(1).ToString().Trim())
    			m_daysdiff = dt2.Subtract(dt1)
    			Outputtmp = "(" & dt1.ToString("MM/dd/yy") & " - " & dt2.ToString("MM/dd/yy") & ")"
    		End Set
    	End Property
    	Public ReadOnly Property OutputString() As String
    			Return Outputtmp
    		End Get
    	End Property
    	Public ReadOnly Property DaysDiff() As Double
    			Return m_daysdiff.TotalDays
    		End Get
    	End Property
    End Class

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now