473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

RegExp and date from Ajax

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
2 1232
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
yandhi
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

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

Similar topics

7
by: serpent17 | last post by:
Hello all, I have this line of numbers: 04242005 18:20:42-0.000002, 271.1748608, , , repeated several times in a text file and I would like each element to be part of a vector. how do...
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
3
by: zzzxtreme | last post by:
hello i have this function to check date (not mine) function (s_date) { // check format if (!re_dt.test(s_date)) return false; // check allowed ranges if (RegExp.$1 > 31 || RegExp.$2 > 12)...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
0
by: | last post by:
Hello, Is anyone have an example of RegExp expression to parse .EML files (Email Message)? I need to extract headers, HTML body, Textual body and attachments if any exists. I did some...
2
by: brad | last post by:
Hello all, I'm new to javascript--not too new to a few other programming languages--and I need your help deciphering the Regexp in the following string. Regular expresions are hard enough in...
3
by: Sunil | last post by:
Hi, I have a from with around 8 date fields. All these are entered by the user and are to validated for holidays. The holiday validation can only be done on server side. I am planning to use...
7
by: =?Utf-8?B?U3R1?= | last post by:
I have a ASP.NET Ajax app (using client library) calling ASP.NET Ajax-enabled web services. We are making use of the javascript proxies generated by ASP.NET Ajax. The problem we have is that the...
5
by: Andelys | last post by:
Hey I have to get some data from a html page into an other page. I know how to get the data by a simple AJAX request, but I don't know to find the data. The data i need to find is in a table,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.