2012-07-13 99 views
0

我必须为客户端构建没有任何库(如jQuery)的DatePicker。 我在本地机器上成功了。然而,我的客户现在正在使用它,如果它包含在他的网络应用程序中,它显示出一些奇怪的行为。Javascripts dateObject:从5月31日跳到下个月获取7月1日

如果我选择5月31日并滚动到下个月,我将在7月1日结束。 DateObject事实上有5月31日我点击按钮来启动“jumpToNextMonth”函数。 我假设dateObject跳转到6月31日,这是非展现,然后前往7月1日前进。 这种情况发生在8月以及其后的所有其他30天的月份之后是31天的月份。

它发射了上点击该生产线是

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

我看不到我的本地机器上的这种行为我也不看它运行的Apache服务器。 我无法想象会破坏客户端Web应用上的日期对象,但不幸的是我无法访问他们的文件。

我会很感激,如果你愿意帮我回答这两个问题:

  1. 它为什么我的本地机器
  2. 如何解决它发生时没有设定日为“1 “例如this.currentDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth() + 1, 1);

我发现类似的非回答问题,这里 Flex Mobile 4.6: DateSpinner dateAndTime jumping from Jan 31st to March 1st

+0

您已回答了您的问题。 6月31日实际上是7月1日。 – 2012-07-13 11:52:29

回答

0

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

这是否解决您的问题?

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); 
+0

好吧,我真的发现为什么它不会发生在我的本地机器上。 这是如何通过JavaScript DatePickers正常修复的?跳到每个月的第一天? – ProblemsOfSumit 2012-07-13 12:04:10

+0

我编辑了我的帖子。所提供的代码是否解决您的问题? – 2012-07-13 12:17:03

+0

它呢,谢谢。虽然我改变了主意,但我只是跳到每个月的第一天:-) – ProblemsOfSumit 2012-07-13 12:29:06

相关问题