Set a trigger to run function the last hour of each month - javascript

In google scripts I know there are triggers to run by date, but I don't think that will work because month's have different amounts of days. So I was wondering if there's a way to set a trigger to run at 11PM on the last night of each month, no matter if that's a 30 or a 31.
Thanks

First create a trigger from project Edit > current project's trigger or register it programmatically that will run every day at 11 pm.
ScriptApp.newTrigger("myTriggerFunction")
.timeBased()
.atHour(23)
.everyDays(1)
.create();
Then in your trigger handler, check if today is the last day of the month, then do your work.
function myTriggrFunction()
{
var today = new Date();
var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);
if(today.getDate() == lastDayOfMonth.getDate() )
{
// your work to be done
}
}

Related

Get actual date and time with Javascript

I'm developing a website where registrations for a particular event will open on a certain date (say, January 1, 2019) and will close on another date (say, January 10, 2019). I'm using JavaScript to redirect users to the relevant pages if they try to access it on before the 1st or after the 10 of January.
My code so far:
var d = new Date();
var startDate = new Date(2019, 0, 1, 8);
var endDate = new Date(2019, 0, 10, 23, 59);
if(endDate-d<0) // Past expiration date
window.location = "register-closed.html";
else if(startDate-d > 0) // Before starting
window.location = "register-unavailable.html";
The main problem as you might have guessed is that this code takes the local date and time from the user; if I set the date on my device as 2nd January, 2019, I'm able to access the actual register page, even though it's May right now.
I feel this would be a common problem for many, but I've been unable to find any solution to this. How do I get the REAL date and time for my country (India) instead of the device time?
TL;DR
How do I get the actual date and time for a country (in my case, INDIA) using JavaScript? If I can't use vanilla JS, is there some other method to do so?
PS: If you have any solutions that can only be bypassed using methods more complicated than changing your device time, I'll readily accept them. This whole website is just for a high school event, so I don't expect any skilled hackers to spend their time on this :)
This code will get the date (as in the 30th), month, and year. This uses the new Date(); variable type. It has several uses, and you can get the output in whatever order using something like new Date(year, month, day, hours, minutes, seconds, milliseconds). That would output something like Wed May 22 2019 10:46:32 GMT-0400 (Eastern Daylight Time).
var todaysDate = new Date();
var date = todaysDate.getDate();
var month = todaysDate.getMonth();
var year = todaysDate.getFullYear();
if(date === 10 || month === 0 || year === 2019){
//January is 0 because counting starts at 0
...
}
https://www.w3schools.com/jsref/jsref_obj_date.asp
you should take a look to this topic as it seems to answer to your problem using only vanilla JS. Hope it helps :)

How can I highlight every 7th day from current day?

