无法格式化默认的 MySQL 日期时间

2021-11-09 00:00:00 datetime angularjs javascript mysql

我的日期从数据库中出来是这样的:2013-11-21 17:43:20

我正在尝试使用 Angular 的日期过滤器将它们变成更漂亮的东西,但是...

{{Objected.created |日期:'shortDate'}}

{{Objected.created |日期:'YYYY'}}

...只是吐出原始日期时间字符串:2013-11-21 17:43:20.没有错误.我做错了什么?

更新我看到 MySQL 的默认日期时间与 Angular 的数据过滤器所期望的不兼容.我正在尝试像这样即时转换它,但它抛出错误:

  • {{ new Date(result.Job.created).toISOString() |日期:'shortDate'}}
  • 我怀疑我无法以我尝试的方式实例化 Date 类.错误是 $parse:syntax 错误.

    更新

    感谢@m59 的帮助,我通过一些小的调整让它工作......

    HTML:

    ...{{Object.created |dateToISO |日期:'shortDate'}}

    JS:

    var myApp = angular.module('myApp',[]);myApp.filter('dateToISO', function() {返回函数(输入){input = new Date(input).toISOString();返回输入;};});

    这个自定义过滤器将默认的 MySQL 日期时间转换为日期过滤器期望的格式,所以我发送它一个又一个然后瞧".

    解决方案

    您需要将日期字符串转换为 Angular 支持的格式,例如 ISO 8601 格式.你可以这样转换:

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

    此处进行现场演示(点击).

    要即时执行此操作,您需要一个自定义过滤器.现场演示(点击).

    标记:

    {{Object.created |dateToISO |日期:'shortDate'}}

    JavaScript:

    app.filter('dateToISO', function() {返回函数(输入){返回新日期(输入).toISOString();};});

    更新:

    这是一种手动转换日期的简单方法 (firefox):

    app.filter('badDateToISO', function() {返回函数(坏时间){var goodTime = badTime.replace(/(.+) (.+)/, "$1T$2Z");回归美好时光;};});

    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...

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

    or

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

    ...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:

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

    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:

    <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;
      };
    });
    

    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:

    $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).

    Markup:

    <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;
      };
    });
    

    相关文章