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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

Javascript Counter/number generator question

Any reason why this script would be off by a month? It should be displaying 2 digit year "11" 2 digit month "10" then the current time. Looks like it's behind a month though.. Weird..  Example: N110904135925

var date_now = new Date ();
var dmon = ""
var dday = ""
var dyear = ""

<!-- hide from old browsers
  var curDateTime = new Date()
  var curHour = curDateTime.getHours()
  var curMin = curDateTime.getMinutes()
  var curSec = curDateTime.getSeconds()
  var curTime = 
    ((curHour < 10) ? "0" : "") + curHour + ((curMin < 10) ? "0" : "") + curMin + ((curSec < 10) ? "0" : "") + curSec 
//  alert(curTime)
//-->

displayMonth = date_now.getMonth()+1 + "-" + date_now.getDate() + "-" + date_now.getYear()+1

dmon = date_now.getMonth();
dday = date_now.getDate();
dyear = date_now.getFullYear() % 100; 


if (dday<=9) {
	dday="0"+dday	
}

if (dmon<=9) {
	dmon="0"+dmon	
}

displayDate = dyear + dmon + dday + curTime
displayMonth = date_now.getMonth()+1 + "-" + date_now.getDate() + "-" + ( date_now.getFullYear() % 100 ) 



function getCookie(NameOfCookie){
    if (document.cookie.length > 0) {              
    begin = document.cookie.indexOf(NameOfCookie+"=");       
    if (begin != -1) {           
      begin += NameOfCookie.length+1;       
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin, end));
    } 
  }
  return null;
}

function setCookie(NameOfCookie, value, expiredays) {
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

  document.cookie = NameOfCookie + "=" + escape(value) + 
  ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function delCookie (NameOfCookie) {
  if (getCookie(NameOfCookie)) {
    document.cookie = NameOfCookie + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
//alert(displayDate);
expireDate = new Date
expireDate.setMonth(expireDate.getMonth())

jcount = "N" + displayDate + " "//YOU CAN ADD OR REMOVE THE SPECIFIC AREAS AS NEEDED

//jcount = eval(cookieVal("theCounter"))
//jcount++
document.cookie = "theCounter="+jcount+";expires=" + expireDate.toGMTString()

function cookieVal(cookieName) {
thisCookie = document.cookie.split("; ")
for (i=0; i<thisCookie.length; i++){
	if (cookieName == thisCookie[i].split("=")[0]){
		return thisCookie[i].split("=")[1]
	}
}
return 0
}

function page_counter(){
for (i=0;i<(14-jcount.toString().length);i++)
document.write('<span class="counter">0</span>')
for (y=0;y<(jcount.toString().length);y++)
document.write('<span class="counter">'+jcount.toString().charAt(y)+'</span>')
}

Open in new window

0
js1138
Asked:
js1138
  • 4
  • 3
1 Solution
 
Kim WalkerWeb Programmer/TechnicianCommented:
January is month 0 (zero). So October is month 9 (nine).
0
 
js1138Author Commented:
I kind of figured that. I guess where my question lies is what do I need to change to to align the months properly? Any suggestions?
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
What are you aligning them to? Please be specific.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
js1138Author Commented:
Basically, the number generated from the script breaks down like this:

Example: N110904135925

11=year
09=month
04=day
135925= military time hours, mins, seconds

This number was generated today and the month should reflect 10 (October) not 09  (September)
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
There are actually several dates generated by this script. In the displayMonth variable, you've wisely added 1 to the value returned by getMonth(). Which part of the script is returning the value you're talking about? I can't find any part of this script that would return a string that begins with the letter "N."
0
 
js1138Author Commented:
Check out line 67...That generates the N plus displays the date information

At line 19 if you do a +1 there in manipulates the date as well   example: dmon = date_now.getMonth()+1;  

Just can't seem to get it to generate the current month..


0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Line 19 is dday. Have you tried your example above on line 18 (dmon = date_now.getMonth()+1;)? That should take care of the problem. If not, can you post a link to the page?
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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now