2011-11-29 55 views

回答

0

在我使用的formatDate() function found here末。較新的JS工具箱似乎沒有保存這個功能(或者至少沒有明顯)。

var entryDate = new Date(entry.publishedDate); // From Google Feed API 
html5date = formatDate(entryDate,'yyyy-MM-ddThh:mm:ss')+entryDate.toString().slice(28,33); 

縱切是讓時區,這formatDate似乎並不支持。

這裏是縮小的功能,爲保存:

/* 
* Author: Matt Kruse <[email protected]> 
* http://www.mattkruse.com/ 
* formatDate (date_object, format) 
* Returns a date in the output format specified. 
* The format string uses the same abbreviations as in getDateFromFormat() 
*/ 
var MONTH_NAMES="January,February,March,April,May,June,July,August,September,October,November,December,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".split(","),DAY_NAMES="Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(",");function LZ(b){return(0>b||9<b?"":"0")+b} function formatDate(b,f){var f=f+"",h="",g=0,e="",c="",e=b.getYear()+"",c=b.getMonth()+1,i=b.getDate(),j=b.getDay(),d=b.getHours(),k=b.getMinutes(),l=b.getSeconds(),a={};4>e.length&&(e=""+(e-0+1900));a.y=""+e;a.yyyy=e;a.yy=e.substring(2,4);a.M=c;a.MM=LZ(c);a.MMM=MONTH_NAMES[c-1];a.NNN=MONTH_NAMES[c+11];a.d=i;a.dd=LZ(i);a.E=DAY_NAMES[j+7];a.EE=DAY_NAMES[j];a.H=d;a.HH=LZ(d);a.h=0==d?12:12<d?d-12:d;a.hh=LZ(a.h);a.K=11<d?d-12:d;a.k=d+1;a.KK=LZ(a.K);a.kk=LZ(a.k);a.a=11<d?"PM":"AM";a.m=k;a.mm=LZ(k);a.s= l;for(a.ss=LZ(l);g<f.length;){e=f.charAt(g);for(c="";f.charAt(g)==e&&g<f.length;)c+=f.charAt(g++);h=null!=a[c]?h+a[c]:h+c}return h}; 
+0

我意識到我應該使用datejs.com。好吧。 – thugsb

0

我通過擴展Date對象爲純JavaScript開始了一個解決方案。不過,我現在必須跑。

http://jsfiddle.net/2sYut/

Date.prototype.getHtml5String = function(){ 
    return this.getFullYear() + "-" + this.getMonth() + "-" + this.getDay() + "T" + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds() + "TZ" + this.getTimezoneOffset(); 
}; 

var d = new Date("13 Apr 2007 12:40:07 -0700"); 
alert(d.getHtml5String()); 

也許你能完成它(或者我有時會今天)。

希望幫助...

+0

雖然Homer6小提琴是有益的,這是不能令人信服。一些.getXXX函數表現怪異,可能是因爲Date對象沒有正確排序。 – thugsb

2

退房JavaScript Date Format。 (下載here)。然後,它的那麼容易,因爲:

var input = "13 Apr 2007 12:40:07 -0700"; 
var date = Date.parse(input); 
var output = dateFormat(date, 'yyyy-mm-dd"T"hh:MM:ssoD'); 
// output == 2007-04-13T12:40:07-0700D 

演示在這裏:http://jsfiddle.net/pkC3s/2/