By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,985 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

RegExp and date from Ajax

P: 7
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
Oct 16 '08 #1
Share this Question
Share on Google+
2 Replies


P: 55
if the space is alway seperating the two parts of the date you could:

Expand|Select|Wrap|Line Numbers
  1. function getDate(datestring){
  2. var timeStamp=datestring.split(' ')
  3. return timeStamp[0]
  4. }
there are plenty of ways to go about this in javascript :

Expand|Select|Wrap|Line Numbers
  1. var index=datestring.indexOf(' ')
  2. return datestring.substring(0,index)
  3.  
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.
Oct 16 '08 #2

P: 7
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.

Expand|Select|Wrap|Line Numbers
  1. function GetDateParts(StringToSearch)
  2.     Dim regEx, CurrentMatch, CurrentMatches
  3.     Set regEx = New RegExp
  4.     regEx.Pattern = "([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})"
  5.     regEx.IgnoreCase = True
  6.     regEx.Global = True
  7.     regEx.MultiLine = True
  8.     Set CurrentMatches = regEx.Execute(StringToSearch)
  9.     If CurrentMatches.Count >= 1 Then
  10.         Set CurrentMatch = CurrentMatches(0)
  11.         If CurrentMatch.SubMatches.Count >= 1 Then
  12.             Dim year, month, day
  13.             year  = CurrentMatch.SubMatches(2)
  14.             month = CurrentMatch.SubMatches(0)
  15.             day   = CurrentMatch.SubMatches(1)
  16.             GetDateParts = day & "/" & month & "/" & year
  17.         End If
  18.     End If
  19.     Set regEx = Nothing
  20. 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.
Oct 21 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.