简体   繁体   English

如何获取开始日期和结束日期之间的日期,结果日期应采用数组格式,使用 javascript

[英]How to get dates that are between Start date and End date and result date should be in an array format, using javascript

  1. I am trying to split day from full-date and calculating the range between them and inserting them into a array to add that to a database.我正在尝试将一天从全天分开并计算它们之间的范围并将它们插入到数组中以将其添加到数据库中。
   <script>
    function()
    {

      var sd = "26-04-2020";
      var a = sd.split("-");

      var ed = "28-04-2020";
      var b = ed.split("-");

      var p1= a[1];
      var p2= a[2];
      var p3= a[3];

      var q1= b[1];
      var q2= b[2];
      var q3= b[3];

      var datearry = [];

      if( p1<q1 && p2=q2 && p3=q3)
        {
           for (i=p1; i<q1; i++)
           {
             datearry = ("i"+"p2"+"p3");
           }
        }
        else if( p1<q1 && p2<q2 && p3=q3)
       {
         for (i=p1; i<q1; i++)
         {
          for (j=p2; j<q2; j++)
          {
            datearry = ("i"+"j"+"p3");
          }
        }
       }
       else if( p1<q1 && p2<q2 && p3<q3)
        {
          for (i=p1; i<q1; i++)
          {
            for (j=p2; j<q2; j++)
            {
              for (k=p3; k<q3; k++)
              {
                datearry = ("i"+"j"+"k");
              }
            }
          }
        }
     alert(datearry);
  }
  </Script>
  1. I did not get the expected result, anyone please give some suggestions or even code that works thankyou.我没有得到预期的结果,任何人都请提出一些建议,甚至是有效的代码,谢谢。

Your code has a number of issues:您的代码有很多问题:

 function()
    {

Is a syntax error, function declarations require a function name.是语法错误,function 声明需要一个function 名称。

 datearry = ("i"+"p2"+"p3");

datearray is initialised as an array, but here a string literal is assigned (literally "ip2p3"). datearray被初始化为一个数组,但这里分配了一个字符串文字(字面意思是“ip2p3”)。 You probably meant:你的意思可能是:

datearray.push(String(i) + p3 + p3);

which will add a string like "26042020" the first time, however as the algorithm progresses and the strings are turned into numbers, you'll start to get values like "152020" for 1 March 2020. So you need some padding, and probably a delimiter, to get "01-05-2020" and match the input format.这将在第一次添加像“26042020”这样的字符串,但是随着算法的进行并且字符串被转换为数字,您将开始获得 2020 年 3 月 1 日的值,例如“152020”。因此您需要一些填充,并且可能一个分隔符,以获得“01-05-2020”并匹配输入格式。

Lastly, the code doesn't seem to correctly accommodate transitions over month and year boundaries.最后,该代码似乎无法正确适应月份和年份边界的转换。 Fixing that really isn't useful as it's much more complex than it needs to be or I'm prepared to bother with given there are much simpler alternatives.修复它真的没有用,因为它比它需要的要复杂得多,或者我准备打扰,因为有更简单的选择。 :-) :-)

If you're looking for an array of timestamps between two dates, one strategy is to convert the timestamps to dates, then loop over the required range storing the required output timestamps in an array.如果您要查找两个日期之间的时间戳数组,一种策略是将时间戳转换为日期,然后遍历所需的范围,将所需的 output 时间戳存储在一个数组中。

Existing questions deal with how to parse a date string and how to format a date .现有问题涉及如何解析日期字符串以及如何格式化日期 You can put those together to create a function something like the following:您可以将它们放在一起创建一个 function,如下所示:

 // startDate, endDate in format dd-mm-yyyy // Return array of dates inclusive of start and end in same format function getDateRange(startDate, endDate) { // Parse dd-mm-yyyy let qParse = s => { let [d, m, y] = s.split(/\D/); return new Date(y, m-1, d); }; // Format date as dd-mm-yyyy let qFormat = d => { let z = n => (n<10? '0': '') + n; return z(d.getDate())+'-'+z(d.getMonth()+1)+'-'+d.getFullYear(); } // Setup for loop let start = qParse(startDate); let end = qParse(endDate); let result = []; // Loop from start to end, incrementing // the start date and writing to result array do { result.push(qFormat(start)); start.setDate(start.getDate() + 1); } while (start <= end) return result; } console.log(getDateRange('26-02-2020','03-03-2020')); // Different delimiters in input console.log(getDateRange('28.12.2020','03/01/2021'));

You should also validate the input.您还应该验证输入。 The parse and format functions are closed in the function as they're only designed to work with the specific format of the OP (ie they aren't general parsing or formatting functions).解析和格式化函数在 function 中被关闭,因为它们仅设计用于 OP 的特定格式(即它们不是一般的解析或格式化函数)。

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

相关问题 如何使用javascript获取给定开始日期和结束日期之间的所有日期? - How to get all dates between given start and end date using javascript? 如何使用 Pure JavaScript 以 14 天的重复模式在开始日期和结束日期之间获取选定的工作日日期 - how to get selected week days dates between Start date and end date with 14 day recurrence pattern using Pure JavaScript 查找是开始日期和结束日期之间的日期 - Find is date between array of start and end dates 在ExtJS中获取开始/结束日期之间的日期 - Get the dates between start/end date in ExtJS 如何使用Javascript在开始日期和结束日期之间中断数据 - How to break the data in between start date and end date using Javascript 如何在开始日期和结束日期之间创建日期数组? - HOW do I creating an array of dates between a start and end date? 获取开始日期和结束日期之间的日期数组 - Getting an array of dates between start date and end date JavaScript - 在两个日期之间循环。 开始和结束日期 - JavaScript - Loop between two dates. Start and end date 给定开始日期和结束日期,创建两者之间的日期数组 - Given a start and end date, create an array of the dates between the two 使用js获取开始日期和结束日期之间的周数 - Get the number of weeks between a start date and end date using js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM