473,703 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegExp and date from Ajax

7 New Member
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 1240
zaphod42
55 New Member
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 New Member
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
1734
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 I do this ? I am not very capable in using
3
9169
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 RegExp object to be non-greedy as using the ? quantifier doesn't seem to work. --------- SAMPLE ----------------
3
7067
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) return false;
4
7475
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 "PRACTICO". What's the best way to do this? TIA,
0
1638
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 example, but not sure that its a good start: ^Message-ID: (?<messageid>.*)\nFrom: (?<from>.*)\nTo: (?<to>.*)\nSubject: (?<subject>.*)\nDate: (?<date>.*)\nMIME-Version: (?<mime>.*)\nContent-Type:
2
4749
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 Python, and since I am new to javascript they are even harder. Well here's the string, thanks for any and all help I receive. document.URL.match(/^(.+?)(?:\?(?:(.*?)@)?(.+))?$/)
3
1690
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 AJAX for these date validations. The validation must be done on onChange event of the date fields. What is the best way to do this using AJAX? Should i return XML data or JSON or just a simple text?
7
4265
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 UTC dates sent by the browser are being adjusted for timezones by the server, but we do not want dates adjusted for timezones. For example, the user enters the date of a transaction as '25 May 2007'. That ends up as a javascript Date()...
5
1617
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, and there are about 100 rows in it. I only need to get one. The only thing i got is the persons ID. The row looks like this: <tr class="Lyscell"> <td>48</td> <td>29</td>
0
8667
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9116
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9013
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7860
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4428
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2445
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.