Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore Time Zone Angularjs

Is there a better way to ignore an timezone in Angularjs:

"2014-01-18 14:30:00" Instead Of "2014-01-18 15:30:00"

function Scoper($scope) {
    $scope.datum = "2014-01-18T14:30:00Z";
}

<div ng:app ng:controller="Scoper">
    DateTime  <br />
    Angular: {{datum | date:'yyyy-MM-dd HH:mm:ss'}} <br />
</div>

http://jsfiddle.net/samibel/2rMXJ/

like image 600
samibel Avatar asked Jan 15 '14 09:01

samibel


3 Answers

I was experimenting the same problem for a while. There is a timezone possible parameter to the date filter which I think should be the preferred solution, instead of making your own filter and append it. So, this is what worked for me:

{{ someAcceptedDateFormat | date : 'shortTime' : 'UTC' }}
like image 106
DanielM Avatar answered Nov 11 '22 12:11

DanielM


I found this answer: Why does angular date filter adding 2 to hour?

Here is an example:

Just pipe another filter:

app.filter('utc', function(){

  return function(val){
    var date = new Date(val);
     return new Date(date.getUTCFullYear(), 
                     date.getUTCMonth(), 
                     date.getUTCDate(),  
                     date.getUTCHours(), 
                     date.getUTCMinutes(), 
                     date.getUTCSeconds());
  };    

});

In your template:

<span>{{ date | utc | date:'yyyy-MM-dd HH:mm:ss' }}</span>
like image 14
Ilan Frumer Avatar answered Nov 11 '22 11:11

Ilan Frumer


I Have the solution:

app.filter('timezone', function(){

 return function (val, offset) {
        if (val != null && val.length > 16) {
    return val.substring(0, 16)
}    
return val;
    };
});

template:

 <span>{{ date | timezone | date:'yyyy-MM-dd HH:mm:ss' }}</span>

http://jsfiddle.net/samibel/n4CuQ/

like image 2
samibel Avatar answered Nov 11 '22 13:11

samibel