Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split() question

Status
Not open for further replies.

Gatchaman

Programmer
Jan 21, 2003
71
AU
Hey all, I'm wanting to split() a date field into the 3 elements (dd, mm and yyyy) and put it into 3 seperate XML elements. this shouldn't be a problem, however, the textbox that it's entered into has a default value of dd/mm/yyyy to try and show the user that that's the format they should use. But, I know I can't fully trust people, and most likely some will put in dd-mm-yyyy or dd\mm\yyyy (people using US formatted dates, mm/dd/yyy, is another problem altogether).

So, I was wondering if there's any way you can sue the split value with multiple things that it will split at ?

I feel this would be easier than:

Code:
dateArray = split(dateString,"/")

If Ubound(dateArray) - LBound(dateArray) < 2 Then

dateArray = split(dateString,&quot;-&quot;)

If Ubound(dateArray) - LBound(dateArray) < 2 Then

dateArray = split(dateString,&quot;\&quot;)

End If

End If

i don't know if this works yet even, I'm new to VBscript and just wrote this then.

Any help would be appreciated, thankyou.

Damon
 
This will answer part of the question:
splitdate = Split(date, &quot;/&quot;,-1,1)
month = splitdate(0)
day = splitdate(1)
year = splitdate(2)
You will probably need to take the input from the InputBox that the user enter in and read it char by char until you hit a / or -, then you can select which split to use. I will keep researching. Hope this helps a little bit though.
 
The &quot;standard&quot; way you'd do this would be the Localization facility built into the scripting engine's underlying libraries. By specifying VBScript you've made this somewhat easier, because it limits us to things like IE, or WSH, or Office applications - generally on Windows.

To start with, you have a string. The .value of an IE <input type=text> element is a string.

Then you use the IsDate( ) function on it to see if it looks legit. If not, tell the user it is in error.

Next you do a CDate( ) on it to get an actual Variant Date item from it.

You can then do range checks against Date literals like #3/1/2003# but if your script runs in the US that will be March 1, 2003. As a result, to check against a range of dates use something like:
Code:
Dim strEventDate, dtEventDate

strEventDate = frmRegister.txtEVentDate.value
If Not IsDate(strEventDate) Then
  --error--
Else
  dtEVentDate = CDate(strEventDate)
  If #April 1, 2003# > dtEventDate Or _
    dtEventDate > #December 31, 2003# Then
      --error--
  Else
    --date good, use it--
  End If
End If
While VBScript will handle many type coercions for you behind the scenes, to avoid nasty surprises (and debugging nightmares) you should generally use explicit type conversion functions where appropriate.

Once you have your date, you can pick it apart using the intrinsic DatePart( ) function. This makes it easy to pick out the day, month, year, day-of-the-week, or whatever else you need.

And all of this avoids the need to worry about the delimiters or the local order of subfields in a date string at run-time. It gives the user flexibility as well. They can use 5-1-2003 or 5/1/2003 and in a UK locale it'll mean January 5, 2003 either way. A US user would be saying May 1, 2003 by entering the same thing. They can even enter May 1, 2003 explicitly and if you've coded things as up above it will still work fine.

You get in trouble server-side such as with ASP though. Now you have trouble if your clients are world-wide and you'll need to get trickier. This is standard Localization stuff though, and VBScript handles it too. This is where you need to use the SetLocale( ) function.

You can convert a Date type back to a String easily where necessary using the FormatDateTime( ) function too.
 
Hrrmmmm, thanks dilettante :)

As a matter of fact, the processing of the date that I'm doing will be on server-side ASP, with international clients . . . I'll look up SetLocale().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top