Mombu the Microsoft Forum sponsored links

Go Back   Mombu the Microsoft Forum > Microsoft > formatting a date
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 4th August 19:18
middletree
External User
 
Posts: 1
Default formatting a date



Seems this question would be commonly asked, but my search didn't yield any
threads on this, so . . .

Is there any code out there for converting dates entered into a text field
like this:
12162004

to this:
12/16/2004?

Cross-browser would be nice, but IE-only will do in this case, for now.
  Reply With Quote


  sponsored links


2 4th August 19:18
tim slattery
External User
 
Posts: 1
Default formatting a date



str = "12162004";

formattedStr = str.substr(0, 2) + "/" + str.substr(2, 2) + "/" +
str.substr(4);

--
Tim Slattery
MS MVP(DTS)
Slattery_T@bls.gov
  Reply With Quote
3 4th August 19:18
middletree
External User
 
Posts: 1
Default formatting a date


Thanks. Follow-up questions:

I put this into an OnBlur statement if I want it to format it right after
they leave the text field?

Also, what if there is a one-digit date, like Dec 6 gets typed in as
1262004?
  Reply With Quote
4 4th August 19:18
mckirahan
External User
 
Posts: 1
Default formatting a date


[snip]

You might not want to do it onBlur() (unless you test for only digits) as it
will add extra slashes if they put focus on that field again.
  Reply With Quote
5 4th August 19:19
middletree
External User
 
Posts: 1
Default formatting a date


So when do you do it? Or can you just check for slashes first?
  Reply With Quote
6 4th August 19:19
mckirahan
External User
 
Posts: 1
Default formatting a date


[snip]

As I suggested: "test for only digits" before you insert the slashes.
  Reply With Quote
7 4th August 19:19
middletree
External User
 
Posts: 1
Default formatting a date


I guess I'm asking for help in the syntax for that. Not sure how to go about
it.
  Reply With Quote
8 4th August 19:19
middletree
External User
 
Posts: 1
Default formatting a date


Never mind. I am going to check for length, and if it's 6, then I'll assume
it's without slashes.
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666