使用JS和moment操作日期/时间 [英] Manipulating Date/time with JS and moment

查看:124
本文介绍了使用JS和moment操作日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个日期,我需要使用这2个日期创建第3个日期.

I have 2 dates and I need to create a 3rd date using those 2 dates.

if (document.getElementById("endDate"))
endDate = document.getElementById("endDate").value;

if (document.getElementById("presentDate"))
presentDate = document.getElementById("presentDate").value;

如果Present date = "12/5/2018"End date = "12/25/2018",则我的New date = "12/26/2018";

由于JavaScript日期月份介于0到11之间,而且日期有点凌乱,所以我没有得到想要的结果.

Since, JavaScript date months range from 0-11 and also the dates are kind of messy, I am not getting the desired results.

我尝试过的事情:

var presentDt = new Date(presentDate);
var endDt = new Date(endDate);

var newDay = endDt.getUTCDate()+1; 
var presentMonth = presentDt.getUTCMonth();
var presentYear = presentDt.getUTCFullYear();
var nextDate= presentMonth + '/' + endDay + '/' + presentYear;

  • 问题1:上面的代码有效,但是如果我的endDate在31日,则添加UTCDate+1使其成为32,这将返回无效的日期.

    • Issue 1: This above code works but if my endDate is on the 31st, then adding UTCDate+1 makes it 32, which returns invalid date.

      问题2:如果我执行UTCMonth(),则返回11,但是如果我要添加2个月,则返回13,这也是无效的.问题基本上是我无法根据需要操纵日期.

      Issue 2: If I do UTCMonth(), it returns 11 but if I want to add 2 months then it returns 13 which is also invalid. The issue is basically that I am not able to manipulate the dates as I want.

      我也试过了片刻,但是我遇到了类似的问题,而且我无法轻松地按我希望的方式操作日期.

      I also tried moment, however I am having similar issues and I am not able to manipulate the dates easily as I want them to.

      也尝试过getDategetMonth,但是它做同样的事情.

      Also tried getDate and getMonth but it does the same thing.

      是否有使用JavaScript处理总日期/时间的更好方法?

      Is there a better way of handling the overall date/time with JavaScript?

      推荐答案

      这就是我所做的:

      首先,我创建了以下函数来查看天数和月数.

      First i created the following functions to look at the number of days and months.

       function formattedDate(date) {
              var day = date.getDate();
              var monthIndex = date.getMonth();
              var year = date.getFullYear();
      
              return (monthIndex + 1) + '/' + day + '/' + year;
          }
          function myNewDate(datestring) {
              var dates = datestring.split("/");
              var year, month, day;
              if (dates.length < 3) {
                  dates = datestring.split("-");
                  year = dates[0];
                  month = dates[1] - 1;
                  day = dates[2];
              } else  {
                  year = dates[2];
                  month = dates[0] - 1;
                  day = dates[1];
              }
      
      
              var days = [31, 28, 31, 30, 31, 30 ,31, 31, 30, 31, 30, 31];
              // Overflow day
              if (day > days[month])
                  day = days[month];
      
              return new Date(year, month, day);
          }
          function newDate(date1, date2) {
              var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
              var year = date1.getFullYear();
              var month = date1.getMonth() + 1;
              var day = date2.getDate() + 1;
      
      
              // Check leap year
              if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                  days[1] = 29;
      
              // Overflow day
              if (day > days[month])
                  day = days[month];
      
              var newdate = new Date(year, month, day);
      
              return newdate;
          }
      

      在这里,我使用上述功能创建了新日期.您还可以通过更新上述内容来添加/减去天/月.

      Here, i used the above function to create my new date. You can also add/subtract days/months as you want by updating the above.

              var mpresent = myNewDate(presentDate);
              var mstart = myNewDate(startDate);
              var myDate =  (formattedDate(newDate(mpresent, mstart)));
      

      这篇关于使用JS和moment操作日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