Solved

currentDate.getMonth(); returns wrong month

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now