Pregunta

los API de Google Feed emite la fecha publicada en el formato: "13 de abril de 2007 12:40:07 -0700".

Deseo cambiar eso al HTML5 válidou003Ctime datetime="YYYY-MM-DDThh:mm:ssTZD"> usando js/jQuery.

hay una manera fácil? O una biblioteca?

¿Fue útil?

Solución 3

Al final usé el función formatDate () que se encuentra aquí. La caja de herramientas JS más nueva no parecía haber conservado esta función (o al menos no lo hizo obvio).

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

El corte es obtener la zona horaria, que Formatdate no parecía soportar.

Aquí está la función minificada, para la preservación:

/*
 * 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};

Otros consejos

Verificar Formato de fecha de JavaScript. (Descargar aquí.) Entonces es tan fácil como:

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

Demostración aquí: http://jsfiddle.net/pkc3s/2/

Comencé una solución para usted en JavaScript puro extendiendo el objeto de fecha. Sin embargo, tengo que correr ahora.

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

Quizás puedas terminarlo (o en algún momento hoy).

Espero que ayude...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top