Solved

currentDate.getMonth(); returns wrong month

Posted on 2006-10-19
3
2,364 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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 how to implement server side field validation and display customized error messages to the client.
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…

691 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