...

View Full Version : Javascript - Display future expiration date in short format



mcarlson
05-16-2012, 12:25 AM
I'm trying to find a way to display a future date in short format (as an expiration date) with Javascript. I've figured out how to do it with long format, but have no idea how to convert it and I'm not great with Javascript.

Here is code that I found on another forum and modified:


<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>


<!doctype html>
<head>
<title>Expires</title>
</head>
<body>

<script>

var myDelayInDays=6, myDate=new Date();
myDate.setDate(myDate.getDate()+myDelayInDays);

document.write('Expires: ' + myDate.toLocaleDateString());

</script>
</body>
</html>

</body>
</html>


Which displays:

"Expires: Monday, May 21, 2012"

I would like it to display:

"Expires: 05/21/2012"

Can anyone help?!

Old Pedant
05-16-2012, 01:02 AM
var myDelayInDays=6, myDate=new Date();
myDate.setDate(myDate.getDate()+myDelayInDays);
var mn = myDate.getMonth()+1;
var dy = myDate.getDate();
var shortDate = ( mn < 10 ? "0" : "" ) + mn + "/" + ( dy < 10 ? "0" : "" ) + dy + "/" + myDate.getFullYear();

document.write('Expires: ' + shortDate);

mcarlson
05-16-2012, 03:00 AM
MY HERO! Thanks so much!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum