admin管理员组文章数量:1334389
I have a date-filter
ponent that I am using in my Ember application that only works on initial render, not on a page reload, or even if I save a file (which triggers the application to live update).
In the main template of my application, I render the date-filter like this passing it a unix timestamp
{{date-filter unixepoch=item.date}}
Then, in ponents/date-filter.js
, I use a puted property called timeConverter
to change the unix epoch into a time string formatted according to user's language of choice, and then in my templates/ponents/date-filter.hbs
file I do {{timeConverter}}
to display the results
timeConverter: function(){
//step 1: get the epoch I passed in to the ponent
var epoch = this.get('unixepoch');
//step 2: create a human readable date string such as `Jun 29, 2015, 12:36PM`
var datestring = new Date(epoch)
//do language formatting --code omitted as the problem is with step2
}
It is step 2
that fails (returning invalid date
) if I refresh the page or even save the file. It always returns the proper date string the first time this ponent is called. Even if I do new Date(epoch)
in the parent ponent, and try to pass the result in to this ponent (to do foreign language formatting), I'm having the same problem.
Question: how can I figure out what's happening inside new Date(epoch), or whether it's an issue related to the ponent?
I have a date-filter
ponent that I am using in my Ember application that only works on initial render, not on a page reload, or even if I save a file (which triggers the application to live update).
In the main template of my application, I render the date-filter like this passing it a unix timestamp
{{date-filter unixepoch=item.date}}
Then, in ponents/date-filter.js
, I use a puted property called timeConverter
to change the unix epoch into a time string formatted according to user's language of choice, and then in my templates/ponents/date-filter.hbs
file I do {{timeConverter}}
to display the results
timeConverter: function(){
//step 1: get the epoch I passed in to the ponent
var epoch = this.get('unixepoch');
//step 2: create a human readable date string such as `Jun 29, 2015, 12:36PM`
var datestring = new Date(epoch)
//do language formatting --code omitted as the problem is with step2
}
It is step 2
that fails (returning invalid date
) if I refresh the page or even save the file. It always returns the proper date string the first time this ponent is called. Even if I do new Date(epoch)
in the parent ponent, and try to pass the result in to this ponent (to do foreign language formatting), I'm having the same problem.
Question: how can I figure out what's happening inside new Date(epoch), or whether it's an issue related to the ponent?
Share asked Jun 29, 2015 at 16:41 BrainLikeADullPencilBrainLikeADullPencil 11.7k24 gold badges81 silver badges138 bronze badges 4-
We would need to know what the value you get from
epoch
is. – T.J. Crowder Commented Jun 29, 2015 at 16:45 - Exactly what value are you passing in for the initialization? – Pointy Commented Jun 29, 2015 at 16:45
- 1 @Pointy it was a unix epoch created by Date.new(), but when it got saved it got saved as a string, which is why it didn't pute properly after the initial render. the answer by T.JCrowder fixed it for me. – BrainLikeADullPencil Commented Jun 29, 2015 at 17:41
- @BrainLikeADullPencil: :-) Heh, sometimes I guess lucky. Well, it was an educated guess... – T.J. Crowder Commented Jun 29, 2015 at 18:02
1 Answer
Reset to default 12I suspect your epoch
value is a string (of all digits). If so, then
var datestring = new Date(+epoch);
// Note ------------------^
...will fix it by converting it to a number (+
is just one way to do it, this answer lists your options and their pros/cons). Note that JavaScript uses the newer "milliseconds since The Epoch" rather than the older (original) "seconds since The Epoch." So if doing this starts giving you dates, but they're much further back in time than you were expecting, you might want epoch * 1000
to convert seconds to milliseconds.
If it's a string that isn't all digits, it's not an epoch value at all. The only string value that the specification requires new Date
to understand is the one described in the spec here (although all major JavaScript engines also understand the undocumented format using /
[not -
] in U.S. date order [regardless of locale]: mm/dd/yyyy
— don't use it, use the standard one).
本文标签: javascriptnew Date(epoch) returning invalid date inside Ember componentStack Overflow
版权声明:本文标题:javascript - new Date(epoch) returning invalid date inside Ember component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742261024a2442515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论