Google Scripts Timestamp - Format Output on Email - email

I'm not sure what I'm doing wrong here, and this is probably really simple... After scouring the net and trying hundreds of different examples found, I've come up empty handed on getting the format needed. My script works, sends the email, all values are there, but the date formats are not what we are after. So, here is what's up:
I've got a basic script that sends an HTML email from a template when a respondent submits a form. The timestamp, start (date and time), end (time only) value is needed, and is printed in the HTML email, but it's showing a full-blown timestamp output such as: "Tue Nov 03 2020 11:39:28 GMT-0700 (Mountain Standard Time)"
What I am trying to do is format the timestamp value shown in the email to this: "Tue Nov 03 2020 HH:mm"
Here is the script I am using:
function onFormSubmit(e) {
var htmlBody = HtmlService.createTemplateFromFile('email');
var rng = SpreadsheetApp.getActiveSheet().getActiveRange();
var timestamp = Utilities.formatDate(new Date(), "MST" , "MM-dd-yyyy | HH:mm:ss");
var email = rng.getValues()[0];
var body = HtmlService.createTemplateFromFile("email");
var to = 'foo#bar';
var subject = 'Activity Report ' + email[0] + '';
htmlBody.timestamp = email[0];
htmlBody.start = email[1];
htmlBody.end = email[2];
htmlBody.activityobserved = email[3];
htmlBody.summary = email[4];
htmlBody.actiontaken = email[5];
htmlBody.attachments = email[6];
var email_html = htmlBody.evaluate().getContent();
MailApp.sendEmail({
to: to,
subject: subject,
htmlBody: email_html,
replyTo:'bar#foo',
});
}

Don't forget to actually use timestamp. To get the "Tue" part in your date, you can use "EEE". I set the date formatting below as you specified in your question.
function onFormSubmit(e) {
// ...
var timestamp = Utilities.formatDate(new Date(), "MST" , "EEE MMM dd yyyy HH:mm");
// ...
htmlBody.timestamp = timestamp;
htmlBody.start = Utilities.formatDate(email[1], "MST" , "EEE MMM dd yyyy HH:mm");
htmlBody.end = Utilities.formatDate(email[2], "MST" , "EEE MMM dd yyyy HH:mm");
// ...
}

Solution:
You don't include variable timestamp in the htmlBody object. Instead you are using the original source value of it.
Replace:
htmlBody.timestamp = email[0];
with:
htmlBody.timestamp = timestamp;
Update based on your comment:
Im a little confused on how to format the start and end times though.
They are still displaying the full output.
Assuming that you have date objects in your sheet,
Replace:
htmlBody.start = email[1];
htmlBody.end = email[2];
with
htmlBody.start = Utilities.formatDate(new Date(email[1]), "MST" , "EEE MMM dd yyyy HH:mm");
htmlBody.end = Utilities.formatDate(new Date(email[2]), "MST" , "EEE MMM dd yyyy HH:mm");

Related

Flutter custom date format

I am facing issue while formatting the date to custom format.
I need to convert date yyyy-MM-dd HH:mm:ss ===> EEEE, MMM dd, yyyy
For example I am getting date from server 27-10-2022 11:02:50, and I need to convert it to Thursday, October 27, 2022
Getting Date format is "dd-MM-yyyy HH:mm:ss" and the desire format will be "EEEE, MMMM dd, yyyy"
final data = "27-10-2022 11:02:50";
final format = DateFormat("dd-MM-yyyy HH:mm:ss");
final DateTime result = format.parse(data);
print(result); //2022-10-27 11:02:50.000
final newFormatter = DateFormat("EEEE, MMMM dd, yyyy");
final newFormatString = newFormatter.format(result);
print(newFormatString); // Thursday, October 27, 2022
I am using intl package
Just checked in flutter docs. Your date format is not good. For converting string to date.
The following date format is required,
"2012-02-27 13:27:00"
"2012-02-27 13:27:00.123456789z"
"2012-02-27 13:27:00,123456789z"
"20120227 13:27:00"
"20120227T132700"
"20120227"
"+20120227"
"2012-02-27T14Z"
"2012-02-27T14+00:00"
"-123450101 00:00:00 Z": in the year -12345.
"2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"
And you can convert that into the format like below
var date1 = DateFormat('dd-MM-yyyy hh:mm:ss').parse("27-10-2022 11:02:50");
var date2 = DateFormat('yyyy-MM-dd hh:mm:ss').format(date1);
print( DateFormat('EEEE, MMMM dd, yyyy').format(date2));
please try this, hope you will get the idea,
print(DateFormat('dd-MM-yyyy HH:mm:ss').parse('27-10-2022 11:02:50'));

