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'))
Related
I have converted a Date into DateTime format, and it is returning me the hour format in 00:00:00 but I want it to be in 23:59:59
Date startDate = Date.newInstance(2021,2,1);
This returns the output as 2021-02-01 00:00:00
When I try to convert this to the 23:59:59 hour format by using the below code
DateTime startDateConvertTwo = DateTime.newInstance(startDate, Time.newInstance(23, 59, 59, 0));
It is pushing the date to next day and returning the value of 2021-02-02 07:59:59
I tried to sort this out by changing the values of Time.newInstance by adding it as Time.newInstance(15, 59, 59, 0) by doing which I get the expected result. But is it the right way to achieve what I am trying to do?
Please let me know if there are any other ways.
The returned output of Date startDate = Date.newInstance(2021,2,1); is not 2021-02-01 00:00:00. It's just a date with no information about time, but System.debug() display it as a DateTime, that's why you see 00:00:00.
Try System.debug(String.valueOf(startDate)); to see only the Date part.
DateTime.newInstance(date, time)
Constructs a DateTime from the specified date and time in the local time zone.
As documentation states, the DateTime you get is in your own time zone. Anyway System.debug() shows it in UTC time zone (GMT+0), so if your time zone is GMT-8 you'll see 2021-02-02 07:59:59.
System.debug(String.valueOf(startDateConvertTwo )); will shows the DateTime in your own time zone, so you'll see 2021-02-01 23:59:59.
If you need a DateTime in GMT you could use DateTime.newInstanceGmt(date, time):
DateTime startDateGMT = DateTime.newInstanceGmt(startDate, Time.newInstance(23, 59, 59, 0));
If you cannot use that method, you could add your offset to a DateTime:
public static DateTime toUTC(DateTime value) {
Integer offset = UserInfo.getTimezone().getOffset(value);
return value.addSeconds(offset/1000);
}
You could test it in anonymous console:
Date startDate = Date.newInstance(2021,2,1);
DateTime startDateConvertTwo = DateTime.newInstance(startDate, Time.newInstance(23, 59, 59, 0));
DateTime startDateGMT = DateTime.newInstanceGmt(startDate, Time.newInstance(23, 59, 59, 0));
DateTime startDateGMT2 = toUTC(startDateConvertTwo);
System.debug('startDateConvertTwo: ' + startDateConvertTwo); // startDateConvertTwo: 2021-02-01 22:59:59 // Because I'm at GMT+1
System.debug('String.valueOf(startDateConvertTwo): ' + String.valueOf(startDateConvertTwo)); // String.valueOf(startDateConvertTwo): 2021-02-01 23:59:59
System.debug('startDateGMT: ' + startDateGMT); // startDateGMT: 2021-02-01 23:59:59 // Now it's in UTC
System.debug('String.valueOf(startDateGMT): ' + String.valueOf(startDateGMT)); // String.valueOf(startDateGMT): 2021-02-02 00:59:59 // So in my locale time it's the day after,
System.debug('startDateGMT2: ' + startDateGMT2); // startDateGMT2: 2021-02-01 23:59:59 // Same as startDateGMT
System.debug('String.valueOf(startDateGMT2): ' + String.valueOf(startDateGMT2)); // String.valueOf(startDateGMT2): 2021-02-02 00:59:59
public static DateTime toUTC(DateTime value) {
Integer offset = UserInfo.getTimezone().getOffset(value);
return value.addSeconds(offset/1000);
}
The output of startDateGMT and startDateGMT2 will be the same.
Noteworthy: DateTime fields are stored in GMT. When shown in the standard Salesforce UI, they're converted to the user's timezone.
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");
I am working on a contract and need a date to be the current date if {month3} is after the current date.
I have tried it every way imaginable, I have 2 scenarios where if {month3} is after the current date it prints {month3}, but if {month3} is prior to the current date it prints the current date and hides part of the text above it.
It works on one but not the other, Go easy on me, it's my first post!
IF TOTEXT({Month3},"MMMM dd, yyyy") >= TOTEXT((Currentdate),"MMMM dd, yyyy") THEN
TOTEXT((Currentdate),"MMMM dd, yyyy")
ELSE
TOTEXT({Month3},"MMMM dd, yyyy")
{Month 3} = 7/30/2020
From contract signing to July 30, 2020
From July 07, 2020 to the start of the official event date
the other one is right
{month3} = 5/18/20
Top sentence is hidden and it says:
From July 07, 2020 to the start of the official event date
It's because you compare text-strings instead of dates. Remove the TOTEXT-function and set the date format directly on the properties of the formula field.
IF {Month3} >= Currentdate THEN
Currentdate
ELSE
{Month3}
I would like convert string to date (dd/MM/YYYY) - this is useful for compare dates in TableColumns.
so i use DateTimeStringConverter (the string "01/11/2014" is a value of DatePicker)
DateTimeStringConverter format = new DateTimeStringConverter(Locale.FRANCE,
"dd/MM/YYYY");
Date d1 = format.fromString("01/11/2014");
d1.toString()
I don't obtain the right date but this date = "Mon Dec 30 00:00:00 CET 2013" !!!
I don't understand what is the problem (which in fact should not be a problem) ?
Any ideas ?
Thank you in advance
Fabrice
Why is below code producing output like this ?
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-mm-dd hh:mm:ss', oldDate )
Output : Sat Jan 05 01:34:54 EST 2013
When it should simply produce December 5 as the date in the output ? In fact in the original string whatever month I put in it produces the exact same output.
The problem is your date format isn't correct. mm is for minutes where MM is for month. So it should be:
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-MM-dd hh:mm:ss', oldDate )
You can find out more from the Java API docs for SimpleDateFormat.