Solved

currentDate.getMonth(); returns wrong month

Posted on 2006-10-19
3
2,344 Views
Last Modified: 2012-06-27
When I use the below code, I get "9/20/2006 1:07 AM".  I checked my system time and all is well.  It's October, so why is it returning 9?

function insertAtCursor(myField) {
  var myValue;
  var currentDate = new Date();
  var day = currentDate.getDate();
  var month = currentDate.getMonth();
  var year = currentDate.getFullYear();
  myValue = month + "/" + day + "/" + year + " ";
 
  var currentTime = new Date();
  var hours = currentTime.getHours();
  var minutes = currentTime.getMinutes();

  var suffix = "AM";
  if (hours >= 12) {
  suffix = "PM";
  hours = hours - 12;
  }
  if (hours == 0) {
  hours = 12;
  }

  if (minutes < 10)
  minutes = "0" + minutes;

  myValue += hours + ":" + minutes + " " + suffix;
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
0
Comment
Question by:traigo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
harshgrover earned 500 total points
ID: 17771829
The javascript components all start from 0. so january would return '0' and not 1. Just a traditional way how array's behave. the index starts from 0, instead of 1

this should help

http://www.w3schools.com/jsref/jsref_obj_date.asp

http://www.tizag.com/javascriptT/javascriptdate.php

Gary
0
 

Author Comment

by:traigo
ID: 17771872
Thanks harshgrover.  I use arrays a lot and I understand starting from 0, but that's just stupid.  Who would make a date function to get the current month and start it from 0?  The least they could do is be consistent and have getDate() act the same way.
0
 
LVL 2

Expert Comment

by:harshgrover
ID: 17771923
naw, i might be wrong, but i thought all the calendars in java behave the same, whether it is GregorianCalendar, java.util.Date etc. if you want to get the number of the month, they would all return 0 for January :)...i guess we programmers dont really care a lot about how things function in the real world ;)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

732 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