Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting dates

Status
Not open for further replies.

tm22

Programmer
Mar 7, 2007
4
US
Hi, I would like to take a date such as March 7, 2007 and format it as 03/07/2007 using JavaScript. Any suggestions on how to do this? Thanks!
 
I'll post you a prototype that will do that for you, it's sexy.

*Note: you have to parse the value as a Date before you can use this prototype method.

To call this all you need to do is:
//get a date object
var now = new Date();
//call it
now.toJSDate();

Code:
Date.prototype.toJSDate = function() {return ((this.getMonth() + 1 < 10) ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1)) + "/" + ((this.getDate() < 10) ? "0" + this.getDate() : this.getDate()) + "/" + this.getFullYear() ;};

[monkey][snake] <.
 
Thanks for script!!! I will try it out.
 
Use 3 dropdown lists for dates: one for month, one for day, one for year. Then put the date together in whatever format you wish from that when the form is submitted. The more you allow users to input text for something like this, the more error checking you'll have to do. It's not difficult to adjust the day of the month dropdown list for the different maximum days of the month, either.

Lee
 
Unfortunately having dropdown lists is not an option. The date has to be user entered, and it has to appear as January 1, 2000. From there I need to format it as 01/01/00. Not my choice, just the customers requirements. Thanks!
 
tm22, did you try monksnake's solution - it works fine for me:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

Date.prototype.toJSDate = function() {return ((this.getMonth() + 1 < 10) ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1)) + "/" + ((this.getDate() < 10) ? "0" + this.getDate() : this.getDate()) + "/" + String(this.getFullYear()).substr(2, 2) ;};

function convertDate(obj) {
   var dte = new Date(obj.value);
   obj.value = dte.toJSDate();
}

</script>
<style type="text/css"></style>
</head>
<body>

<input type="text" id="txt" />
<input type="button" value="convert date" onclick="convertDate(document.getElementById('txt'))" />

</body>
</html>

That's a pretty sweet javascript date formatter prototype method monksnake, where'd you get that code? The guy that wrote that must be a genius.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
I think I wrote it, it lists month, day, then year.

I saw a simular code once that was year, month, then day but it was novice work.

[beaver]

[monkey][snake] <.
 
I did get that script to work...it's awesome!!! Thanks [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top