/    Sign up×
Community /Pin to ProfileBookmark

How to set a function to activate on a specific date at a specific time?

How to set a function to activate on a specific date at a specific time? eg
June 21 19:36

var d = new Date();
var h = d.getHours();
var m = d.getMinutes();

Brain gone to mud…something like
function showEl() {
if (d == 21 June && h == 19 && m == 36) { $(‘#el’).fadeIn(1200) };
}
don’t understand how to format the date…

Cheers.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rootJun 14.2018 — simplest way of setting a future date is to set the date with an ISO format date.

today = new Date();<br/>
theDate = new Date("2018-06-30T10:30:00");<br/>
difference = theDate.getTime() - today.getTime();


so theDate will be the future date, today is todays date, you subtract one from the other, if the result is 0 or more (positive not negative result) then the event has happened or passed.

You can then use an interval timer to check every second to see if the event happens or not...

Also, I note that you have some JQuery in there... so if you are using JQuery, you are in the wrong place as JQuery is not JavaScript, its a framework that happens to use JavaScript as its base language, written in JavaScript BUT NOT JavaScript its self (as in the JQuery language bit) ?
Copy linkTweet thisAlerts:
@Oddjob41authorJun 14.2018 — Many thanks. jQ caveat understood: but here, the jQ is independent of the timing question.
Copy linkTweet thisAlerts:
@rootJun 14.2018 — An ISO date string format is : YYYY-MM-DD at its simplest and you can have a time in there as well YYYY-MM-DDTHH:MM:SS to set the time.

So using something like...
now = new Date();
// the end is every year at the same time...
end = new Date( ( now.getFullYear() )+"-06-21T19:36:00");

if( now.getTime() &gt;= end.getTime() ){
// ended!
console.log("ended");
}

Which is fine if the page only needs to check once on load... will lead to a routine that will check every second the page is open...

So you need to have a function that will be called by setInterval that will automate checking and in that function, when the criteria is met, your function can then kill off the interval timer and then do what is needed.

function ended(){
now = new Date();
// the end is every year at the same time...
end = new Date( ( now.getFullYear() )+"-06-21T19:36:00");
if( now.getTime() &gt;= end.getTime() ){
// ended!
clearInterval( checkTime );
console.log("ended");
}
console.log( now );
}
checkTime = setInterval("ended()",1000);
*untested.

is the basic idea, try playing with it and work out what you need to do to make your script do likewise... F12 will open the console tab, there is an inspector window, then some tabs, one says console, the output will appear in there.
×

Success!

Help @Oddjob41 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...