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

Posted on 2011-05-01
Medium Priority
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
  • 2
LVL 39

Accepted Solution

Pratima Pharande earned 2000 total points
ID: 35502988
    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)
ID: 35504118
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)
ID: 35504169
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


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

807 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