Question

Le API Google Feed affiche la date publiée dans le Format:. "13 avril 2007 12:40:07 -0700"

Je souhaite que le changement du HTML5 valide . Utilisant JS / jQuery

Y at-il un moyen facile? Ou une bibliothèque?

Était-ce utile?

La solution 3

En fin de compte, j'ai utilisé la fonction formatDate () trouvé . Le plus récent JS Toolbox ne semble pas avoir conservé cette fonction (ou du moins n'a pas évident).

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

Le découpage est d'obtenir le fuseau horaire, qui formatDate ne semble pas supporter.

Voici la fonction minified, pour la conservation:

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

Autres conseils

Consultez JavaScript Format de date . (. Télécharger ) Ensuite, il est aussi simple que:

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

Démo ici: http://jsfiddle.net/pkC3s/2/

J'ai commencé une solution pour vous en javascript pur en étendant l'objet Date. Cependant, je dois courir maintenant.

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

Peut-être que vous pouvez finir (ou je quelque temps aujourd'hui).

L'espoir qui aide ...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top