[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

javascript date change month two digits 1 = 01

Posted on 2007-10-12
5
Medium Priority
?
8,046 Views
Last Modified: 2009-04-02
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
Comment
Question by:bschwarting
  • 2
  • 2
5 Comments
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 1200 total points
ID: 20065359
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
 
LVL 1

Author Comment

by:bschwarting
ID: 20065439
that didn't work, now my date2 variable is blank
0
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 1200 total points
ID: 20065624
is date2 a global variabel in your JS? In this case remove the var before date2
0
 
LVL 30

Accepted Solution

by:
third earned 800 total points
ID: 20066010
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
 
LVL 1

Author Comment

by:bschwarting
ID: 20068447
went another route, got it to work in AS/access with the DATEDIFF function.  thanks all!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question