Reputation: 258
I am beginner in script programing and I would like to format the date from today as following ("DD-MM-YY). I have written the following script
function onFormSubmit(e) {
//Déclaration des variables
var SheetResponse = SpreadsheetApp.getActiveSheet();
var DerniereLigne = SpreadsheetApp.getActiveSheet().getLastRow();
var DateToday = new Date();
var YearToday = DateToday.getYear();
var DayToday = DateToday.getDate();
var MonthToday = DateToday.getMonth() + 1;
var ID = String(DayToday) + String(MonthToday) + String(YearToday);
//Création de l'ID dans la derniére ligne et colonne "N"
SheetResponse.getRange(DerniereLigne,14).setValue(ID);
}
and the results is obviously for today (August 21, 2015) is "21082015", but I want only "210815".
Could somebody help me. Thank you in advance.
Upvotes: 0
Views: 4485
Reputation: 76
Alternative 1: string manipulation
Try to replace with this:
var YearToday = DateToday.getFullYear().toString().substr(-2);
getFullYear()
is guaranteed to return a four digit year, so you can then use string manipulation to get the two last characters from that.
Alternative 2: Utilities.formatDate
Look into the function Utilities.formatDate. It can save you a lot of code.
You can delete most of your line and just write:
var ID = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'ddMMYY');
or
var ID = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'dd-MM-YY');
if you wanted those hyphens.
Upvotes: 3