Revert datetime format, from short to long

Tried searching around couldn't really find anything. Was hoping to find a way to revert the datetime format.
So I start off with: 4-11-22 and I want to change to Friday, 12 November 2022.
Using the intl package
import intl package
import 'package:intl/intl.dart';
Then, you can do as follows:
String myDate = '4-11-22'; // input date
String pattern = 'dd-MM-yy'; // define parse pattern for the input date
DateTime date = DateFormat(pattern).parse(myDate); // parse the input date
String newPattern = 'EEEE, dd MMMM yyyy'; // define new pattern
String formattedDate = DateFormat(newPattern).format(date); // reformat
print(formattedDate); // result: Friday, 04 November 2022
Try on DartPad
For more formatting possibilities, go to the docs.
final oldDateDateTime = DateFormat('dd-MM-yy').parse('4-11-22');
final newDateString = DateFormat('EEEE, d MMMM y', 'en_US').format(oldDateDateTime);
print(oldDateDateTime.toString());
print(newDateString);
Output:
2022-11-04 00:00:00.000
Friday, 4 November 2022
More in: https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html
You can do as follows
final DateFormat formatter = DateFormat('EEEE, dd MMMM yyyy');
final String formatted = formatter.format(DateTime.now());
Then you use the formatted

New month date continues to count up

The new month of Feburary date continues to count up from January. So instead of showing Feburary 1St, it's showing Feburary 32 Like the picture below, any help would be appreciated thanks.
This is how I am getting the current date:
let date = Date()
let format = DateFormatter()
format.dateFormat = "EE, MMM DD, YYYY"
let currentDate = format.string(from: date)
header.headerTitle.text = currentDate
This is the result Feburary 32, 2018
Change "EE, MMM DD, YYYY" to "EE, MMM dd, yyyy" (or maybe just one d) and next time please try to read up on how date formatters work before trying to use them:
http://userguide.icu-project.org/formatparse/datetime

I want to remove Time Stamp and dateline from date entries

I copy and pasted the mail merge script from a third party source. It works terrifically except for one thing.
When I type a date into my form (ex. July 7, 2012) the script in the mail merge converts the date into a dateline (ex. July 7, 2012 00:00:00 GMT).
I want to remove the unnecessary date after the date. Can you help me?
here is the portion of the code that is the most relevant to this problem:
// Setup the current timestamp and timezone.
timeZone = myVariablesSheet.getRange("B13").getValue();
dateline = myVariablesSheet.getRange("B7").getValue();
if(typeof timeZone == 'undefined' || timeZone == '') {
timeZone = 'GMT';
}
if(typeof dateline == 'undefined' || dateline == '') {
dateline = Utilities.formatDate(new Date(), timeZone, "EEE, MMM d, ''yy");
} else {
dateline = Utilities.formatDate(dateline, timeZone, "EEE, MMM d, ''yy");
}
if(debug) Browser.msgBox("dateline = " + dateline + "\ntimeZone = " + timeZone);
I think you are asking how to format a date.
If you want the date formatted as July 9, 2012 you'd need enter this 'MMMM d, yyyy'.
Like this:
Logger.log(Utilities.formatDate(new Date(), 'PST', 'MMMM d, yyyy'))

Current date from BlackBerry

I am developing an application which needs the current date from the device, but it only needs day-of-week, month, and day-of-month, like Friday October 14
I have tried this code with Calendar. How do I convert Date to String? Is this possible to get date in this format?
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
System.out.println("Date" + c.getTime());
and my output : `Fri Oct 14 16:17:03 Asia/Calcutta 2011`
You could use SimpleDateFormat:
SimpleDateFormat fmt = new SimpleDateFormat("EEEE MMMM dd");
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
System.out.print("Date ");
System.out.println(fmt.format(c.getTime()));
If you need those values in different Strings it gets a little bit more complicated (using DateFormatSymbols to get the month/weekday names):
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
DateFormatSymbols dateSymbols = new DateFormatSymbols();
String[] monthsText = dateSymbols.getMonths();
String[] weekdaysText = dateSymbols.getWeekdays();
String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
String month = monthsText[c.get(Calendar.MONTH)];
String weekday = weekdaysText[c.get(Calendar.DAY_OF_WEEK)];
System.out.format("day: %s, month: %s, weekday: %s", day, month, weekday);