本文介绍了Javascripts dateObject:从5月31日到下个月从7月1日起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不为客户端构建一个没有任何库(如jQuery)的DatePicker。
我在本地机器上成功了。然而,我的客户端现在正在使用它,如果它包含在他的网络应用程序中,它显示了一些奇怪的行为。

I had to build a DatePicker without any Library (like jQuery) for a client.I succeeded on my local machine. However my client is using it now and it shows some odd behaviour if its included in his web-app.

如果我选择5月31日并滚动到下个月我最终在7月1日。
DateObject infact在5月31日之前,我点击按钮启动jumpToNextMonth功能。
我假设dateObject跳到6月31日,这是不匹配的,然后去一个foward到7月1日。
这也发生在八月以及所有其他30天的月份,其次是31天 - 月。

If i select the 31st of May and scroll to the next month i end up at July 1st.The DateObject infact has May 31st before i click the button to fire up the "jumpToNextMonth" function.I assume the dateObject jumps to June 31st which is nonexistend and then goes one foward to 1st of july.This happens in August as well and all other 30-day-months which are followed by 31-day-months.

点击是

this.currentDate = new Date(this.currentDate.getFullYear(),
                           this.currentDate.getMonth() + 1,
                           this.currentDate.getDate());

我在本地机器上看不到此行为,也不会看到它运行apache服务器。
我无法想象什么破坏了我的客户端网络应用程序的日期对象,不幸的是我无法访问他们的文件。

I don't see this behaviour on my local machine nor do i see it running an apache server.I can't imagine what corrupts the date object on my clients web-app and unfortunately i don't have access to their files.

我会非常感谢您能帮我回答这两个问题:

I'd really appreciate if you'd help me answering these two questions:


  1. 为什么不会在本地机器上发生

  2. 如果将日期设置为1,我该如何解决这个问题 this.currentDate = new Date(this.currentDate.getFullYear(),this.currentDate.getMonth()+ 1,1);

  1. why is it not happening on my local machine
  2. how do i fix it without setting the Day to "1" e.g. this.currentDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth() + 1, 1);

我在这里找到类似的未回答的问题

I found similar non-answered questions hereFlex Mobile 4.6: DateSpinner dateAndTime jumping from Jan 31st to March 1st

推荐答案

您已经回答了自己的问题。 6月31日的对象实际上是7月1日。

You have answered your own question. June 31st in the object is effectively July 1st.

这是否解决了您的问题?

Does this solve your issue?

function daysInMonth(month, year)
{
    return 32 - new Date(year, month, 32).getDate();
}

var y = this.currentDate.getFullYear();
var m = this.currentDate.getMonth() + 1;
var d = Math.min(this.currentDate.getDate(), daysInMonth(m, y);
this.currentDate = new Date(y, m, d);

这篇关于Javascripts dateObject:从5月31日到下个月从7月1日起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!