new Date() doesn't get the correct outcome

You must Login before you can answer or comment on any questions.

titanium sdk:2.1.1 os: mac os platform: iphone simulator 5.1

i met a very curious problem, please see following three lines of code, quite simple:

Ti.API.info(post.created_at);
var _time = new Date(post.created_at);
Ti.API.info(_time);
the first info logs: 2012-10-03T01:54:38+0000 the second info logs: 1969-12--2147483629 -596:-31:-23 +0000, it seems that the new Date doesn't get the correct date. but on android platform, it gives me correct date.

— asked 8 months ago by kent hao
0 Comments

2 Answers

I'm assuming that post.created_at contains the string "2012-10-03T01:54:38+0000". So it looks like on iOS, the Date() constructor isn't able to parse your date correctly. Whenever I see a December 1969 date, I know we're dealing with a zero- or null-value date (as measured in seconds from the UNIX epoch).

I don't know the intricacies of exactly what string formats would be recognized by the Date() constructor on both platforms, but it's possible that this date format isn't recognized by the Date() implementation on iOS. The format looks pretty straightforward, so I'm kind of surprised by that.

At any rate, you might want to consider moment.js to see if it can reliably parse the date format for you.

Or see if you can provide the created_at date to your javascript in a numeric format rather than a string.

— answered 8 months ago by Jason Priebe
answer permalink
1 Comment
  • i found this issue in the issue tracker, maybe it's saying the same thing http://jira.appcelerator.org/browse/TIMOB-1640?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

    — commented 8 months ago by kent hao

this post's answer can also solve my problem http://developer.appcelerator.com/question/125827/date-conversion-need-some-help#219201

Your Answer

Think you can help? Login to answer this question!