I'm creating a script in Apps Script that will count a habit streak. It will use today's date to find the correct column on the Sheet and count backwards from there until it hits a missed day. To get today's date, I'm trying to use:
Utilities.formatDate(new Date(), "EST", "yyyy.MM.dd");,
which keeps giving me the error:
"Cannot find method formatDate(object,string,string)"
My code uses exactly the same syntax as they show on the references page other than my changing the time zone from "GMT" to "EST", but changing it back to "GMT" or to "GMT-5" didn't fix it, so I'm kind of at a loss.
I think it's an issue creating the date object from "new Date()", as I've tried to use other date methods like Date.now() to which it throws up "TypeError: Cannot find function now in object [object Object]." I figured this may be something to do with my scopes but I checked and it looked like I had all the ones I needed.
I also tried creating the Date object separately and then passing the variable into formatDate but had the same result.
Code I've tried is:
var day = Utilities.formatDate(day, "GMT-5", "yyyy.MM.dd");
------
var day = Utilities.formatDate(new Date(), "EST", "yyyy.MM.dd");
------
var day = new Date();
var day = day.now();
Let me know if you need more to go off of than that. Any help is appreciated, even if it's just using a different method to set a variable equal to today's date, thanks!!!
I got same situation, but i can fix it.
You did Utilities.formatDate(new Date(), "EST", "yyyy.MM.dd");
You may change like the following
var date = new Date;
var today= new Date(date.getFullYear(), date.getMonth(), date.getDate());
today = Utilities.formatDate(today, "EST", "yyyy/MM/dd");
Try running the following test in your script:
function test() {
Logger.log(Utilities.formatDate(new Date(), "EST", "yyyy.MM.dd"));
// output: "2020.02.07"
}
I got the expected output. As #TheMaster points out, you probably have conflicting variable names somewhere.
As soon as I created a new project the utilities starting working properly.
From #TheMaster's comments:
Try isolating the issue> create a new project>add in only one function> one line with utilities. See minimal reproducible example
You probably have conflicting variables somewhere, like variables with the same name. day is a common name... you might've used it in any of the other file scripts attached.
Related
Hello I am trying to set a date into Google Doc's document header. Now, here is the code I got so far but it won't change the current date inside my document's header as I close the document and re-open it any ideas what might be wrong with this code? on the document this is how I have my date: "Daily Report of: 10-19-20"
function onOpen() {
var date = Utilities.formatDate(new Date(), "GMT", "MM-dd-yyyy");
var pattern = "\\d{2}-\\d{2}-\\d{4}";
var header = DocumentApp.getActiveDocument().getHeader();
header.editAsText().replaceText(pattern, date);
}
Also please note that I do have a date in this format inside my header: 15-10-2020. But this should update to give me today's date. However, that's not happening.
Thanks
It is not happening because on your pattern you are looking for a date like 18.10.2020
Instead of using your pattern with . change it to be with -:
var pattern = "\\b\\d{2}\\-\\d{2}\\-\\d{4}\\b";
Also, I would recommend running the script from the Script editor the first time and checking if it works, then doing it by closing and re-opening the document.
I believe your goal as follows.
You want to update the text of 15-10-2020 at the header using var date = Utilities.formatDate(new Date(), "GMT-6", "dd-MM-yyyy") in Google Document using Google Apps Script.
In this case, I would like to propose to modify the value of pattern as follows.
Modified script:
Please modify as follows and test it again.
From:
var pattern = "\\b\\d{2}\\.\\d{2}\\.\\d{4}\\b";
To:
var pattern = "\\d{2}-\\d{2}-\\d{4}";
Added:
From your updated question, it was found that your value on Google Document was Daily Report of: 10-19-20. In this case, how about the following modification?
From:
var pattern = "\\b\\d{2}\\.\\d{2}\\.\\d{4}\\b";
To:
var pattern = "\\d{1,2}-\\d{1,2}-\\d{2,4}";
By this modification, the values of 10-19-20 and 10-19-2020 can be updated.
i am making an ionic app v2 in which i have to show 'today' instead of current date and 'yesterday' and 'tomorrow' also for respective dates. i tried using moment but it gives days till last week like last monday and reference time issue is also there with moment. i need for only these 3 days without reference time . can you tell me how to customize moment in ionic framework ? if you have any other suggestions than using moment . please tell. thanks in advance.
P.S: i want to implement this only in html code of ionic not in typescript code.
Just like this: moment().add(-1, 'days'). It will give you the previous day with the same exact current time that is on your local pc.
Ref here
Agreed that this will be difficult without JS/TS. In your .ts file couldn't you have 3 date member variables:
//Set up 3 new dates, defaulting them to today
yesterday: Date = new Date();
today: Date = new Date();
tomorrow: Date = new Date();
And then in your ctor or init method, set them up properly (below is likely not the most valid/efficient way, but is an example).
//Today is already set up from instantiation, but re-set tomorrow and yesterday
this.tomorrow.setDate(this.tomorrow.getDate() + 1);
this.yesterday.setDate(this.yesterday.getDate() - 1);
And then in your HTML, bind to them:
Yesterday was: {{yesterday?.toDateString()?.slice(0,10)}}
<br> Today is: {{today?.toDateString()?.slice(0,10)}}
<br> Tomorrow will be: {{tomorrow?.toDateString()?.slice(0,10)}}
I'm currently working with embarcadero c++, this is the first time I'm working with it so it's completely new to me.
What I'm trying to achieve is to get the current date, make sure the date has the "dd/MM/yyyy" format. When I'm sure this is the case I want to add a month to the current date.
So let's say the current date is 08/18/2016 this has to be changed to 18/08/2016 and then the end result should be 18/09/2016.
I've found that there is a method for this in embarcardero however I'm not sure how to use this.
currently I've only been able to get the current date like this.
TDateTime currentDate = Date();
I hope someone will be able to help me out here.
I figured it out.
After I've searched some more I found the way to use the IncMonth method on this page.
The example given my problem is as follows:
void __fastcall TForm1::edtMonthsExit(TObject *Sender)
{
TDateTime StartDate = edtStartDate->Text;
int Months = edtMonths->Text.ToInt();
TDateTime NextPeriod = IncMonth(StartDate, Months);
edtNextPeriod->Text = NextPeriod;
}
After looking at I changed my code accordingly to this
TDateTime CurrentDate = Date();
TDateTime EndDate = IncMonth(CurrentDate, 1);
A date object doesn't have a format like "dd/MM/yyyy". A date object is internally simply represented as a number (or possibly some other form of representation that really isn't your problem or responsibility).
So you don't have to check if it's in this format because no date objects will ever be in this format, they simply don't have a format.
You will have to do additions/subtractions on the Date object that the language or library gives you, THEN (optionally) you can format it to a human-readable string so it looks like 18/08/2016 or 18th of August 2016 or whatever other readable format that you choose.
It might be that the TRANSFER of a date between 2 systems is in a similar format, but then formatting the date like that is entirely up to you.
As for how to do that, the link you posted seems like a possible way (or alternatively http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.IncMonth), I'm afraid I can't give you an example as I'm not familiar with the tool/language involved, I'm just speaking generically about Date manipulations and they should ALWAYS be on the raw object.
I am trying to compare and check the date if it is today's date or not in a spesific program. I tried to use assertion method but when I use it the time will remain same if you try it next day. The main problem that I need to know when open a page from program It should be today's date and should be passed. if you know already anything about it please let me know also :)
Thanks yo!
Use System.DateTime.Now.ToString("yyyy-MM-dd") as one argument of the assertion. You may need to use a different format rather in the ...ToString() method. The exact format depends on how the date is shown on the screen.
This could be done using "StringAssert" to verify that your programs date string contains today's date string, while ignoring the time:
var programDateString = "7/25/2016 12:00:00"; //this is an example of your date retrieved from the application with time included
var todaysDate = System.DateTime.Today.ToShortDateString(); //short date string
StringAssert.Contains(programDateString, todaysDate);
Hi Im using firefox and CoffeeScript in an app, I want to get current date with momentjs using default method moment() however when I debug the code I seeinvalid Date, it is very weird, this is my code:
questionStarts =
started_at: moment()
running: true
Then later in my code I create another object and add the property
answer = {}
answer.started_at = questionStarts.started_at
But when I check answer.started_at I get back Invalid date any idea?
Moment has an alternate constructor for strange dates. Since I can't see the value of answer.started_at in your code, I can only guess about how to solve your problem.
Consider for example some strange date format like 08.16.2015 00:00:00. Trying to construct a moment object from it will give the same error you have, Invalid date. That's what happens if you tried constructing my example like this:
//this doesn't work, throws Invalid date message
var ex = moment('08.16.2015 00:00:00');
So, to fix my problem, I give another constructor that informs Moment of the strange date format.
//this does work, however
var ex = moment('08.16.2015 00:00:00', 'MM.DD.YYYY hh:mm:ss');
Now, I can use ex as a typical moment object taking advantage of moment's other methods such as format() diff() and isBetween().