Open cjmling opened 5 years ago
In laravel we can define that field as dates field in the model, which will automatically do it for us
https://laravel.com/docs/5.8/eloquent-mutators#date-mutators
protected $dates = ['post_date'];
Mutators like above doesn't seems to work when we want to read multiple datas and return json
for field post_date
above we could use accessor
public function getPostDateAttribute($value)
{
return (new DateTime($value))->format('c');
}
In Angular when using date input field we may need to provide timezone option in ng-model-options="{timezone: 'UTC'}"
Example
<input type="date" ng-model="post.post_date" placeholder="yyyy-MM-dd" ng-model-options="{timezone: 'UTC'}" ng-change="ctrl.dateChanged(post)">
<input type="time" ng-model="post.post_date" placeholder="HH:mm" ng-model-options="{timezone: 'UTC'}" ng-change="ctrl.dateChanged(post)">
In javascript to reset timezone offset we can do
var postDate = new Date(dateStr);
postDate.setMinutes(postDate.getMinutes() + postDate.getTimezoneOffset());
when we return date in format
Y-m-d H:i:s
( string with space in between and no timezone format ) from the server, browser will automatically convert the date variable into client timezone.We should send date from server in format like
2018-01-02T02:21:00+00:00
.Then in client we reset the time back to the required timezone value using
getTimezoneOffset
or in angular usingtimezone
SEO : angular