Unable to format default MySQL datetime(无法格式化默认的 MySQL 日期时间)
问题描述
我的日期从数据库中出来是这样的:2013-11-21 17:43:20
我正在尝试使用 Angular 的日期过滤器将它们变成更漂亮的东西,但是...
或
...只是吐出原始日期时间字符串:2013-11-21 17:43:20
.没有错误.我做错了什么?
更新我看到 MySQL 的默认日期时间与 Angular 的数据过滤器所期望的不兼容.我正在尝试像这样即时转换它,但它抛出错误:
我怀疑我无法以我尝试的方式实例化 Date 类.错误是 $parse:syntax 错误.
更新
感谢@m59 的帮助,我通过一些小的调整让它工作......
HTML:
JS:
这个自定义过滤器将默认的 MySQL 日期时间转换为日期过滤器期望的格式,所以我发送它一个又一个然后瞧".
您需要将日期字符串转换为 Angular 支持的格式,例如 ISO 8601 格式.你可以这样转换:
此处进行现场演示(点击).
要即时执行此操作,您需要一个自定义过滤器.现场演示(点击).
标记:
JavaScript:
更新:
这是一种手动转换日期的简单方法 (firefox):
My dates come out of the database looking like this: 2013-11-21 17:43:20
I'm trying to user Angular's date filter to turn them into something prettier, but...
or
...just spits out the original datetime string: 2013-11-21 17:43:20
. There are no errors. What am I doing wrong?
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:
I suspect I can't instantiate the Date class in the way I'm trying. The error is a $parse:syntax error.
Update
Thanks to @m59's help, I got it working with a few minor adjustments...
HTML:
JS:
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".
You need to convert your date string to something supported by Angular, like ISO 8601 format. You could convert it like this:
Live demo here (click).
To do this on the fly, you need a custom filter. Live demo here (click).
Markup:
JavaScript:
Update:
Here's a simple way to convert your date manually (firefox):
这篇关于无法格式化默认的 MySQL 日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!