473,763 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Javascript Date Format (other than current date)

xstatic
2 New Member
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 4665
pbmods
5,821 Recognized Expert Expert
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
2 New Member
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 Recognized Expert Expert
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 Recognized Expert Contributor
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
5107
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 = document.createElement('a'); link.href ="javascript:show_calendar('document.form1.date',document.form1.date.value);"; img = document.createElement('img'); img.setAttribute("src","H:Diverse\\cal.gif"); link.appendChild(img);
9
2112
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 user to the JavaScript language, and focuses on creating user interactivity on personal websites. This Object Oriented Language is known for creating a high level of interactivity with minimal coding, and is fairly simple to learn. We will begin...
2
2937
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 where there is no scripts allowed running (the software is from Opentext called Livelink)- therefore I need javascript to do the tasks listed below: 1. validate the form - this has been completed 2. pop up another window that will go ahead and...
7
31839
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 current date. If not, I'd like to display the error message to ValidationSummary. It seems to make sense to me to use CompareValidator but the problem is put the current date into CompareValidator. So, I created a hidden text field in my aspx. ...
15
18894
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 the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value with it. the output looks like that "01/01/1900 08:59:00" in that case time is
9
2419
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
2582
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
3156
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 then the calender gone actually i want if i click outside off the calender then it should me removed ..How kan i do this ... Pls inform me as early as possible .. I am waiting for ur quick replay ...Here i attached the source code .... <!DOCTYPE...
1
3462
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 have to made in my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type"...
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9943
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,...
0
8825
agi2029
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...
1
7370
isladogs
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...
0
6643
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();...
0
5271
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...
1
3918
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
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.