Getting day of the week not working when using setNumberFormat - google-apps-script

I would like programably enter the date into a cell and format it to include the Day of the week (Mon, Tue, Wed,...). If I use the .setNumberFormat method (which I would prefer to do because it keeps the info as a date), the simpleDateFormat for Day of the week does not work. If I use Utilities.formatDate I can use 'EEE, MM/dd' and it will show up correctly, but I lose the date format.
function setformat(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var lab = ss.getSheetByName("test2");
// This is what I want to use but the day of the week 'EEE' doesn't work
var todaySNF = new Date();
lab.getRange("a1").setValue(todaySNF);
lab.getRange("a1").setNumberFormat('EEE, MM/dd'); // Should read Wed, 09/23 but reads EEE, 09/23 instead.
var cellA1asDate = new Date(lab.getRange("a1").getValue());
Logger.log(cellA1asDate);}

use this instead :
lab.getRange("a1").setNumberFormat('DDD, MM/dd');

Related

google app script how to get date in string date(spread sheet)

I want to compare the date that was created in the spreadsheet with a specific date.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var spreaddate = ss.getSheetByName("sheet123").getRange("B2").getValue();
//--> Thu Dec 12 00:00:00 GMT+09:00 2019
var comparedate = new date(yyyy-mm-dd)//somting like this
if(spreaddate > comparedate){
do something
}
The spreaddate is not recognized as a date.
Even if I try to convert it to a date, it's hard because it's written in letters, not numbers like Thu Dec.
What should I do?
I think the problem is that you're not correctly defining your comparedate. You need to use a capital 'D' new Date().

How to format Date in Google Script

I am trying to write a small Google Script which takes data from some cells of Google Sheet and Paste it in the GMAIL.
However, while pasting the 'Date Values' it always displays it in the following manner:-
Mon Apr 15 2019 00:00:00 GMT+0530 (IST)
Fri Apr 26 2019 00:00:00 GMT+0530 (IST)
But I need the dates in an appropriate way i.e.
"Mon Apr 15 2019" or
"Fri 04/26/2019"
I gone through these possible options i.e. Utilities.formatDate & .Split but somehow I am not able to write these codes appropriately. Can you please help me with this matter. Below I have mentioned the entire issue in detail.
My Code
function temp2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName('Sheet1');
var templateSheet = ss.getSheetByName('Sheet2');
var emailTemplate2 = templateSheet.getRange("G1").getValue();
var rec = templateSheet.getRange("C1").getValue();
var sub = templateSheet.getRange("D1").getValue();
var date = templateSheet.getRange("E1").getValue();
// Logger.log(rec);
MailApp.sendEmail(rec, sub, emailTemplate2);
}
here var date = templateSheet.getRange("E1").getValue(); is the part of code which picks value of date.
Do let me know if you need more details in this regard
Regards,
Alok
Requirement:
Format date value from cell in Google Apps Script.
Solution:
Pass value to date constructor new Date() then format using Utilities.formatDate().
// pass date to date constructor
var date = new Date(templateSheet.getRange("E1").getValue());
// "Mon Apr 15 2019" example
var formattedDate = Utilities.formatDate(date, "GMT+0", 'E MMM dd yyyy');
// "Fri 04/26/2019" example
var formattedDate = Utilities.formatDate(date, "GMT+0", 'E MM/dd/yyyy');
Note: I've set this to timezone "GMT+0" by default, you can change this to whichever time zone you need.
References:
new Date() for date constructor.
Utilities.formatDate() for formatting dates in Google Apps Script.
SimpleDateFormat for date format strings.

google apps script convert string to object

I have a question about the date convert:
in sheet, I have a date '05/11/2018', I need to get next date 06/11/2018.
var end=Utilities.formatDate(new Date(coach_date.getTime()+1*3600000*24), 'GMT', 'dd/MM/yyyy');
var start = new Date();
var events = calendar.getEvents(start,end);
it shows the 'end' is string, not object. it has to be getEvents(object,object)
so I used end = new Date(end); it got '11/06/2018', change month from Nov to June.
How could I fix it, then use it on getevents() feature.
Thanks a lot.
Utilities.formatDate() converts the date object into a string. So, use
var end = coach_date.setDate(coach_date.getDate()+1);

Utilities.formatDate returning wrong week number

