When I click the button "Change Dates" it updates the dates in the date column. It increases the dates by 1 month and inputs a Timestamp after the date. As you can see for the Date for Account 2 it increases the Date perfecting 1 month, but for Account 1 it increases it only 30 days from October 1st to October 31st.
I only want the month number to change and I don't want the timestamp inputted after the date.
function dateChanger() {
var sheetName = "Sheet 1"; // Please set your sheet name.
var a1Notations = ['G5:G27', 'T5:T27', 'AG5:AG27', 'AT5:AT27']; // This is from your question.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
var ranges = a1Notations.map(e => `'${sheetName}'!${e}`);
var { valueRanges } = Sheets.Spreadsheets.Values.batchGet(ssId, { ranges, valueRenderOption: "UNFORMATTED_VALUE" });
var data = valueRanges.map(({ values }, i) => {
var values = values.map(([v]) => {
var unixTime = (v - 25569) * 86400 * 1000; // Ref: https://stackoverflow.com/a/6154953
var temp = new Date(unixTime);
temp.setMonth(temp.getMonth() + 1);
var serialNumber = (temp.getTime() / 1000 / 86400) + 25569; // Ref: https://stackoverflow.com/a/6154953
return [serialNumber];
});
return { range: ranges[i], values };
});
Sheets.Spreadsheets.Values.batchUpdate({ data, "valueInputOption": "USER_ENTERED" }, ssId);
}
I've tried to get rid of the unixTime and Serial Number variable but it doesn't work.
From your provided Spreadsheet, the timezone offset might be related to your current issue. So, as one modification, how about the following modification?
var data = valueRanges.map(({ values }, i) => {
var values = values.map(([v]) => {
var unixTime = (v - 25569) * 86400 * 1000; // Ref: https://stackoverflow.com/a/6154953
var temp = new Date(unixTime);
temp.setMonth(temp.getMonth() + 1);
var serialNumber = (temp.getTime() / 1000 / 86400) + 25569; // Ref: https://stackoverflow.com/a/6154953
return [serialNumber];
});
return { range: ranges[i], values };
});
var data = valueRanges.map(({ values }, i) => {
var values = values.map(([v]) => {
var unixTime = (v - 25569) * 86400 * 1000; // Ref: https://stackoverflow.com/a/6154953
var temp = new Date(unixTime);
temp = new Date(unixTime + (temp.getTimezoneOffset() * 60 * 1000)); // Added
temp.setMonth(temp.getMonth() + 1);
var serialNumber = ((temp.getTime() - (temp.getTimezoneOffset() * 60 * 1000)) / 1000 / 86400) + 25569; // Modified Ref: https://stackoverflow.com/a/6154953
return [serialNumber];
});
return { range: ranges[i], values };
});
10/1/23
is changed to 11/1/23
.