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!!!
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.
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!
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: -
var $textbox = document.getElementById('theTextbox');
-
-
var $dateTrans = new Date($textbox.value); // If this doesn't work try:
-
// var $dateTrans = new Date(Date.parse($textbox.value));
-
-
$textbox.value = $dateTrans.getMonth() + '/' + $dateTrans.getDay() + '/' + $dateTrans.getYear();
-
rnd me 427
Recognized Expert Contributor
to coerce input:
this function worked on 2/3 examples you posted. -
function parseDate(s) {
-
var d = new Date();
-
var td=s.replace(/[-_\\\.]/gm, "/")
-
d.setTime(Date.parse(td))
-
return d;
-
}
-
-
-
parseDate("9/7/2007")
-
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: -
Date.prototype.format=function(form){ //edited for posting
-
function f(n) { return n < 10 ? "0" + n : n; }
-
var d = form;
-
d = d.replace("MM", f(this.getMonth() + 1));
-
d = d.replace("M", this.getMonth() + 1);
-
d = d.replace("DD", f(this.getDate()));
-
d = d.replace("D", this.getDate());
-
d = d.replace("YYYY", this.getFullYear());
-
d = d.replace("YY", f(this.getYear() - 100));
-
return d;
-
}
-
"I want to force the format to be M/D/YY. " -
alert(parseDate("9/7/2007").format("M\/D\/YY"))
Sign in to post your reply or Sign up for a free account.
Similar topics |
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);
|
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...
|
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...
|
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. ...
|
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
| |
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();
|
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();
|
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...
|
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"...
|
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,...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |