By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

Need Javascript Date Format (other than current date)

xstatic
P: 2
After searching through the hundreds of links about "Javascript Date Formatting", all I am finding is how to format dates that gives a "current date" result.

Here is what I need...
I have a couple date fields that I have users input various dates like "Date of Birth" and "Access From Date" and "Access To Date". There are multiple users and they invariably input the dates in different formats (i.e., 9/07/06, 09-07-07, 9/7/2007, etc.) The fields are a single field and I want to force the format to be M/D/YY. I want the months and days to have no leading zeros and slashes instead of dashes.

Can someone please show me a javascript (or even php) that will either change the users input to the correct format automatically or somehow force them to input the correct format?

Thanks in advance!!!
Sep 8 '07 #1
Share this Question
Share on Google+
4 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Xstatic. Welcome to TSDN!

When creating a new JavaScript Date object, you can specify a date/time as the parameter to the constructor, or you can use Date::parse().

The formats that it accepts are similar to PHP's strtotime() function.
Sep 8 '07 #2

xstatic
P: 2
Heya, Xstatic. Welcome to TSDN!

When creating a new JavaScript Date object, you can specify a date/time as the parameter to the constructor, or you can use Date::parse().

The formats that it accepts are similar to PHP's strtotime() function.
Thanks for the reply pbmods,
In reading the description of the Date:parse object, I don't know if this is will work. Maybe I am just not understanding its use.

The fields that my users are inputting are various dates, and can never be pre-determined in a hard-code situation. I need a script to take the user's input and reformat it to the proper format of M/D/YY. So if they enter 09-07-07, the script will change it to 9/7/07.

Hope this clarifies and if anyone can give me an example script, that would be great!
Sep 8 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, Xstatic.

Use Date's constructor to create a new Date object, then use its accessors to structure the date how you want it:

Expand|Select|Wrap|Line Numbers
  1. var $textbox = document.getElementById('theTextbox');
  2.  
  3. var $dateTrans = new Date($textbox.value);  // If this doesn't work try:
  4. // var $dateTrans = new Date(Date.parse($textbox.value));
  5.  
  6. $textbox.value = $dateTrans.getMonth() + '/' + $dateTrans.getDay() + '/' + $dateTrans.getYear();
  7.  
Sep 8 '07 #4

rnd me
Expert 100+
P: 427
to coerce input:



this function worked on 2/3 examples you posted.
Expand|Select|Wrap|Line Numbers
  1. function parseDate(s) {
  2.     var d = new Date();
  3. var td=s.replace(/[-_\\\.]/gm, "/")  
  4.   d.setTime(Date.parse(td))
  5.   return d; 
  6. }
  7.  
  8.  
  9. parseDate("9/7/2007")
  10.  
it returns a date object however, not a string.
i didn't have a proto that is formatted like you want, but you can use my custom formatter to do it:

Expand|Select|Wrap|Line Numbers
  1. Date.prototype.format=function(form){   //edited for posting
  2.   function f(n) {        return n < 10 ? "0" + n : n;    }
  3.   var d = form;
  4.     d = d.replace("MM", f(this.getMonth() + 1));
  5.     d = d.replace("M", this.getMonth() + 1);
  6.     d = d.replace("DD", f(this.getDate()));
  7.     d = d.replace("D", this.getDate());
  8.     d = d.replace("YYYY", this.getFullYear());
  9.     d = d.replace("YY", f(this.getYear() - 100));
  10.       return d;
  11. }
  12.  
"I want to force the format to be M/D/YY. "

Expand|Select|Wrap|Line Numbers
  1. alert(parseDate("9/7/2007").format("M\/D\/YY"))
Sep 9 '07 #5

Post your reply

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