1

How do I handle dates from a spreadsheet like this. enter image description here

I need to get the last date from column 2 and then find the next date. I have created this code for retrieving the last date.

lastDateRow = sheet.getLastRow()-1;
lastDate = Date(sheet.getRange("B"+(lastDateRow)+":B"+(lastDateRow).getValue());

2 Answers 2

1

The value you get from the sheet is already a date object (complete with year, date, hours etc), no further action should be necessary.

When you say 'next date' I guess you meant 'next day' ? If so, simply add 24*60*60*1000 to the date in milliseconds and you'll get the next day ;-)

In code it becomes something like this :

function xxx(){
var sheet = SpreadsheetApp.getActiveSheet()
var lastDateRow = sheet.getLastRow();
var lastDate = sheet.getRange(lastDateRow,2).getValue();
Logger.log(lastDate)
var nextDay = new Date(lastDate.getTime()+24*60*60*1000);
Logger.log(nextDay)
}

Logger result on a test :

Tue Aug 21 15:00:00 PDT 2012
Wed Aug 22 15:00:00 PDT 2012
Sign up to request clarification or add additional context in comments.

Comments

1

Actualy this is what I ended up with after looking up js nextdate fnction here on stackowerflow.

var lastDateRow = sheet.getLastRow();
var lastDate = sheet.getRange(lastDateRow,2).getValue();
lastDate = getTomorrow(lastDate,1);

function getTomorrow(d,offset) {
    if (!offset) { offset = 1 }
    return new Date(new Date(d.getTime()).setDate(d.getDate() + offset));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.