473,856 Members | 1,754 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,
Oct 16 '08 #1
2 1244
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)
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
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

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
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 ----------------
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;
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,
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:
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(/^(.+?)(?:\?(?:(.*?)@)?(.+))?$/)
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?
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()...
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>
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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,...
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...
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...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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();...
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...
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

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.