HighDots Forums  

json e le date

Javascript (Italian) Il linguaggio JavaScript (it.comp.lang.javascript)


Discuss json e le date in the Javascript (Italian) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
max
 
Posts: n/a

Default json e le date - 10-17-2009 , 12:20 PM






Ciao.a tutti.
Devo visualizzare in una pagina una data in formato json
"DataOrdine":"\/Date(1254651688450)\/" qualcuno sa dirmi come posso
fare a convertirla in formato gg/mm/aaaa.
Grazie anticipatamente a chi mi da una mano.

Reply With Quote
  #2  
Old   
Simone (Demo) Gentili
 
Posts: n/a

Default Re: json e le date - 10-18-2009 , 05:04 AM






On 17 Ott, 18:20, max <msanfel... (AT) gmail (DOT) com> wrote:
Quote:
Ciao.a tutti.
Devo visualizzare in una pagina una data in formato json
"DataOrdine":"\/Date(1254651688450)\/" qualcuno sa dirmi come posso
fare a convertirla in formato gg/mm/aaaa.
Grazie anticipatamente a chi mi da una mano.
Mi sembra tanto uno unixtimestamp, se conosci il formati tratta quel
numerone come meglio credi, Diversamente non so. Con php per generare
uno unixtimestamp altro non devi fare che mktime
(ore,minuti,secondi,giorno,mese,anno); al contrario, se vuoi conoscere
la data, date("Y/m/d", $unixTimestamp);

Non è esattamente quello che hai chiesto ma spero possa darti qualche
spunto.

Reply With Quote
  #3  
Old   
enos76
 
Posts: n/a

Default Re: json e le date - 10-18-2009 , 08:09 AM



max wrote:
Quote:
Devo visualizzare in una pagina una data in formato json
"DataOrdine":"\/Date(1254651688450)\/" qualcuno sa dirmi come posso fare
a convertirla in formato gg/mm/aaaa. [...]

Estrai la stringa, convertila in numero, infine usa il numero per creare
un oggetto Date da cui estrarre i campi che ti servono.


parseDate = function(jsonInput) {
/* free software by enos76 */
var myJsonObject;
if (JSON) {
myJsonObject = JSON.parse('{'+jsonInput+'}');
} else {
myJsonObject = eval('({'+jsonInput+'})');
}
if (!myJsonObject.DataOrdine) {
throw "campo data assente";
}
var from = myJsonObject.DataOrdine.indexOf("(") + 1;
var to = myJsonObject.DataOrdine.indexOf(")");
var timestampString = myJsonObject.DataOrdine.substring(from, to);
var timestampInt = parseInt(timestampString,10);
if (isNaN(timestampInt)) {
throw "formato data errato";
}
var date = new Date(timestampInt);
var day = "" + date.getDate();
if (day.length === 1) {
day = "0" + day;
}
var month = "" + date.getMonth();
if (month.length === 1) {
month = "0" + month;
}
var year = "" + date.getFullYear();
var parsedDate = "" + day + "/" + month + "/" + year;
return parsedDate;
}


testParseDate = function() {
var jsonInput = '"DataOrdine":"\\/Date(1254651688450)\\/"';
try {
document.write(parseDate(jsonInput));
} catch(exception) {
document.write("ERRORE: " + exception +".");
}
}();


Saluti
--
Enos /* looking4work */

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.