...

View Full Version : getMonth what am I doing wrong



wibblemonster
08-24-2011, 03:12 PM
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


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');

devnull69
08-24-2011, 03:15 PM
There is a difference between


var timeStamp = today.getDate() + '-' + today.getMonth()+1 + '-' + today.getFullYear() + ' ' + today.getHours() + '.' + today.getMinutes() + '.' + today.getSeconds();

and


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum