简体   繁体   English

javascript 的公共假期

[英]public holiday in javascript

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.所以基本上我已经尝试在我的简单练习 html 文件中加入 javascript能够访问服务器。 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.而且我还想知道如何从互联网上获取日期和月份,而不是 html 运行的设备的日期。 Please help(beginner here)请帮助(这里是初学者)

I use API http://worldtimeapi.org/api/timezone/Asia/Tokyo for getting the datetime for Tokyo by ajax calling.我使用 API http://worldtimeapi.org/api/timezone/Asia/Tokyo通过 ajax 调用获取东京的日期时间。

url1: http://worldtimeapi.org/api/timezone gives all the timezones with city/country names url1: http://worldtimeapi.org/api/timezone给出了所有带有城市/国家名称的时区

url2: http://worldtimeapi.org/api/timezone/Asia/Tokyo gives the datetime for tokyo with the following detais url2: http://worldtimeapi.org/api/timezone/Asia/Tokyo给出了东京的日期时间,详情如下

{"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.获取日期时间后,将其传递给您编码的 function 并分配给您创建的变量,剩下的会处理。

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.您的代码存在问题,并且有多个这样的if语句是一种代码味道,应该不惜一切代价避免。

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.正如我在评论中提到的,除非它是前面if语句中提到的日期,否则您也将始终输入else语句。

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.您将来也可以通过添加挂钩或在 function 中采用 arguments 以编程方式执行此操作,但这不在问题的 scope 范围内。

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 . TBH,您可能还想坚持使用函数来做一件事,所以在这种情况下,您要检查今天是否是公共假期 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.您甚至可以为您的 function 实现一些可重用性,这也总是很好的尝试和执行,并允许包含公共假期的参数,因此可以在以后轻松修改。

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.还有其他一些事情我可以提到,但这应该让您对使用 JavaScript 时一般需要考虑的事情有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM