简体   繁体   English

将RFC 822日期转换为JavaScript的有效日期

[英]Convert RFC 822 date to valid Date for javascript

I'm using a script calendar that when I choose a date, it convert it to a new format (yyyy-mm-dd) 我正在使用脚本日历,当我选择日期时,它将其转换为新格式(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. 它适用于大多数浏览器,但是在Firefox和Opera中,我得到的日期格式无效,因为我使用的格式是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 ? 可以用javascript完成吗?

UPDATE 更新

Here's my code trying to replace the (EDT) to nothing 这是我的代码试图将(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();
          }
        });

Now I get an error 现在我得到一个错误

selectedDate.replace is not a function

How come ? 怎么会 ?

UPDATE 2 更新2

Fixed it because it seems that it was an object and not a darn string. 修复了它,因为它似乎是一个对象,而不是织补字符串。 Added 添加

selectedDate = selectedDate.toString();

before the new Date(); 在新的Date()之前;

Now it's working for all browsers... 现在,它适用于所有浏览器...

Works in Firefox6, see my jsfiddle . 在Firefox6中可以使用,请参阅我的jsfiddle

var sOriginalDate = 'Thu Sep 08 2011 12:00:00 GMT-0400 (EDT)';
var oDate = new Date(sOriginalDate);
var iMonth = oDate.getMonth() + 1;
var iDay = oDate.getDate();
var sNewDate = oDate.getFullYear() + '-'
    + (iMonth < 10 ? '0' : '') + iMonth + '-'
    + (iDay < 10 ? '0' : '') + iDay;
alert(sNewDate);

Since the date is RFC 822 you could parse it to a valid Date (the ending EDT does not affect the result): 由于日期是RFC 822,因此您可以将其解析为有效的日期(结尾的EDT不会影响结果):

var dateAsDateObject = new Date(Date.parse(dateInRFC822Format));

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" 这将与dateInRFC822Format一起使用,该dateInRFC822Format等于"Thu Sep 08 2011 12:00:00 GMT-0400 (EDT)""Thu Sep 08 2011 12:00:00 GMT-0400"

Now you can get the info you require from dateAsDateObject: 现在,您可以从dateAsDateObject获取所需的信息:

  • year: dateAsDateObject.getFullYear() 年: dateAsDateObject.getFullYear()
  • month: dateAsDateObject.getMonth() 月: dateAsDateObject.getMonth()
  • day: dateAsDateObject.getDay() 日期: dateAsDateObject.getDay()

Note: for formatting, if you don't mind using jqueryui you could also use the $.datepicker.formatDate() method. 注意:对于格式化,如果您不介意使用$.datepicker.formatDate() ,则还可以使用$.datepicker.formatDate()方法。 Eg var stringRepresentation = $.datepicker.formatDate('yy-mm-dd', dateAsDateObject); 例如var stringRepresentation = $.datepicker.formatDate('yy-mm-dd', dateAsDateObject);

Try: 尝试:

var mydate = new Date(originaldate);
mydate = mydate.getYear() + '-' + (mydate.getMonth() + 1) + '-' + mydate.getDate();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM