Domanda

Il Google Feed API emette la data pubblicato nella formato:. "13 apr 2007 12:40:07 -0700"

desidero cambiamento che alla HTML5 valido . Usando JS / jQuery

C'è un modo semplice? O una libreria?

È stato utile?

Soluzione 3

Alla fine ho usato la funzione FormatoData () ha trovato qui . Il più recente JS Toolbox non sembra aver conservato questa funzione (o almeno non ce l'ha fatta ovvio).

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

L'affettamento è quello di ottenere il fuso orario, che FormatoData non sembrava supporto.

Questa è la funzione di minified, per la conservazione:

/*
 * Author: Matt Kruse <matt@mattkruse.com>
 * 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};

Altri suggerimenti

JavaScript Formato data . (. Scarica qui ) Allora è facile come:

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

Demo qui: http://jsfiddle.net/pkC3s/2/

Ho iniziato una soluzione per voi in puro JavaScript estendendo l'oggetto Date. Tuttavia, devo correre oggi.

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() );

Forse si può finirlo (o qualche volta lo farò oggi).

La speranza che aiuta ...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top