Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular date filter not working with Stripe timestamp

I have a timestamp supplied by Stripe's API. 1397166153.

However when using it in the most simplest form {{ 1397166153 | date }} I get: Jan 17, 1970 when in fact it should be Apr 10, 2014.

Does anyone know what's going on here?

I really appreciate your help!

like image 980
Stephen Bluck Avatar asked Apr 10 '14 22:04

Stephen Bluck


2 Answers

I always run all of my timestamps through a function, as JavaScript epoch is in milliseconds, and the Stripe response epoch is in seconds.

$scope.timestamp = function(epoch){
    return (epoch * 1000);
};

Then, in your template:

 <p>{{ timestamp(1397166153) | date }}</p>

Alternatively, if you need to just do it once, you can always do it inline:

 <p>{{ (1397166153 * 1000) | date }}</p>
like image 156
couzzi Avatar answered Oct 20 '22 00:10

couzzi


It appears that the timestamp is in Seconds.

var seconds = 1397166153; //your value
var ms = seconds * 1000;

new Date(seconds) // Fri Jan 16 1970 21:06:06 GMT-0700 (Mountain Standard Time)
new Date(ms) // Thu Apr 10 2014 15:42:33 GMT-0600 (Mountain Daylight Time)

I'm not sure what format Stripe sends it back in. But I've always had to convert to a Date object or to ms to work with Angular date filters.

like image 34
EnigmaRM Avatar answered Oct 20 '22 00:10

EnigmaRM