javascript - JS: Bug converting string to Date -


i converting data object json , json.stringify , json.parse.

this works great, on devices on samsung galaxy sii line:

console.log(jsonobj.gebdat+"::"+new date(jsonobj.gebdat)); 

i output:

1973-07-01t10:49:25.134z::invalid date

i implementing @ this answer, , works devices, doing wrong??

update

to clarify question. create string calling

var stringtosave = json.stringify({gebdat: dataclass.gebdat, <here more variables>}); 

then save it. later, load string , parse with

var jsonobj = json.parse(stringtosave); 

then, try set date again (calling log before line) with

console.log(jsonobj.gebdat+"::"+new date(jsonobj.gebdat)); this.gebdat = new date(jsonobj.gebdat); 

the log gives me invalid date shown above, , when represent date displays nan.nan.nan instead of expected 01.07.1973

1.date string formats implementation dependent. recommended use timestamps when save dates.

var timestamp = date.parse( new date() );//1372675910000 

now can use saved timestamps recreate date later

var date = new date(1372675910000);//mon jul 01 2013 16:21:50 gmt+0530 (india standard time) 

2.for simple transition current solution, in case dont handle different timezones,

var datestring = jsonobj.getdat.substring(0,23); var datepart = datestring.split('t')[0].split('-'); var timepart = datestring.split('t')[1].split(/[:.]/); var dateoj = new date(datepart[0], datepart[1], datepart[2], timepart[0], timepart[1], timepart[2]); 

let me clarify 1, reference update.

var stringtosave = json.stringify({gebdat: date.parse(dataclass.gebdat), <here more variables>});  var jsonobj = json.parse(stringtosave);  console.log('timestamp :' + jsonobj.gebdat);//1372680083000 console.log(new date(jsonobj.gebdat));//mon jul 01 2013 17:31:23 gmt+0530 (india standard time) 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -