RegExp and date from Ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yandhi
    New Member
    • Oct 2008
    • 7

    RegExp and date from Ajax

    Greetings once again.

    I was a little puzzled today when I searched the forums and wasn't able to find my answer. So I decided to post and pick at the available fishbowl of wisdom.

    I'm accessing data in SQL via ASP on one page and bringing that data over to my main page via Ajax. One of the pieces of data is a date as a string but there's too much in it so I need to strip some out.

    The date is coming over as M/D/YYYY H:MM:SS AM/PM

    (note, it's not always MM/DD/YYYY)

    I only want to get the month, day, and year to my page and I figured I would just do it in the Javascript.

    Would this be a good place for regular expressions? Seems to me that I should be able to get everything before the first space and omit the rest. Is that possible or what would be the best way of going about this particular problem?


    Thanks in advance,
    John
  • zaphod42
    New Member
    • Oct 2008
    • 55

    #2
    if the space is alway seperating the two parts of the date you could:

    Code:
    function getDate(datestring){
    var timeStamp=datestring.split(' ')
    return timeStamp[0]
    }
    there are plenty of ways to go about this in javascript :

    Code:
    var index=datestring.indexOf(' ')
    return datestring.substring(0,index)
    note that depending on how your string is recieved the (' ') may need to be replaced with ('&nbsp')

    also keep in mind that when dealing with strings or arrays in javascript the first character or string is always 0. Using the indexOf method is fun though, if you decide to use regexp with it, remember that if you use the /g (global) switch, you will be returned an array of positions that match your string, while if you don't use it you will be returned the position of only the first match.

    Comment

    • yandhi
      New Member
      • Oct 2008
      • 7

      #3
      Thanks for the reply!

      Your solution worked perfectly. However, I ended up doing the processing in on the vbscript the page that was writing my XML.

      I created a function that just gets the part of the date I need. Here is the code I came up with.

      Code:
      function GetDateParts(StringToSearch)
      	Dim regEx, CurrentMatch, CurrentMatches
      	Set regEx = New RegExp
      	regEx.Pattern = "([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})"
      	regEx.IgnoreCase = True
      	regEx.Global = True
      	regEx.MultiLine = True
      	Set CurrentMatches = regEx.Execute(StringToSearch)
      	If CurrentMatches.Count >= 1 Then
      		Set CurrentMatch = CurrentMatches(0)
      		If CurrentMatch.SubMatches.Count >= 1 Then
      			Dim year, month, day
      			year  = CurrentMatch.SubMatches(2)
      			month = CurrentMatch.SubMatches(0)
      			day   = CurrentMatch.SubMatches(1)
      			GetDateParts = day & "/" & month & "/" & year
      		End If
      	End If
      	Set regEx = Nothing
      end function
      Basically, just pass the date to the function and it does the rest.

      Thanks again for the response. Your assistance is much appreciated.

      Comment

      Working...