I need help to create a drop down for day to pick from. Is there a way to do this? Thanks
using javascript you can populate that field
toget number of days in that month can use
var date = new Date()
which is javascript function which will give you today's date
and then use
date.getMonth()
date.getYear()
to get today's month and year
//Month is 1 based
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
//July
daysInMonth(7,2009); //31
//February
daysInMonth(2,2009); //28
daysInMonth(2,2008); //29
Related
var date = Utilities.formatDate(new Date(), "GMT-8", "m/dd/yyyy")
if (formS.getRange("B7").getValue() != " " && formS.getRange("B7").getValue() != date)
{
SpreadsheetApp.getUi().alert("Please Enter A Valid Date");
return
}
Trying to make the condition above check if the cell is not empty and that it does not contain a date prior to Today's Date
function myfunk() {
const ss = SpreadsheetApp.getActive();
const formS = ss.getSheetByName('formS');
const dtv = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()).valueOf();
if (!formS.getRange("B7").isBlank() && new Date(formS.getRange("B7").getValue()).valueOf() < dtv) {
SpreadsheetApp.getUi().alert("Please Enter A Valid Date");
return;
}
}
Checking Dates in Apps Script
In general you can use the Date object as you would in normal JavaScript code. There are just one main thing to bear in mind if your script needs to be sensitive to timezones.
The timezone is defined in the manifest:
This cannot be changed dynamically. So if you need to be sensitive to them, then you will need to manage the offsets in your code.
Your script
This line:
var date = Utilities.formatDate(new Date(), "GMT-8", "m/dd/yyyy")
Returns a string. Not a date object, so you can't compare it to another date object, such as what is returned from a sheet value if it is formatted as a date.
You could use Regex or split to get the year and month and compare it that way, but then you may run into issue when you use the script on the 1st of January. This is because by simply comparing the year, month and date of 31/12/2021 with 01/01/2022, then your conditional statements would be a bit tricky. Possible, but maybe a bit hard to read.
Initializing to midnight
What follows is one approach to take to carry out this comparison in a relatively simple way.
It seems convenient to get a date object initialized to 00:00:00 of today. Then you can quickly compare the date using Unix time.
var now = new Date()
now.setHours(0)
now.setMinutes(0)
now.setSeconds(0)
now.setMilliseconds(0)
You can also do this in a more concise way like this:
var now = new Date()
now.setHours(0,0,0,0);
Then you can use the getTime() method on the date objects to get Unic time in milliseconds and compare them.
var dateToCheck = formS.getRange("B7").getValue()
if (
!(dateToCheck instanceof Date) || // If value is not instance of a Date object
dateToCheck.getTime() <= now.getTime() // If date is before 00:00:00 today.
) {
SpreadsheetApp.getUi().alert("Please Enter A Valid Date");
return
}
}
Which seems like a concise way to do the comparison you are looking for.
References
Apps Script Dates
JS Date object
I am trying to add exactly one month to a date using Google script. This is what I have so far, which works fine up to a point;
var everyNth = sheet.getRange(2,2).getValue(); // cell which contains the date
everyNth.setMonth(everyNth.getMonth() + 1); // adds 1 month to the date
If date value is 5/25/2020 the above returns 6/25/2020 which is as intended
If date value is 2/29/2020 the above returns 3/29/2020 which is NOT as intended
I need it to return 3/31/2020, which is exactly one month after, same if the month has 30 or 31 days.
The Number of Days in the next month
This function adds the number of days in the next month to the dates in column A and puts the resulting date in column B
I have this data in column one:
1/25/2020
2/25/2020
3/25/2020
4/25/2020
5/25/2020
6/25/2020
7/25/2020
8/25/2020
9/25/2020
10/25/2020
11/25/2020
12/25/2020
If I run this function using the next month for each date in column one:
function dateplay() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');
const data=sh.getRange(1,1,sh.getLastRow(),1).getValues();
const output=sh.getRange(1,2,sh.getLastRow(),1).getValues();
output.forEach(function(e,i){
let dt=new Date(data[i][0]);
dt.setDate(dt.getDate()+daysInNextMonth(dt.getMonth()+1));
e[0]=dt;
});
sh.getRange(1,2,sh.getLastRow(),1).setValues(output);
}
function daysInNextMonth(m=0) {
return new Date(new Date().getFullYear(),m+1,0).getDate();
}
Then I get this output in column two that adds the numbers of days in the next month to the first column entrees:(I provided both columns this time)
1/25/2020,2/23/2020
2/25/2020,3/27/2020
3/25/2020,4/24/2020
4/25/2020,5/26/2020
5/25/2020,6/24/2020
6/25/2020,7/26/2020
7/25/2020,8/25/2020
8/25/2020,9/24/2020
9/25/2020,10/26/2020
10/25/2020,11/24/2020
11/25/2020,12/26/2020
12/25/2020,1/25/2021
Data is csv format.
How to get week number in google-apps-script?
For example, this week is Week37, How to get the 37 from google apps script?
Thank you very much..
Maybe there was no such functionality in 2015, but now just you can:
var data = Utilities.formatDate(new Date(), "GMT", "'Week'w");
// data = 'Week48' for example
You can use Utilities.formatDate(new Date(), "GMT", "u") which returns the number of the day in a week (1-Monday, 7-Sunday) and update week number depending on its value.
Add this to the top of your Apps Script:
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}
Then you can use the getWeek method, for example to get this week:
var now = new Date();
Logger.log(now.getWeek());
Source: http://javascript.about.com/library/blweekyear.htm
By default, Google Sheets start to count week numbers from Sunday.
If u want to start count from Monday you can do something like that:
function getWeek(date) {
return Number(Utilities.formatDate(new Date(date), "Europe/Kiev", "u")) === 7 ?
Number(Utilities.formatDate(new Date(date), "Europe/Kiev", "w")) - 1 :
Number(Utilities.formatDate(new Date(date), "Europe/Kiev", "w"));
}
So the code will analyze if the day of the week equal to 7 (Sunday) it will returns (currentWeek - 1), else currentWeek.
And you have to specify timeZone param to your country time zone.
This is the second param in Utilities.formatDate, in my case that was "Europe/Kiev", but you can find zone that you need here:
https://developers.google.com/adwords/api/docs/appendix/codes-formats#timezone-ids
If you need some more information what params you can throw in Utilities.formatDate you can find it here:
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Ok there is a pretty complicated thing I need done in Flash CS4 with as3. I've looked around the web but I couldn't find much useful info.
Basically I have 3 ComboBoxes, one for the UTC date, another for the UTC month and another for the UTC year. I need it to be done so that the comboboxes will show the dates inbetween a set date (a date which I initially set in the flash script) and the current UTC date. For example, if I put the set date as 1st of Feb 2013, and the current date is 4th of March, I want the user to only be able to select February and March in the 'month' combobox. If they select February, then the available dates in the 'date' combobox should be 1 - 28, but if March is selected then only 1 - 4 should be available. This should be able to update every day automatically, so for example on March 5th, the number 5 should be added so it should be 1 - 5 available on the 'date' combobox if March is selected and so on...
I honestly have no idea how to go about this, but I really need it done one way or another. If anyone could help me I would be thankful.
When you select a new month, your two boundary dates can update accordingly, then this function can help you generate the date in between
private function test():void
{
var date1:Date = new Date(2012, 11, 25);
var date2:Date = new Date();
generateDateBetween(date1, date2);
}
private function generateDateBetween(date1:Date, date2:Date):void
{
for (var i:Number = date1.time; i < date2.time; i+= 3600*24*1000)
{
var date:Date = new Date();
date.time = i;
trace(date);
}
}
Hi I'm having a problem setting a date in as3
here is the code i'm using
var endDate = new Date(2009,9,10);
trace (endDate);
the trace statement always shows the date as 1 month further on the the date I have added eg
10th Oct 2009 instead of 10th september 2009
Is there a way around this?
The month is 0 index.
var endDate = new Date(2009,9-1,10);
Yeah, dates are zero indexed in AS, so you'll need to subtract one
0 indexed like the other said. Try and take a look at this post for more tips on the date object:
How can you save time by using the built in Date class?
It might be because you are converting strings to numbers.
(Implicit coercion of a value of type String to an unrelated type Number.)
If you just make it:
var day:Number=parseInt("10");
var month:Number=parseInt("9");
var year:Number=parseInt("2009");
var adjMonth =month-1;
var endDate = new Date(year,adjMonth,day);
trace(endDate.toString());
It'll work fine.