将 RFC 822 日期转换为 javascript 的有效日期
我正在使用脚本日历,当我选择日期时,它会将其转换为新格式(yyyy-mm-dd)
它适用于大多数浏览器,但在 Firefox 和 Opera 中,我得到无效的日期格式,因为我工作的格式是 RFC 822。
我正在寻找一种方法来转换此日期格式
示例:
Thu Sep 08 2011 12:00:00 GMT-0400 (EDT)
并将其更改为
2011-09-08
Could that be done in javascript ?
更新
这是我的代码,试图将 (EDT) 替换为空
$(".taskDate").datepick({
onSelect: function(selectedDate){
selectedDate = selectedDate.replace(/ \(.+\)/, '');
//alert(selectedDate);
var newDate = new Date(selectedDate);
$(".selectedDate").text(newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate());
location.href="index.php?date="+newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate();
}
});
现在我收到错误
selectedDate.replace is not a function
这是怎么回事?
更新2
修复了它,因为它似乎是一个对象而不是一个该死的字符串。 添加
selectedDate = selectedDate.toString();
在 new Date() 之前;
现在它适用于所有浏览器......
I'm using a script calendar that when I choose a date, it convert it to a new format (yyyy-mm-dd)
It works in most browser but in Firefox and Opera, I get an invalid date format because the format i work with is RFC 822.
I'm looking for a way to convert this date format
example:
Thu Sep 08 2011 12:00:00 GMT-0400 (EDT)
and change it to
2011-09-08
Could that be done in javascript ?
UPDATE
Here's my code trying to replace the (EDT) to nothing
$(".taskDate").datepick({
onSelect: function(selectedDate){
selectedDate = selectedDate.replace(/ \(.+\)/, '');
//alert(selectedDate);
var newDate = new Date(selectedDate);
$(".selectedDate").text(newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate());
location.href="index.php?date="+newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate();
}
});
Now I get an error
selectedDate.replace is not a function
How come ?
UPDATE 2
Fixed it because it seems that it was an object and not a darn string.
Added
selectedDate = selectedDate.toString();
before the new Date();
Now it's working for all browsers...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
适用于 Firefox6,请参阅我的 jsfiddle。
Works in Firefox6, see my jsfiddle.
由于日期是 RFC 822,您可以将其解析为有效日期(结束 EDT 不会影响结果):
这将与等于
"Thu Sep 08 2011 12 的
或dateInRFC822Format
一起使用: 00:00 GMT-0400 (EDT)""2011 年 9 月 8 日星期四 12:00:00 GMT-0400"
现在您可以从 dateAsDateObject 获取所需的信息:
dateAsDateObject.getFullYear()
dateAsDateObject.getMonth()
注意:用于格式化,如果你不介意使用 jqueryui 你也可以使用
$.datepicker.formatDate()
方法。例如var stringRepresentation = $.datepicker.formatDate('yy-mm-dd', dateAsDateObject);
Since the date is RFC 822 you could parse it to a valid Date (the ending EDT does not affect the result):
This will work with
dateInRFC822Format
equal to either"Thu Sep 08 2011 12:00:00 GMT-0400 (EDT)"
or"Thu Sep 08 2011 12:00:00 GMT-0400"
Now you can get the info you require from dateAsDateObject:
dateAsDateObject.getFullYear()
dateAsDateObject.getMonth()
dateAsDateObject.getDay()
Note: for formatting, if you don't mind using jqueryui you could also use the
$.datepicker.formatDate()
method. E.g.var stringRepresentation = $.datepicker.formatDate('yy-mm-dd', dateAsDateObject);
尝试:
Try: