Web Analytics Made Easy -
StatCounter getMonth what am I doing wrong - CodingForum

Announcement

Collapse
No announcement yet.

getMonth what am I doing wrong

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • getMonth what am I doing wrong

    I'm new to java and have been given the following piece of code to try and fix.

    The code is renaming an existing HTML file with the current date but the problem is the date is out by one. I'm aware that in java Jan=0 Feb=1 etc but just dont know enough to know how to fix this...

    If I add +1 to getmonth all that happens is the file gets named LEICESTER_DAILY - 24-71-2011 17.20.36 rather than LEICESTER_DAILY - 24-8-2011 17.20.36

    Here's the piece of code

    Code:
    function makePerm() {
    var txtFile;
    	// Swap the temporary file with the original
    	if (fileSys.FileExists(grabParentFolderUrl() + 'tempsave.htm')) {
    		txtFile = fileSys.getFile(grabParentFolderUrl() + 'tempsave.htm');
    		if (!(txtFile.size == 0)) { // Check file is not empty
    			if (fileSys.FileExists(grabParentFolderUrl() + 'LEICESTER_DAILY.htm')) { 
    				var today = new Date();
    				var timeStamp = today.getDate() + '-' + today.getMonth() + '-' + today.getFullYear() + ' ' + today.getHours() + '.' + today.getMinutes() + '.' + today.getSeconds();
    				txtFile = fileSys.getFile(grabParentFolderUrl() + 'LEICESTER_DAILY.htm');
    				txtFile.Move(grabParentFolderUrl() + 'LEICESTER_DAILY - ' + timeStamp.toString() + '.htm');
    				txtFile = fileSys.getFile(grabParentFolderUrl() + 'tempsave.htm');
    				txtFile.Move(grabParentFolderUrl() + 'LEICESTER_DAILY.htm');

  • #2
    There is a difference between
    Code:
    var timeStamp = today.getDate() + '-' + today.getMonth()+1 + '-' + today.getFullYear() + ' ' + today.getHours() + '.' + today.getMinutes() + '.' + today.getSeconds();
    and
    Code:
    var timeStamp = today.getDate() + '-' + (today.getMonth()+1) + '-' + today.getFullYear() + ' ' + today.getHours() + '.' + today.getMinutes() + '.' + today.getSeconds();
    The first one with evaluate getMonth() first, then convert it to a string, then convert 1 to a string and append it to the result of getMonth()

    The second one will evaluate getMonth() first and add 1 to it before(!) converting the result to a string. So this is what you'll need.

    Comment

    Working...
    X