471,854 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,854 software developers and data experts.

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,
Oct 16 '08 #1
2 1114
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)
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
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.

Similar topics

7 posts views Thread by serpent17 | last post: by
3 posts views Thread by zzzxtreme | last post: by
2 posts views Thread by brad | last post: by
3 posts views Thread by Sunil | last post: by
7 posts views Thread by =?Utf-8?B?U3R1?= | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.