473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need Javascript Date Format (other than current date)

xstatic
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
4 4618
pbmods
5,821 Expert 4TB
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
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
5,821 Expert 4TB
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
427 Expert 256MB
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

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

Similar topics

8
by: timmy_dale12 | last post by:
I need help with this one. I have a function that pastes a row. In each row i am pasting a link which is to call a date picker javascript function. This is the code that pastes the link : link =...
9
by: Charlene Russ | last post by:
Learn on-line at your own in a user-centered format with plenty of interaction and personal attention. This is a basic level coursed designed to introduce the novice to intermediate computer...
2
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
15
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
9
by: mistral | last post by:
Need help to remove list of days from date script. Need format "June 07, 2006" <SCRIPT LANGUAGE="JavaScript"> <!-- Begin // Get today's current date. var now = new Date();
5
by: settyv | last post by:
Hi, Below is the Javascript function that am trying to call from asp:Button control. <script language="javascript"> function ValidateDate(fromDate,toDate) { var fromDate=new Date();
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
1
by: swethak | last post by:
hi, i have a code to disply the calendar and add events to that. It works fine.But my requirement is to i have to disply a weekly and daily calendar.Any body plz suggest that what modifications i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.