In Google Script I have the following code:
var myDate = new Date(sheet.getRange(3,1).getValue());
var year = Utilities.formatDate(myDate, "Europe/Amsterdam", 'dd-MM-yyyy');
var weekyear = Utilities.formatDate(myDate, "Europe/Amsterdam", 'dd-MM-YYYY');
var week = Utilities.formatDate(myDate, "Europe/Amsterdam", 'w');
When I insert 30-12-2015 as date, the result will be.
// year: 30-12-2015
// weekyear: 30-12-201**6**
// week: 1
In my local timezone, it should be week number 53. Not 1.
If I calculate the week in ISO 8601, the result is 52.
Strange, isn't it?
Edited:
New attempt, with this code
var cursus_datum = sheet.getRange(3,1).getValue();
Logger.log('type of data: ' + typeof cursus_datum);
Logger.log(cursus_datum);
Logger.log(Utilities.formatDate(cursus_datum, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "d-MM-y HH:mm"));
Logger.log(Utilities.formatDate(cursus_datum, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "w"));
This results in
// type of data: object
// Wed Dec 30 09:00:00 GMT+01:00 2015
// 30-12-2015 00:00 which is perfect
// 1 which is **not** correct.
The wrong week is the bug.
The Utilities.formatDate documentation states that it uses Java SimpleDateFormat - I have a feeling this might be the cause of the problem as they are Locale sensitive and I would take a guess it will be using the default US locale (I don't think passing in the timezone changes the locale). The problem is the US locale calendar has getFirstDayOfWeek() = 1 (SUNDAY) and getMinimalDaysInFirstWeek() = 1. And for ISO 8601 you need the settings 2 and 4 respectively. I think you're better off sticking to the Javascript code you linked to if you want to work with week years.
new Date() will accept a few different parameter configurations, but you still need to make sure that the parameters are correct. You can't just plug anything into new Date() If the variable is already a date type, then there is no point using new Date(). You can test for the data type with typeof.
var dateFromSheet = sheet.getRange(3,1).getValue();
Logger.log('type of data: ' + typeof dateFromSheet);
If the data type is a string, it must be in a valid date string format. There are a few different formats. But, again, you need to use a valid format.
ISO 8601 syntax "YYYY-MM-DD" or "YYYY-MM" or "YYYY" or "YYYY-MM-DDTHH:MM:SS"
Long Date syntax - Year, month and day can be in any order: "Mar 7 2015" or "2015 Mar 7", and Month can be written in full: "2015 March 7"
Short dates - "/" or "-" can be used. "MM/DD/YYYY" or "YYYY/MM/DD" NOTE: You can not use "DD/MM/YYYY"
Full Date Format - "Wed Mar DD YYYY HH:MM:SS GMT+0100 (W. Europe Standard Time)"
This is what I did:
function getCurrentCalendarWeek(){
let date = new Date();
let weekNum = Utilities.formatDate(date, "Europe/Berlin", "w") - 1;
if (weekNum === 0){
weekNum = 1
}
return weekNum
}

Trying to subtract 5 days from a defined date - Google App Script

I'm trying to write a script which is supposed to send out an email and create two calender entries when submitting a form. To be honest, this is my first script and I am very happy that the email is send out and the calender entries are working as well. The thing which gives me a headache is to subtract 5 days (actually x days) from a defined date.
First I thought I could simply do something like
var liveday = e.values[2];
var newday = liveday-5;
well, this didn't work :-)
I tried more:
var newtime = new Date(liveday);
var yr = newtime.getYear();
var dt = newtime.getDay();
var mt = newtime.getMonth();
var dtnew = dtnew.setDate(mt, dt-5, yr);
But here I received 1418256000000 whereas liveday = 12/01/2014. Not sure why days were added, rather than subtracted.
I am quite confused here and the answer can't be that hard.
I just want to subtract 5 days from 12/01/2014 to receive 11/27/2014.
Thanks for having a look
the comment sends you to a rather complicated serie of codes... there is a far more simple way to get that, here is the code :
function test() {
Logger.log('today= '+new Date()+' and 5 days ago is '+subDaysFromDate(new Date(),5));
}
function subDaysFromDate(date,d){
// d = number of day ro substract and date = start date
var result = new Date(date.getTime()-d*(24*3600*1000));
return result
}
Logger result :
[13-11-18 23:39:50:364 CET] today= Mon Nov 18 2013 23:39:50 GMT+0100 (CET) and 5 days ago is Wed Nov 13 2013 23:39:50 GMT+0100 (CET)
if you want to get the date in the form dd/mm/yyyy use Utilities.formatDate(date, timeZone, 'dd/MM/yyyy), see doc here