Home > Mobile >  How can I change the formatting of my date within my script, would like it to show dd/mm/yyyy only
How can I change the formatting of my date within my script, would like it to show dd/mm/yyyy only

Time:12-09

function reminder() {
  var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
  var data = sh.getDataRange().getValues()
  var d = new Date().getTime();
  for (var i=1;i<data.length;i  ){
    if (data[i][4]<=new Date(d 7*24*60*60*1000) && data[i][4]>=new Date(d 5*24*60*60*1000) && data[i][6]==''){
      MailApp.sendEmail({to:data[i][3],
        subject: 'Reminder Process Update Required in 1 week',
        htmlBody: 'Hello ' data[i][1] ' The page for ' data[i][0] ' is due to review on ' data[i][4] ' Please review the content and contact the team before its due date if amendments are required.'
      })
      sh.getRange(i 1,7).setValue('sent')
    }
    else if (data[i][4]<=new Date(d 30*24*60*60*1000)  && data[i][4]>=new Date(d 28*24*60*60*1000)  && data[i][5]==''){
      MailApp.sendEmail({to:data[i][3],
        subject: 'Reminder Process Update Required in 1 month',
        htmlBody: 'Hello ' data[i][1] ' The page for ' data[i][0] ' is due to review on ' data[i][4] ' Please review the content and contact the team before its due date if amendments are required.'
      })
      sh.getRange(i 1,6).setValue('sent')
    }
  }
}

How can I change the date formatting in my script, It is currently generating an automated email however the date is being formatted and showing the Time as well as (Eastern Standard Time) I would like it to only show the date without the time included.

CodePudding user response:

You can create a format function like

function formatDate(d) {
  return `${String(d.getDate()).padStart(2, '0')}/${String(d.getMonth()   1).padStart(2, '0')}/${d.getFullYear()}`;
}

Example:

function formatDate(d) {
  return `${String(d.getDate()).padStart(2, '0')}/${String(d.getMonth()   1).padStart(2, '0')}/${d.getFullYear()}`;
}

const d = new Date();
console.log(formatDate(d));

CodePudding user response:

call this function where you need formatted date.

function formatDate(date) {
    var d = new Date(date),
        month = ''   (d.getMonth()   1),
        day = ''   d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) 
        month = '0'   month;
    if (day.length < 2) 
        day = '0'   day;

    return [ day, month, year].join('/');
}
  • Related