无法格式化MySQL默认的日期时间 [英] Unable to format default MySQL datetime

查看:254
本文介绍了无法格式化MySQL默认的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期出来的数据库看起来像这样的: 2013年11月21日十七点43分20秒

My dates come out of the database looking like this: 2013-11-21 17:43:20

我想用户角度的日期过滤器把他们弄成prettier,但是......

I'm trying to user Angular's date filter to turn them into something prettier, but...

{{Objected.created | date:'shortDate'}}

{{Objected.created | date:'YYYY'}}

...刚刚吐出来的是原来的日期时间字符串: 2013年11月21日十七点43分20秒。有没有错误。我在做什么错了?

...just spits out the original datetime string: 2013-11-21 17:43:20. There are no errors. What am I doing wrong?

更新
我看到了MySQL的默认日期时间是什么角度的数据过滤器预计不兼容。我试图将其转换就这样飞,但它引发错误:

Update I see that MySQL's default datetime is incompatible with what Angular's data filter expects. I'm attempting to convert it on the fly like this but it's throwing errors:

<li ng-repeat="result in data">{{ new Date(result.Job.created).toISOString() | date:'shortDate'}}</li>

我怀疑我不能实例化我想顺便Date类。错误是$解析:语法错误

I suspect I can't instantiate the Date class in the way I'm trying. The error is a $parse:syntax error.

更新

感谢@ M59的帮助下,我得到了它与一些小的调整工作...

Thanks to @m59's help, I got it working with a few minor adjustments...

HTML

<html ng-app="myApp">
...
{{Object.created | dateToISO | date:'shortDate'}}

JS:

var myApp = angular.module('myApp',[]);

myApp.filter('dateToISO', function() {
  return function(input) {
    input = new Date(input).toISOString();
    return input;
  };
});

这个自定义过滤器转换MySQL默认日期时间到该日期过滤器期望的格式,所以我把它扔之一,然后又和瞧。

This custom filter converts the default MySQL datetime into the format that the date filter expects, so I send it throw one then another and "voila".

推荐答案

您需要将日期字符串转换为通过角支撑的东西,像ISO 8601的格式。你可以把它转换是这样的:

You need to convert your date string to something supported by Angular, like ISO 8601 format. You could convert it like this:

$scope.Object.created = new Date($scope.Object.created).toISOString();

住在这里的演示(点击进入)。

Live demo here (click).

要做到这一点就飞,你需要自定义过滤器。 住在这里的演示(点击进入)。

To do this on the fly, you need a custom filter. Live demo here (click).

标记:

<div>{{Object.created | dateToISO | date:'shortDate'}}</div>

JavaScript的:

app.filter('dateToISO', function() {
  return function(input) {
    return new Date(input).toISOString();
  };
});

更新:

下面是转换您的日期手动(火狐)一个简单的方法:

Update:

Here's a simple way to convert your date manually (firefox):

app.filter('badDateToISO', function() {
  return function(badTime) {
    var goodTime = badTime.replace(/(.+) (.+)/, "$1T$2Z");
    return goodTime;
  };
});

这篇关于无法格式化MySQL默认的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