function myFunction(){
var today = new Date();
var month = today.getMonth() 1;
var date = today.getDate();
if (month=== 1 && date===1){
window.location = "http://google.com";
}
if (month=== 1 && date===2){
window.location = "http://google.com";
}
if (month===1 && date===9){
window.location = "http://google.com";
}
if (month=== 2 && date===11){
window.location = "http://google.com";
}
if (month=== 2 && date===23){
window.location = "http://google.com";
}
if (month=== 3 && date===21){
window.location = "http://google.com";
}
if (month=== 4 && date===29){
window.location = "http://google.com";
}
if (month=== 5 && date===3){
window.location = "http://google.com";
}
if (month===5 && date===4){
window.location = "http://google.com";
}
if (month=== 5 && date===5){
window.location = "http://google.com";
}
if (month=== 7 && date===17){
window.location = "http://google.com";
}
if (month=== 8 && date===11){
window.location = "http://google.com";
}
if (month=== 9 && date===18){
window.location = "http://google.com";
}
if (month=== 9 && date===23){
window.location = "http://google.com";
}
if (month=== 10 && date===9){
window.location = "http://google.com";
}
if (month=== 11 && date===3){
window.location = "http://google.com";
}
if (month=== 11 && date===23){
window.location = "http://google.com";
} else{
window.location = "http://10.2.3.30:81";
}
}
so basically i have tried to incorporate javascript in my simple practice html file where by when i press a button the program should verify where it matches with the dates(public holidays) given and if it does it should redirect to another website or else it should be able to access the server. But the problem is that if any of the dates fall on a weekend then the holiday is carried over to a the staring of the new week day. And also i want to know how to get date and month from the internet rather than the date of the device the html runs on. Please help(beginner here)
CodePudding user response:
I use API http://worldtimeapi.org/api/timezone/Asia/Tokyo for getting the datetime for Tokyo by ajax calling.
url1 : http://worldtimeapi.org/api/timezone gives all the timezones with city/country names
url2 : http://worldtimeapi.org/api/timezone/Asia/Tokyo gives the datetime for tokyo with the following detais
{"abbreviation":"JST",
"client_ip":"117.245.152.80",
"datetime":"2022-08-09T16:14:30.067969 09:00",
"day_of_week":2,
"day_of_year":221,
"dst":false,
"dst_from":null,
"dst_offset":0,
"dst_until":null,
"raw_offset":32400,
"timezone":"Asia/Tokyo",
"unixtime":1660029270,
"utc_datetime":"2022-08-09T07:14:30.067969 00:00",
"utc_offset":" 09:00","week_number":32
}
$("#btn").on("click", function () {
$.ajax({
dataType: 'json',
url: 'http://worldtimeapi.org/api/timezone/Asia/Tokyo',
success: function (result) {
var today = new Date(result.datetime);
console.log(today);
myFunction(today);
}
});
});
function myFunction(idate){
var today = new Date(idate);
......
}
After getting the datetime pass it to the function you coded and assign to the variable you created, the remaing it will take care.
CodePudding user response:
There's an issue with your code, and having multiple if
statements like this is a code smell, and should be avoided at all costs.
As mentioned in my comment, you'll also always enter the else
statement unless it's the date mentioned in the if
statement preceding it.
function isPublicHoliday(){
const today = new Date();
const month = today.getMonth() 1;
const date = today.getDate();
const holidays = [
[1, 1],
[1, 2],
[1, 9],
[2, 11],
[2, 23],
[3, 21],
[4, 29],
[5, 3],
[5, 4],
[5, 5],
[7, 17],
[8, 11],
[9, 18],
[9, 23],
[10, 9],
[11, 3],
[11, 23]
];
for (const holiday of holidays) {
if (month === holiday[0] && date === holiday[1]) {
window.location = "http://google.com";
return;
}
}
window.location = "http://10.2.3.30:81";
}
Is a lot neater, doesn't have the bug in it, and is much more maintainable. If a public holiday is added, or removed, you just remove its entry from the array. You can also do this programmatically in the future, by adding hooks, or taking arguments in the function, but that's outside the scope of the question.
Oh, and name your functions properly, even when prototyping, it's just a good habit to get in to.
TBH, you also probably want to stick to using functions to do one thing, so in this case, you want to check if today is a public holiday. Which is a yes or no question. As such you want to get a yes or no response. Something like:
// Do stuff...
const todayIsAHoliday = isPublicHoliday();
if (todayIsAHoliday) {
window.location = "http://google.com";
} else {
window.location = "http://10.2.3.30:81";
}
// Do more stuff...
// Functions declared with this syntax will be `hoisted` when
// the JavaScript is run, so it's fine to have it defined at the
// bottom of your script.
function isPublicHoliday(){
const today = new Date();
const month = today.getMonth() 1;
const date = today.getDate();
const holidays = [
[1, 1], [1, 2], [1, 9], [2, 11],
[2, 23], [3, 21], [4, 29], [5, 3],
[5, 4], [5, 5], [7, 17], [8, 11],
[9, 18], [9, 23], [10, 9], [11, 3],
[11, 23]
];
for (const holiday of holidays) {
if (month === holiday[0] && date === holiday[1]) {
return true;
}
}
return false;
}
You can even implement some re-usability for your function, which is also always good to try and do, and allow for an argument containing the public holidays, so it can easily be modified at a later date.
Building on the previous example, that leads us to:
// Do stuff...
const currentHolidays = [
[1, 1], [1, 2], [1, 9], [2, 11],
[2, 23], [3, 21], [4, 29], [5, 3],
[5, 4], [5, 5], [7, 17], [8, 11],
[9, 18], [9, 23], [10, 9], [11, 3],
[11, 23]
];
const todayIsAHoliday = isPublicHoliday(currentHolidays);
if (todayIsAHoliday) {
window.location = "http://google.com";
} else {
window.location = "http://10.2.3.30:81";
}
// Do more stuff...
// We can give the argument a default value, for how the public
// holidays stand at the moment, but with an option to override
// them in the future.
function isPublicHoliday(holidays = [
[1, 1], [1, 2], [1, 9], [2, 11],
[2, 23], [3, 21], [4, 29], [5, 3],
[5, 4], [5, 5], [7, 17], [8, 11],
[9, 18], [9, 23], [10, 9], [11, 3],
[11, 23]
]) {
const today = new Date();
const month = today.getMonth() 1;
const date = today.getDate();
for (const holiday of holidays) {
if (month === holiday[0] && date === holiday[1]) {
return true;
}
}
return false;
}
There are other things I could mention, but that should give you a bit of insight in to things to think about in general when working with JavaScript.