?
Solved

currentDate.getMonth(); returns wrong month

Posted on 2006-10-19
3
Medium Priority
?
2,404 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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

752 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