• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8189
  • Last Modified:

javascript date change month two digits 1 = 01

I'm currently using this script to pass a date in an ASP page.  When it passes the month, it goes as one digit.  How can i change this to pass two digits for month?

Currently passes month as 1,2,3,4,5,6,7,8,9
I need to it to pass month as 01,02,03,04,05,06,07,08,09

<script language="JavaScript">
function StartUp() {
        date2 = new Date;
       document.forms[0].date2.value = (date2.getMonth()+1) + "-" + date2.getDate() + "-" + date2.getFullYear() + "     " + date2.getHours() + ":" + date2.getMinutes() + ":" + date2.getSeconds();
}
</script>

Ideas?
0
bschwarting
Asked:
bschwarting
  • 2
  • 2
3 Solutions
 
hernst42Commented:
I'll do it this way, but there might be better ones:

function StartUp() {
  var date2 = new Date;
  var month =date2.getMonth()+1;
  if (monat.toString().length==1) { month = '0' + month; }
  document.forms[0].date2.value = month + "-" + date2.getDate() + "-" + date2.getFullYear() + "     " + date2.getHours() + ":" + date2.getMinutes() + ":" + date2.getSeconds();
}
0
 
bschwartingAuthor Commented:
that didn't work, now my date2 variable is blank
0
 
hernst42Commented:
is date2 a global variabel in your JS? In this case remove the var before date2
0
 
thirdCommented:
try this,

<!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>
<title>Third Santor</title>
<script language="JavaScript">
function StartUp() {
       var date2 = new Date(2007, 8, 8);
         //var date2 = new Date();  //uncomment this later to activate date as current date
       document.forms[0].date2.value = formatNum((date2.getMonth()+1)) + "-" + formatNum(date2.getDate()) + "-" + date2.getFullYear() + "     " + formatNum(date2.getHours()) + ":" + formatNum(date2.getMinutes()) + ":" + formatNum(date2.getSeconds());
}

function formatNum(num){
  var mynum = num * 1;
  var retVal = mynum<10?'0':'';
  return (retVal + mynum)
}

window.onload = function(){
  StartUp();
}
</script>
</head>
<body>
<form name="form1" method="post" action="" onsubmit="return ">
      <input type="text" name="date2" value="" />
</form>
</body>
</html>
0
 
bschwartingAuthor Commented:
went another route, got it to work in AS/access with the DATEDIFF function.  thanks all!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now