Matthew Valenta
Matthew Valenta

Reputation: 11

incorrect date format sent in email

I'm using a very basic automated email script. The message that I want to send includes a date summit through a form and is sent on a trigger. In the spreadsheet this is formatted as "06/11/2013" but when the email is received it appears in the body of the email as "Wed Nov 06 2013 00:00:00 GMT-0000 (GMT)".

I want it to appear formatted in the body of the email as just the date as it is down in the spreadsheet. Is there any way to do this? Here is the (very basic) script I am using:

var AppointmentDateRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("master").getRange("H" + lastRow);
var AppointmentDate = AppointmentDateRange.getValues();

Upvotes: 0

Views: 287

Answers (1)

0Valt
0Valt

Reputation: 10345

Problem

Retaining date format extracted from a spreadsheet.

Solution 1

Use Utilities class formatDate() method to format extracted date to a desired output. Please, take note that the first argument is of type Date, so if you store dates as ISO Strings, you'll need to parse them with new Date('your datestring here').

Since you most likely use the default auto format, getValues() already contains your date parsed into a Date instance, so var formatted = Utilities.formatDate(date, 'GMT', 'MM/dd/YYYY') will suffice (this code span assumes that input date is written into date variable and your desired timezone is GMT).

Solution 2

Or you can just use the getDisplayValues() method of the Range class designed to account for formatting and data types.

Reference

  1. formatDate() method reference;
  2. getDisplayValues() method reference;
  3. Simple date format reference;

Upvotes: 2

Related Questions