• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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

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,
  • 2
1 Solution
Pratima PharandeCommented:
    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") & ")"

Robberbaron (robr)Commented:
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.
Robberbaron (robr)Commented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now