I have a fullcalendar defaults to week view. Current day is highlighted. There is another external event div from where an event need to drop on the calendar. This things works as designed. There is also a custom button clicking on which event should be added on the calendar. By default it drops on current date. But when user changes week, navigate to next or previous week, no day is selected.
I want to not only select every 7th day (+7 for next or -7 for previous) to be default day and change its color.
Difficult to provide full code, but here it, run the following link and set the view to week view. When you open week view, Friday 26th is current day and selected. When user navigate to Prev or next, I want to 2nd Nov or 19th Oct be the default day and be highlighted (color)
https://fullcalendar.io/docs/external-dragging-demo
I have tried few things without any success:
$('.fc-prev-button').click(function(){
//currCalDate is global variable to store the current day
currCalDate.setDate(currCalDate.getDate() - 7);
console.log(currCalDate);
$('#calendar').fullCalendar('gotoDate', currCalDate);
});
$('.fc-next-button').click(function(){
currCalDate.setDate(currCalDate.getDate() + 7);
$('#calendar').fullCalendar('gotoDate', currCalDate);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Then tried using dayRender in FC definition or may be something can be done with viewRender?
dayRender: function (date, cell) {
var today = new Date(currCalDate);
date = moment(date).toDate();
if (date.getDate() === today.getDate()) {
cell.css("background-color", "red");
}
},
Here, you can check the code for next 7th day highlight:
dayRender: function (date, cell) {
var today = new Date();
date = moment(date).toDate();
dateFromplus = moment().add(7,'d').format('YYYY-MM-DD');
$(".fc-day[data-date='"+dateFromplus+"']").css("background-color", "red");
dateFromminus = moment().subtract(7,'d').format('YYYY-MM-DD');
$(".fc-day[data-date='"+dateFromminus+"']").css("background-color", "red");
}
For more fullcalendar info : fullcalendar hacks

How can I get the next time it's 14:00?

Is it possible to get the next time it's 14:00 (for example) as a Date object in Javascript using Datejs?
The documentation doesn't seem to show such an example and trying next 14:00 doesn't really seem to work.
You don't really need a library for this. You can create a Date object and if it's after 14:00, set the date to tomorrow, then set time to 14:00.
Edit
The original would allow any time from 14:00 to 14:59:59.999 as it only checked the hours. Updated so that any time from 14:00:00.001 to 23:59:59.999 is treated as after 14:00.
Also added Date.js version (the documentation for which is minimal — perhaps there's something better).
// Create a date for now and remember the time
var d = new Date();
var time = +d;
// Set the time to 14:00 and, if earlier than now, add a day
d.setHours(14,0,0,0);
if ( d < time) {
d.setDate(d.getDate() + 1);
}
document.write('Next 14:00: ' + d)
Here is the Date.js version. I couldn't find a CDN to include as a snippet.
// Equivalent using Date.js
var d = Date.present();
var t = +d;
d.set({hour:14});
if (d < t) {
d.addDays(1);
}
document.write('<br>' + d3);
Or, as one line:
Date.today().set({hour:14}).isBefore(Date.present())? Date.today().addDays(1).set({hour:14}) : Date.today().set({hour:14});
// Or
Date.today().set({hour:14}).addDays(Date.today().set({hour:14}).isBefore(Date.present())? 1:0);
I don't recommend the one liner versions; they're just there to show it can be done. They each create 3 date objects, whereas the other versions create only 1.
I do not know how to use Datejs, but here is a simple solution in pure JavaScript. Hopefully it helps a bit. This sample gets the current date and checks if it is already past 14:00, and if so, creates a Date object for 14:00 the next day. If it is not already past 14:00, it creates a Date object for 14:00 that day.
var currentDate = new Date();
if (currentDate.getHours() >= 14){
var newDate = new Date(currentDate.getYear(), currentDate.getMonth(), currentDate.getDate()+1, 14, 0, 0, 0);
}
else{
var newDate = new Date(currentDate.getYear(), currentDate.getMonth(), currentDate.getDate(), 14, 0, 0, 0);
}

java script comparison for date; always showing message as current date is an earlier date

While selecting current date,show the message as You cannot select a day earlier than today.
function checkDate(sender, args) {
if (sender._selectedDate) < new Date()) {
alert("You cannot select a day earlier than today!");
sender._selectedDate = new Date();
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}
}
Comparison is not working properly.
Please help me.
I'm guessing that your sender._selectedDate has 0 for its hours, minutes, seconds, and milliseconds. The new Date() you are comparing it to will have hours, minutes, seconds, and milliseconds set to the current time, which is likely later than the _selectedDate. So, you should create the new date, and then set its hours, minutes, seconds, and milliseconds to 0. Then you can compare the dates properly. You can use setHours(), and pass in hours, minutes, seconds, and milliseconds. Also, thanks to #andrex for noticing that you also have a syntax error: remove the ) after `sender._selectedDate.
function checkDate(sender, args) {
var earliestDate = new Date();
earliestDate.setHours(0,0,0,0);
if (sender._selectedDate < earliestDate) {
alert("You cannot select a day earlier than today!");
sender._selectedDate = earliestDate;
sender._textbox.set_Value(sender._selectedDate.format(sender._format));
}
}
Not sure what the data of the date verification is used for so several solutions spring to mind.
Anyway I would divide the dates into its parts as (year/month/day/hours) etc and (+/-) find out if one or the other (year/month/day/hours) etc. doesnt compare or is earlier cq later then your checksum variable.
I gather you want to compare only (year/month/day) realy so when those check to be valid discard the rest!
var dag = nu.getDate();
var wkdag = nu.getDay();
var maand = nu.getMonth();
var jaar1 = nu.getYear();
btw. Thinking about it, it makes a whole lot of difference if the form is checked right away or later, since more time that passes between validation of the function and sending the message, more chance of the date being earlier, if checked right away and using server time least chance of errors, but even then if send at 24:00 hours, validation of the date cq day will be done the next day. You may think of some code to put a stopper in the hole!

Determine when it's monday by the minute

I feel this is a simple question, but there are several factors in play here which make it a bit more complex than it would seem.
For a site, we need to change a page weekly (with javascript/jQuery, no PHP or anything), with the transition being sunday 00:00 AM (from sunday to monday). It is important this happens on that minute/hour, due to page content being relevant for past and next weeks (actions, discounts, product advertisements etc.).
I searched a lot, tried several scripts, and i eventually ended up with this script:
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
};
var today = new Date();
var currentWeekNumber = today.getWeek();
//Returns weeknumber, content is adjusted through if(weeknumber == 35){etc.}
Now this seems to do what i want it to do, but when we tested it last sunday it seemed to initiate (at least) 3 hours too early as far as we could see (oddly enough not too many testers at sundaynight!).
Could anyone help me get underway with a proper script?
Something that adds to my struggle is timezones. I live in Amsterdam (GMT+2), and the visitors will be in the same timezone (Netherlands, the site is not aimed at people outside this zone).
Another thing that adds to the complexity for me, is that i am unable to test this more than 1 time a week. So any help with that would already be handy.
Summary: I need to change webcontent every week at sunday->monday 00:00 AM and a script to help me do that.
Something like this?
var d = new Date()
var n = d.getDay() //returns 0-6 for Sunday to Saturday
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var time = hours+':'+minutes+':'+seconds;
if(n==1 && time=='00:00:00'){
//do something
}

Categories

Resources